[求助]如何根据顺OR逆时针来确定点的顺序
<p>大家好,有个问题向大家请教:</p><p>比如有一个多线段组成的封闭图形,各个节点依次衔接起来。如A,B,C,D,E'''''',我的问题是如果根据顺OR逆时针来确定点的顺序。</p>
<p>如附件(cad2004)</p>
<p> </p> <p>在晓东看到的Arx的代码,最近太忙了,有时间改写下</p>
<p><font face="Verdana"><a href="http://www.xdcad.net/forum/showthread.php?s=&threadid=34540&perpage=15&display=&pagenumber=1">http://www.xdcad.net/forum/showthread.php?s=&threadid=34540&perpage=15&display=&pagenumber=1</a></font></p> <p>谢谢~</p> 试下:
public static double GetArea(this IEnumerable<Point2d> pnts)
{
IEnumerator<Point2d> it = pnts.GetEnumerator();
it.MoveNext();
Point2d start = it.Current;
Point2d p2 = start, p1;
double area = 0;
while(it.MoveNext())
{
p1 = p2;
p2 = it.Current;
area += (p1.X * p2.Y - p2.X * p1.Y);
}
area = (area + (p2.X * start.Y - start.X * p2.Y)) / 2.0;
return area;
}
public static bool IsClockWise(this IEnumerable<Point2d> pnts)
{
return pnts.GetArea() <= 0;
}
测试代码
public void test()
{
var db = HostApplicationServices.WorkingDatabase;
var doc = Application.DocumentManager.GetDocument(db);
var ed = doc.Editor;
var resEnt = ed.GetEntity("\n请选择优化多段线:");
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Polyline pl = resEnt.ObjectId.GetObject(OpenMode.ForWrite) as Polyline;
var pnts =GetAll<Point2d>(0, pl.NumberOfVertices - 1, i => pl.GetPoint2dAt(i));
ed.WriteMessage("\n面积:{0}\n顺时针:{1}", pnts.GetArea(), pnts.IsClockWise());
tr.Commit();
}
}
public IEnumerable<T> GetAll<T>(int start, int end, Func<int,T> func)
{
for (int i = start; i <= end; i++)
{
yield return func(i);
}
}
效果:
命令: ctest
请选择优化多段线:
面积:304398.872847913
顺时针:False
我来顶一下
页:
[1]