拆解凹多邊形

偶遇需要拆解凹多邊形

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.pswp.cn/news/259242.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/259242.shtml
英文地址,請注明出處:http://en.pswp.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;但至少多了一個可選擇的…