- 积分
- 316
- 明经币
- 个
- 注册时间
- 2004-4-6
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
最近想做一条多段线顶点压缩,条件如下:首先将一条多段线的首尾相连,然后求出个点到该连线的距离,求出最大值的点,用该最大值和事先给定的阀值比较,若大于阀值,则保留该点,若小于阀值,则删除该点,再将该点和首尾点各自相连,设起点到该点的连线为L1,该点到结束点的连线为L2,然后用同样的方法依次求出在起点到该点之间的点到L1的距离,求出最大值的点,同理,求出到L2最大距离的点,继续和阀值比较,依次类推,求出满足条件的所有的点,最后将这些点连成一条新的多段线,这就是压缩过的多段线,部分代码如下:// #include "StdAfx.h" #include "StdArx.h" #include"acedads.h" #include"adscodes.h" #include <stdlib.h> #include <rxobject.h> #include <rxregsvc.h> #include <aced.h> #include <dbents.h> #include <adslib.h> #include <geassign.h> #include "math.h" double pt[50][3],h[50]; double v=10; void listPline(); void iterate(AcDbObjectId id); int maxi(int,int); void studyaaddd() { listPline(); } void listPline() { int rc; ads_name en; AcGePoint3d pt; rc = acedEntSel("\nSelect a polyline: ", en, asDblArray(pt));
if (rc != RTNORM) { acutPrintf("\nError during object selection"); return; }
AcDbObjectId eId; acdbGetObjectId(eId, en);
AcDbObject *pObj; acdbOpenObject(pObj, eId, AcDb::kForRead); if (pObj->isKindOf(AcDb2dPolyline::desc())) { pObj->close(); iterate(eId); } else { pObj->close(); acutPrintf("\nSelected entity is not an AcDb2dPolyline. \nMake sure the setvar PLINETYPE is set to 0 before createing a polyline"); } } void iterate(AcDbObjectId plineId) { AcDb2dPolyline *pPline; acdbOpenObject(pPline, plineId, AcDb::kForRead);
AcDbObjectIterator *pVertIter= pPline->vertexIterator(); pPline->close();
AcDb2dVertex *pVertex; AcGePoint3d location; AcDbObjectId vertexObjId; int i=0; for (int vertexNumber = 0;!pVertIter->done(); vertexNumber++,pVertIter->step()) { vertexObjId = pVertIter->objectId(); acdbOpenObject(pVertex, vertexObjId, AcDb::kForRead); location = pVertex->position(); pt[0]=float(location[X]); pt[1]=float(location[Y]); pt[2]=float(location[Z]); pVertex->close(); acutPrintf("\npt[%d][0]=%.4f,pt[%d][1]=%.4f,pt[%d][2]=%.4f", i,location[X],i,location[Y],i,location[Z]); i++; }
delete pVertIter; acutPrintf("\n总共%d个顶点",i); int m,n; n=i-1; double max1,a1,b1,c1,e1,s1; b1=sqrt((pt[1][1]-pt[0][1])*(pt[1][1]-pt[0][1])+(pt[1][0]-pt[0][0])*(pt[1][0]-pt[0][0])); a1=sqrt((pt[2][1]-pt[0][1])*(pt[2][1]-pt[0][1])+(pt[2][0]-pt[0][0])*(pt[2][0]-pt[0][0])); c1=sqrt((pt[2][1]-pt[1][1])*(pt[2][1]-pt[1][1])+(pt[2][0]-pt[1][0])*(pt[2][0]-pt[1][0])); e1=(a1+b1+c1)/2; s1=sqrt(e1*(e1-a1)*(e1-b1)*(e1-c1)); max1=(2*s1)/a1; switch(i) {case 2:acutPrintf("\n不符合条件,无法求距离!");return; case 3:acutPrintf("\n第2点距离为%f:",max1); acutPrintf("\n无最大值!"); } if(i>3) { acutPrintf("\n该点为pt[%d]且最大值h[%d]=%lf",m,m,h[m]); for(m=maxi(0,n);p=maxi(0,n);n-p>1;m>1;m=maxi(0,m);p=maxi(p,n)) {k1=maxi(maxi(0,m),m); u1=maxi(p,maxi(p,n)); for(k2=k1;k3=k1;m-k3>1;k2-maxi(0,m)>1;k2=maxi(maxi(0,m),k2);k3=maxi(k3,m))
} } int maxi(int x,int y) {int z; double a,b,c,e,s,max=0; for(int j=x+1;j<y;j++) {a=sqrt((pt[y][1]-pt[x][1])*(pt[y][1]-pt[x][1])+(pt[y][0]-pt[x][0])*(pt[y][0]-pt[x][0])); b=sqrt((pt[j][1]-pt[x][1])*(pt[j][1]-pt[x][1])+(pt[j][0]-pt[x][0])*(pt[j][0]-pt[x][0])); c=sqrt((pt[y][1]-pt[j][1])*(pt[y][1]-pt[j][1])+(pt[y][0]-pt[j][0])*(pt[y][0]-pt[j][0])); e=(a+b+c)/2; s=sqrt(e*(e-a)*(e-b)*(e-c)); h[j]=(2*s)/a; if(h[j]>max) {max=h[j]; z=j; } acutPrintf("\nh[%d]=%lf",j,h[j]); } acutPrintf("\n第%d点距离最大为%lf:",z+1,max); return z; } 在顶点遍历中可能需要采用递归和循环思路,可是我花了好些时间也不能理顺思路,真是急啊,恳请各位高手指点一下有什么好的思路和方法啊,如何去用递归和循环来实现顶点遍历的问题,在下感激不尽!!!!! |
|