明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2085|回复: 5

[几何] [求助]如何根据顺OR逆时针来确定点的顺序

[复制链接]
发表于 2010-6-1 10:56 | 显示全部楼层 |阅读模式

大家好,有个问题向大家请教:

比如有一个多线段组成的封闭图形,各个节点依次衔接起来。如A,B,C,D,E'''''',我的问题是如果根据顺OR逆时针来确定点的顺序。

如附件(cad2004)

 

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2010-6-1 21:06 | 显示全部楼层

在晓东看到的Arx的代码,最近太忙了,有时间改写下

http://www.xdcad.net/forum/showthread.php?s=&threadid=34540&perpage=15&display=&pagenumber=1

 楼主| 发表于 2010-6-2 07:47 | 显示全部楼层

谢谢~

发表于 2010-6-13 16:24 | 显示全部楼层
试下:
  1.         public static double GetArea(this IEnumerable<Point2d> pnts)
  2.         {
  3.             IEnumerator<Point2d> it = pnts.GetEnumerator();
  4.             it.MoveNext();
  5.             Point2d start = it.Current;
  6.             Point2d p2 = start, p1;
  7.             double area = 0;
  8.             while(it.MoveNext())
  9.             {
  10.                 p1 = p2;
  11.                 p2 = it.Current;
  12.                 area += (p1.X * p2.Y - p2.X * p1.Y);
  13.             }
  14.             area = (area + (p2.X * start.Y - start.X * p2.Y)) / 2.0;
  15.             return area;
  16.         }
  17.         public static bool IsClockWise(this IEnumerable<Point2d> pnts)
  18.         {
  19.             return pnts.GetArea() <= 0;
  20.         }
复制代码
发表于 2010-6-13 16:37 | 显示全部楼层
测试代码
  1.         [CommandMethod("CTest")]
  2.         public void test()
  3.         {
  4.             var db = HostApplicationServices.WorkingDatabase;
  5.             var doc = Application.DocumentManager.GetDocument(db);
  6.             var ed = doc.Editor;
  7.             var resEnt = ed.GetEntity("\n请选择优化多段线:");
  8.             using (Transaction tr = db.TransactionManager.StartTransaction())
  9.             {
  10.                 Polyline pl = resEnt.ObjectId.GetObject(OpenMode.ForWrite) as Polyline;
  11.                 var pnts =GetAll<Point2d>(0, pl.NumberOfVertices - 1, i => pl.GetPoint2dAt(i));
  12.                 ed.WriteMessage("\n面积:{0}\n顺时针:{1}", pnts.GetArea(), pnts.IsClockWise());
  13.                 tr.Commit();
  14.             }
  15.         }
  16.         public IEnumerable<T> GetAll<T>(int start, int end, Func<int,T> func)
  17.         {
  18.             for (int i = start; i <= end; i++)
  19.             {
  20.                 yield return func(i);
  21.             }
  22.         }

效果:
命令: ctest
请选择优化多段线:
面积:304398.872847913
顺时针:False
发表于 2010-7-22 12:20 | 显示全部楼层
我来顶一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-29 00:49 , Processed in 0.391785 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表