拆解凹多边形

偶遇需要拆解凹多边形

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;namespace DrawPolygon
{public static class Settings{public const float Epsilon = 1.192092896e-07f;/// <summary>/// The maximum number of vertices on a convex polygon./// </summary>public static int MaxPolygonVertices = 7;}public class Vertices : List<Point>{public Vertices(){}public Vertices(int capacity){Capacity = capacity;}public Vertices(Point[] Point){for (int i = 0; i < Point.Length; i++){Add(Point[i]);}}public Vertices(IList<Point> vertices){for (int i = 0; i < vertices.Count; i++){Add(vertices[i]);}}public int NextIndex(int index){if (index == Count - 1){return 0;}return index + 1;}public Point NextVertex(int index){return this[NextIndex(index)];}public int PreviousIndex(int index){if (index == 0){return Count - 1;}return index - 1;}public Point PreviousVertex(int index){return this[PreviousIndex(index)];}public float GetSignedArea(){int i;float area = 0;for (i = 0; i < Count; i++){int j = (i + 1) % Count;area += (float)(this[i].X * this[j].Y);area -= (float)(this[i].Y * this[j].X);}area /= 2.0f;return area;}public const float Pi = 3.14159f;public bool IsCounterClockWise(){return Count < 3 ? true : (GetSignedArea() > 0.0f);}public void ForceCounterClockWise(){if (!IsCounterClockWise()){Reverse();}}public override string ToString(){StringBuilder builder = new StringBuilder();for (int i = 0; i < Count; i++){builder.Append(this[i].ToString());if (i < Count - 1){builder.Append(" ");}}return builder.ToString();}}public static class BayazitDecomposer{private static int num = 1;private static int stop = 20;private static int stopTemp = 12;public static List<Vertices> GetConvexPartition(PointCollection points){return GetConvexPartition(new Vertices(points));}public static List<Vertices> GetConvexPartition(Vertices vertices){stop = 50;stopTemp = 20;return ConvexPartition(vertices, 1);}private static List<Vertices> ConvexPartition(Vertices vertices, int verticesTemp){stopTemp++;num = verticesTemp;//verticesTemp为1时正常分解,为2时代表遇到不能分解的轮廓,这时返回null,并放弃这个轮廓的更新if (stop == 1){stop = vertices.Count;}if (stopTemp >= stop){num = 2;}if (num == 1){if (vertices == null)return null;vertices.ForceCounterClockWise();List<Vertices> list = new List<Vertices>();float d, lowerDist, upperDist;Point p;Point lowerInt = new Point();Point upperInt = new Point();int lowerIndex = 0, upperIndex = 0;Vertices lowerPoly, upperPoly;for (int i = 0; i < vertices.Count; ++i){try { Reflex(i, vertices); }catch{num = 2;return null;}if (Reflex(i, vertices)){lowerDist = upperDist = float.MaxValue;for (int j = 0; j < vertices.Count; ++j){// if line intersects with an edgeif (Left(At(i - 1, vertices), At(i, vertices), At(j, vertices)) && RightOn(At(i - 1, vertices), At(i, vertices), At(j - 1, vertices))){// find the point of intersectionp = LineTools.LineIntersect(At(i - 1, vertices), At(i, vertices), At(j, vertices), At(j - 1, vertices));if (Right(At(i + 1, vertices), At(i, vertices), p)){// make sure it's inside the polyd = SquareDist(At(i, vertices), p);if (d < lowerDist){// keep only the closest intersectionlowerDist = d;lowerInt = p;lowerIndex = j;}}}if (Left(At(i + 1, vertices), At(i, vertices), At(j + 1, vertices)) && RightOn(At(i + 1, vertices), At(i, vertices), At(j, vertices))){p = LineTools.LineIntersect(At(i + 1, vertices), At(i, vertices), At(j, vertices), At(j + 1, vertices));if (Left(At(i - 1, vertices), At(i, vertices), p)){d = SquareDist(At(i, vertices), p);if (d < upperDist){upperDist = d;upperIndex = j;upperInt = p;}}}}// if there are no vertices to connect to, choose a point in the middleif (lowerIndex == (upperIndex + 1) % vertices.Count){Point sp = new Point((lowerInt.X + upperInt.X) / 2, (lowerInt.Y + upperInt.Y) / 2);lowerPoly = Copy(i, upperIndex, vertices);lowerPoly.Add(sp);upperPoly = Copy(lowerIndex, i, vertices);upperPoly.Add(sp);}else{double highestScore = 0, bestIndex = lowerIndex;while (upperIndex < lowerIndex) upperIndex += vertices.Count;for (int j = lowerIndex; j <= upperIndex; ++j){if (CanSee(i, j, vertices)){double score = 1 / (SquareDist(At(i, vertices), At(j, vertices)) + 1);if (Reflex(j, vertices)){if (RightOn(At(j - 1, vertices), At(j, vertices), At(i, vertices)) && LeftOn(At(j + 1, vertices), At(j, vertices), At(i, vertices))){score += 3;}else{score += 2;}}else{score += 1;}if (score > highestScore){bestIndex = j;highestScore = score;}}}lowerPoly = Copy(i, (int)bestIndex, vertices);upperPoly = Copy((int)bestIndex, i, vertices);}//在每次递归时都进行检测,遇到不能分解的就开始跳出循环var lslowerPoly = ConvexPartition(lowerPoly, num);var lsupperPoly = ConvexPartition(upperPoly, num);if (lslowerPoly == null || lsupperPoly == null){return null;}list.AddRange(lslowerPoly);list.AddRange(lsupperPoly);return list;}}// polygon is already convexif (vertices.Count > Settings.MaxPolygonVertices){lowerPoly = Copy(0, vertices.Count / 2, vertices);upperPoly = Copy(vertices.Count / 2, 0, vertices);//当轮廓的点数目大于Settings.MaxPolygonVertices时分解轮廓的带你数目var lslowerPoly = ConvexPartition(lowerPoly, num);var lsupperPoly = ConvexPartition(upperPoly, num);if (lslowerPoly == null || lsupperPoly == null){return null;}list.AddRange(lslowerPoly);list.AddRange(lsupperPoly);}else{list.Add(vertices);}//The polygons are not guaranteed to be without collinear points. We remove them to be sure.for (int i = 0; i < list.Count; i++){list[i] = SimplifyTools.CollinearSimplify(list[i], 0);}//Remove empty vertice collectionsfor (int i = list.Count - 1; i >= 0; i--){if (list[i].Count == 0){list.RemoveAt(i);}}return list;}return null;}private static bool Reflex(int i, Vertices vertices){return Right(i, vertices);}private static bool Right(int i, Vertices vertices){return Right(At(i - 1, vertices), At(i, vertices), At(i + 1, vertices));}private static bool Left(Point a, Point b, Point c){return MathUtils.Area(ref a, ref b, ref c) > 0;}private static bool LeftOn(Point a, Point b, Point c){return MathUtils.Area(ref a, ref b, ref c) >= 0;}private static bool Right(Point a, Point b, Point c){return MathUtils.Area(ref a, ref b, ref c) < 0;}private static bool RightOn(Point a, Point b, Point c){return MathUtils.Area(ref a, ref b, ref c) <= 0;}private static float SquareDist(Point a, Point b){float dx = (float)(b.X - a.X);float dy = (float)(b.Y - a.Y);return dx * dx + dy * dy;}private static Point At(int i, Vertices vertices){int s = vertices.Count;return vertices[i < 0 ? s - (-i % s) : i % s];}private static Vertices Copy(int i, int j, Vertices vertices){Vertices p = new Vertices();while (j < i) j += vertices.Count;for (; i <= j; ++i){p.Add(At(i, vertices));}return p;}private static bool CanSee(int i, int j, Vertices vertices){if (Reflex(i, vertices)){if (LeftOn(At(i, vertices), At(i - 1, vertices), At(j, vertices)) &&RightOn(At(i, vertices), At(i + 1, vertices), At(j, vertices))) return false;}else{if (RightOn(At(i, vertices), At(i + 1, vertices), At(j, vertices)) ||LeftOn(At(i, vertices), At(i - 1, vertices), At(j, vertices))) return false;}if (Reflex(j, vertices)){if (LeftOn(At(j, vertices), At(j - 1, vertices), At(i, vertices)) &&RightOn(At(j, vertices), At(j + 1, vertices), At(i, vertices))) return false;}else{if (RightOn(At(j, vertices), At(j + 1, vertices), At(i, vertices)) ||LeftOn(At(j, vertices), At(j - 1, vertices), At(i, vertices))) return false;}for (int k = 0; k < vertices.Count; ++k){if ((k + 1) % vertices.Count == i || k == i || (k + 1) % vertices.Count == j || k == j){continue;}Point intersectionPoint;if (LineTools.LineIntersect(At(i, vertices), At(j, vertices), At(k, vertices), At(k + 1, vertices), out intersectionPoint)){return false;}}return true;}}public static class MathUtils{public static bool IsValid(double x){return double.IsNaN(x) ? false : !double.IsInfinity(x);}public static void Cross(ref Point a, ref Point b, out float c){c = (float)(a.X * b.Y - a.Y * b.X);}public static float Area(ref Point a, ref Point b, ref Point c){return (float)(a.X * (b.Y - c.Y) + b.X * (c.Y - a.Y) + c.X * (a.Y - b.Y));}public static bool Collinear(ref Point a, ref Point b, ref Point c, float tolerance){return FloatInRange(Area(ref a, ref b, ref c), -tolerance, tolerance);}public static bool FloatEquals(float value1, float value2){return Math.Abs(value1 - value2) <= Settings.Epsilon;}public static bool FloatInRange(float value, float min, float max){return (value >= min && value <= max);}}public static class SimplifyTools{public static Vertices CollinearSimplify(Vertices vertices, float collinearityTolerance){if (vertices.Count < 3)return vertices;Vertices simplified = new Vertices();for (int i = 0; i < vertices.Count; i++){int prevId = vertices.PreviousIndex(i);int nextId = vertices.NextIndex(i);Point prev = vertices[prevId];Point current = vertices[i];Point next = vertices[nextId];if (MathUtils.Collinear(ref prev, ref current, ref next, collinearityTolerance))continue;simplified.Add(current);}return simplified;}}public static class LineTools{public static Point LineIntersect(Point p1, Point p2, Point q1, Point q2){Point i = new Point(0, 0);float a1 = (float)(p2.Y - p1.Y);float b1 = (float)(p1.X - p2.X);float c1 = (float)(a1 * p1.X + b1 * p1.Y);float a2 = (float)(q2.Y - q1.Y);float b2 = (float)(q1.X - q2.X);float c2 = (float)(a2 * q1.X + b2 * q1.Y);float det = a1 * b2 - a2 * b1;if (!MathUtils.FloatEquals(det, 0)){i.X = (b2 * c1 - b1 * c2) / det;i.Y = (a1 * c2 - a2 * c1) / det;}return i;}public static bool LineIntersect(ref Point point1, ref Point point2, ref Point point3, ref Point point4, bool firstIsSegment, bool secondIsSegment, out Point point){point = new Point();float a = (float)(point4.Y - point3.Y);float b = (float)(point2.X - point1.X);float c = (float)(point4.X - point3.X);float d = (float)(point2.Y - point1.Y);float denom = (a * b) - (c * d);if (!(denom >= -Settings.Epsilon && denom <= Settings.Epsilon)){float e = (float)(point1.Y - point3.Y);float f = (float)(point1.X - point3.X);float oneOverDenom = 1.0f / denom;float ua = (c * e) - (a * f);ua *= oneOverDenom;if (!firstIsSegment || ua >= 0.0f && ua <= 1.0f){float ub = (b * e) - (d * f);ub *= oneOverDenom;if (!secondIsSegment || ub >= 0.0f && ub <= 1.0f){if (ua != 0f || ub != 0f){point.X = point1.X + ua * b;point.Y = point1.Y + ua * d;return true;}}}}return false;}public static bool LineIntersect(Point point1, Point point2, Point point3, Point point4, out Point intersectionPoint){return LineIntersect(ref point1, ref point2, ref point3, ref point4, true, true, out intersectionPoint);}}
}
View Code

 

转载于:https://www.cnblogs.com/jiailiuyan/p/3392275.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/259242.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机教学的弊端,信息技术在教学中的利弊及解决对策

摘 要&#xff1a;信息技术教育已成为国家信息化事业的重要组成部分&#xff0c;是当今素质教育的重要内容之一。从阐述信息技术教育的内涵和发展阶段出发&#xff0c;分析了当前信息技术在教学应用中的优势和存在的问题&#xff0c;并提出了相应的解决对策。关键词&#xff1a…

【转】Linux命令之查看文件占用空间大小-du,df

原文网址&#xff1a;http://blog.csdn.net/wangjunjun2008/article/details/19840671 du(disk usage),顾名思义,查看目录/文件占用空间大小#查看当前目录下的所有目录以及子目录的大小$ du -h $ du -ah #-h:用K、M、G的人性化形式显示 #-a:显示目录和文件 du -h tmp du -ah tm…

一个FORK的面试题

为什么80%的码农都做不了架构师&#xff1f;>>> #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void) { int i; for(i0; i<2; i){ fork(); printf("-"); } wait(NULL); wait(NULL); return 0; }/c 如果…

C++11系列学习之二-----lambda表达式

C11添加了一项名为lambda表达式的新功能&#xff0c;通过这项功能可以编写内嵌的匿名函数&#xff0c;而不必编写独立函数和函数对象&#xff0c;使得代码更容易理解。lambda表达式的语法如下所示&#xff1a;[capture_block](parameters) exceptions_specification -> retu…

php四种基础算法:冒泡,选择,插入和快速排序法

许多人都说 算法是程序的核心&#xff0c;一个程序的好于差,关键是这个程序算法的优劣。作为一个初级phper&#xff0c;虽然很少接触到算法方面的东西 。但是对于冒泡排序&#xff0c;插入排序&#xff0c;选择排序&#xff0c;快速排序四种基本算法&#xff0c;我想还是要掌握…

GCPC2014 C Bounty Hunter

题意&#xff1a;给你一个平面上的点集&#xff08;x值各不相等&#xff09;&#xff0c;问你从最左边走到最右边&#xff08;只能以x递增的顺序&#xff09;&#xff0c;再从最右边回到最左边&#xff08;以x递减的顺序&#xff09;问你最短距离是多少。 解题思路&#xff1a;…

计算机启动时运行ccleaner,Ccleaner的使用方法

ccleaner是一款非常好用的系统优化工具&#xff0c;它可以提升电脑速度&#xff0c;可以对上网历史记录、临时文件夹、回收站垃圾清理、注册表进行垃圾项扫描和清理、软件卸载等功能&#xff0c;保护用户的个人浏览隐私&#xff0c;为Windows系统腾出更多硬盘空间。下面小编就为…

PLSQL Developer软件使用大全

PLSQL Developer软件使用大全 第一章 PLSQL Developer特性 PL/SQL Developer是一个集成开发环境&#xff0c;专门面向Oracle数据库存储程序单元的开发。如今&#xff0c;有越来越多的商业逻辑和应用逻辑转向了Oracle Server&#xff0c;因此&#xff0c;PL/SQL编程也成了整个开…

C++11系列学习之三----array/valarray

创建数组&#xff0c;是程序设计中必不可少的一环。我们一般可以有以下几种方法来创建数组。 一、C内置数组 数组大小固定&#xff0c;速度较快 通用格式是&#xff1a;数据类型 数组名[ 数组大小 ]; 如 int a[40];//一维数组 int a[5][10];//二维数组 二、vector创建数组 包…

实验7综合练习

一、填空&#xff1a;阅读下列程序说明和程序&#xff0c;在可选答案中&#xff0c;挑选一个正确答案。填补(1) (2) (3) (4)处空白&#xff0c;并注释说明为什么。 程序说明 求 1 2/3 3/5 4/7 5/9 … 的前15项之和。 运行示例&#xff1a; sum 8.667936 程序如下&#x…

计算机专业课的教学准备,计算机专业课程教学中的分层教学模式

《计算机专业课程教学中的分层教学模式》由会员分享&#xff0c;可在线阅读&#xff0c;更多相关《计算机专业课程教学中的分层教学模式(5页珍藏版)》请在人人文库网上搜索。1、编号&#xff1a;XXXX时间&#xff1a;2021年x月x日Error! No text of specified style in documen…

angular-过滤器

过滤器描述currency格式化数字为货币格式。filter从数组项中选择一个子集。lowercase格式化字符串为小写。orderBy根据某个表达式排列数组。uppercase格式化字符串为大写。内容中&#xff1a;数值转为货币格式 <p>总价 {{ (quantity * price) | currency }}</p> 排…

SSH三大框架的工作原理及流程

Hibernate工作原理及为什么要用? 原理&#xff1a; 1.通过Configuration().configure();读取并解析hibernate.cfg.xml配置文件 2.由hibernate.cfg.xml中的<mapping resource"com/xx/User.hbm.xml"/>读取并解析映射信息 3.通过config.buildSessionFactory();/…

二分查找法(递归与循环实现)

问题&#xff1a; 给定一个排序数组和一个数k&#xff0c;要求找到第一个k的位置和最后一个k的位置 解析&#xff1a; 由于给定的数组是从小到大排序的&#xff0c;故可以按照二分查找法来找&#xff0c;下面分别从递归和循环两种方法来阐述&#xff1a; //递归方法 int GetF…

电脑显示器变色_电脑维修(看完后就可以开一家自己的电脑维修店!)

第二部分 常见故障判断本部分将计算机从开机一直到关机期间的故障进行分类。每一类的判断、定位过程都是第一部分中维修判断一节的有机组成部分&#xff0c;即不论使用什么方法或不论去判断什么内容&#xff0c;这两部分总是相互结合使用的。以下各故障类型中所列的故障现象只是…

linux运维基础篇 unit7

unit 71.进程定义进程就是cpu未完成的工作2.ps命令psa ##关于当前环境的所有进程x ##与当前环境无关的所有进程f ##显示进程从属关系e ##显示进程调用环境工具的详细信息l ##长列表显示进程的详细信息u ##显…

运行快捷指令无法连接服务器失败,快捷指令打不开怎么回事?iPhone快捷指令无法载入的解决办法...

经常会有果粉朋友反馈&#xff0c;自己的 iPhone 快捷指令打不开。具体表现是&#xff0c;在 Safari 浏览器中&#xff0c;打开快捷指令下载安装页面&#xff0c;点击“获取捷径”后&#xff0c;一直卡在快捷指令中心正在载入页面&#xff0c;等半天都无法正常载入需要安装的快…

Bigpipe---FaceBook使用的页面加载技术

BigPipe&#xff08;FaceBook使用的页面加载技术&#xff09; 理论部分&#xff1a;用户输入域名发送请求到服务端&#xff0c;服务端组合出需要的业务数据返回给客户端&#xff0c;这一过程是现在网页请求最基本传统的方式了。 好处&#xff1a;只做了一次http请求&#xff0c…

maven搭建多模块项目和管理

在eclipse下构建maven项目&#xff0c;该项目由多个子模块组成。 1.创建一个父项目 NEW -->project-->maven-->maven Project&#xff0c;点击下一步&#xff0c;进入new maven Project的Select project name and location界面 &#xff0c;什么也不做&#xff0c;直接…

shsh验证服务器,教你从Cydia上取出SHSH并验证有效性!

原标题&#xff1a;教你从Cydia上取出SHSH并验证有效性&#xff01;今天在第一篇内容中和大家说了如何让32位设备进行降级&#xff0c;但这其中有个很重要的问题就是如何提取出对应设备的SHSH&#xff0c;虽然说本篇内容并不是对所有人都有效&#xff0c;但至少多了一个可选择的…