拆解凹多边形

偶遇需要拆解凹多边形

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,一经查实,立即删除!

相关文章

一个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…

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编程也成了整个开…

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

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

linux运维基础篇 unit7

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

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

经常会有果粉朋友反馈&#xff0c;自己的 iPhone 快捷指令打不开。具体表现是&#xff0c;在 Safari 浏览器中&#xff0c;打开快捷指令下载安装页面&#xff0c;点击“获取捷径”后&#xff0c;一直卡在快捷指令中心正在载入页面&#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;但至少多了一个可选择的…

n!后面有多少个0(转载)

我的思路&#xff1a; 从”那些数相乘可以得到10”这个角度&#xff0c;问题就变得比较的简单了。 首先考虑&#xff0c;如果N的阶乘为K和10的M次方的乘积&#xff08;N&#xff01;K∗10M&#xff09;&#xff0c;那么N!末尾就有M个0。 如果将N的阶乘分解后&#xff0c;那么…

ico的尺寸_批量压缩、加水印、调整尺寸……用这 6 款 Mac 图片工具一键解决

不论是写文章、做教程&#xff0c;还是处理摄影作品、上传社交平台&#xff0c;对图片的处理肯定少不了。庞大又贵重的专业软件不仅成本较高&#xff0c;还有着不小的上手难度。如果我们仅仅是想要&#xff1a;缩小图片体积、添加水印或者批量对图片进行操作等等&#xff0c;使…

转:MAC 下安装PHONEGAP开发环境

MAC 下安装PHONEGAP开发环境 什么是Phonegap呢&#xff1f;Phonegap是一个利用HTML5去开发App的框架。可以为安卓、iOS、WP、黑莓、火狐等移动操作系统。采用HTML5来编写交互界面。其优点是编写一次可以编译到各种移动平台上&#xff0c;大大为公司节省了开发周期。但是它也是有…

从零开始学android编程_android初学者的入门秘籍

大概是去年年底开始接触android原本是学习嵌入式的我&#xff0c;领导让我看看能不能搞一下这个android APP。一开始的我懵逼得很。。。这android APP 不是得用java写吗&#xff1f;&#xff1f;&#xff1f; 现在我看网上说比较多还是用kotlin&#xff0c;没去学。。。好家伙&…

web中的cookie管理

本篇是以JSP为背景介绍&#xff0c;但是在web开发中也是相同的原理。 什么是cookie 由于http是一种无状态的协议&#xff0c;因此服务器收到请求后&#xff0c;只会当做一次新的请求。即便你重复发送了1000次同样的请求&#xff0c;这1000次都属于独立的请求。 这样显然效率很低…

unity怎么设置游戏页面_杭州有没有正规的unity游戏开发培训机构?

现在Unity游戏开发是个火热的行业&#xff0c;薪资待遇比较高&#xff0c;未来的发展方向和前景也比较不错&#xff0c;很多人也都想成为专业Unity游戏开发工程师&#xff0c;学习Unity游戏开发已经成为很多追求更好就业前景的人的选择。学习专业、系统的Unity游戏开发知识并达…

VC++ 使用attributes定义接口

1.定义预处理命令_ATL_ATTRIBUTES 2.在一个全局的Cpp文件里面配置module的attribute [module(dll, uuid "{3845951F-15B8-4286-8E7D-E9D4F5C7B6CE}", name "TestApp")]3.定义接口 [object,uuid("9F414A8A-1D5E-4aff-A60E-CFD65155ABB6"),dual,…

Knockout.Js案例一Introduction

在这第一个教程中,您将体验的一些基本知识构建的web UI Model-View-ViewModel使用knockout.js(MVVM)模式。案例1&#xff1a;添加:data-bind <p>First name: <strong data-bind"text:firstName">1</strong></p><p>Last name: <stro…

谷歌浏览器外贸版_做外贸快两个月,没有单怎么办?

Hello 大家好&#xff0c;我是Jack。今天给大家更新一篇在知乎看到的外贸问题&#xff1a;做外贸快两个月&#xff0c;没有单怎么办?外贸这个话题在知乎算是小众话题了&#xff0c;相比较于职场&#xff0c;英语学习&#xff0c;国际政治&#xff0c;IT等&#xff0c;这些话题…

React Native通信机制详解

http://blog.cnbang.net/tech/2698/ React Native是facebook刚开源的框架&#xff0c;可以用javascript直接开发原生APP&#xff0c;先不说这个框架后续是否能得到大众认可&#xff0c;单从源码来说&#xff0c;这个框架源码里有非常多的设计思想和实现方式值得学习&#xff0c…