[转]多边形点集排序--针对凸多边形,按逆时针方向进行排序
2013-11-13 10:08
323 查看
原文地址:/article/4983856.html
原文是C++下的,稍微的改了为C#的,呵呵
主要方法:
辅助方法:
原文是C++下的,稍微的改了为C#的,呵呵
主要方法:
public static void ClockwiseSortPoints(List<Point3D> vPoints) { //计算重心 Point3D center = new Point3D(); double X = 0, Y = 0; for (int i = 0; i < vPoints.Count; i++) { X += vPoints[i].X; Y += vPoints[i].Y; } center.X = (int)X / vPoints.Count; center.Y = (int)Y / vPoints.Count; //冒泡排序 for (int i = 0; i < vPoints.Count - 1; i++) { for (int j = 0; j < vPoints.Count - i - 1; j++) { if (PointCmp(vPoints[j], vPoints[j + 1], center)) { Point3D tmp = vPoints[j]; vPoints[j] = vPoints[j + 1]; vPoints[j + 1] = tmp; } } } }
辅助方法:
//若点a大于点b,即点a在点b顺时针方向,返回true,否则返回false static bool PointCmp(Point3D a, Point3D b, Point3D center) { if (a.X >= 0 && b.X < 0) return true; if (a.X == 0 && b.X == 0) return a.Y > b.Y; //向量OA和向量OB的叉积 int det = Convert.ToInt32((a.X - center.X) * (b.Y - center.Y) - (b.X - center.X) * (a.Y - center.Y)); if (det < 0) return true; if (det > 0) return false; //向量OA和向量OB共线,以距离判断大小 double d1 = (a.X - center.X) * (a.X - center.X) + (a.Y - center.Y) * (a.Y - center.Y); double d2 = (b.X - center.X) * (b.X - center.Y) + (b.Y - center.Y) * (b.Y - center.Y); return d1 > d2; }
相关文章推荐
- .NET清除Session 的几个方法[clear/removeAll/remove/Abandon]
- 在CentOS 6.4上安装daemontools(守护进程工具)
- 前端开发知识结构
- Java读取xls文件
- VS2005串口通信程序打开多余的文件,导致窗口自动关闭
- EBS AP、AR所有含CCID的业务表
- fabric远程部署实践
- 多线程使用利弊
- sql 执行顺序
- Java程序员必知的8大排序
- iOS学习之sqlite的创建数据库,表,插入查看数据
- mysql的一些配置优化
- xxx
- BW增强数据源的两种方法
- 使用ffmpeg快速生成视频截图
- MOSH 快捷方便的mobile shell
- IntelliJ、ReSharper 4折 加入慧都“惊喜惠”
- 第一手知识——切身体验
- 解决Win7硬盘无法弹出BitLocker解密窗口即无法解锁的情况
- qsort 功 能: 使用快速排序例程进行排序