[CSharpTips]C# 判断多边形边界曲线顺/逆时针

C# 判断多边形边界曲线顺/逆时针

一个List<Point>表示的不规则多边形判断是顺时针还是逆时针

自己想到的方法是选择三个连续的点ABC利用向量积,AB*AC小于零顺时针,大于零逆时针,不过要先排除三个点在一条直线上且中间点为凹点的情况

后来找到个大佬用Green公式判断,简单高效,感叹数学的神奇

代码贴在下面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
 
namespace ClockDirectionCheck
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Point> points = new List<Point>();
            points.Add(new Point(0, 0));
            points.Add(new Point(0, 100));
            points.Add(new Point(100, 100));
            points.Add(new Point(50, 90));
            points.Add(new Point(100, 75));
            points.Add(new Point(50, 60));
            points.Add(new Point(100, 45));
            points.Add(new Point(50, 30));
            points.Add(new Point(100, 0));
 
            Console.WriteLine("================顺序==================");
            Console.WriteLine("==SelfFunction==");
            if (GetClockDirection(points))
            {
                Console.WriteLine("顺时针");
            }
            else
            {
                Console.WriteLine("逆时针");
            }
            Console.WriteLine("=====Green=====");
            if (Green(points))
            {
                Console.WriteLine("顺时针");
            }
            else
            {
                Console.WriteLine("逆时针");
            }
            Console.WriteLine("================逆序==================");
            points.Reverse();
            Console.WriteLine("==SelfFunctio===");
            if (GetClockDirection(points))
            {
                Console.WriteLine("顺时针");
            }
            else
            {
                Console.WriteLine("逆时针");
            }
            Console.WriteLine("=====Green=====");
            if (Green(points))
            {
                Console.WriteLine("顺时针");
            }
            else
            {
                Console.WriteLine("逆时针");
            }
            Console.WriteLine("================End==================");
            Console.ReadLine();
        }
 
        /// <summary>
        /// 排除不在一条直线上,排除凹点后用向量积判断
        /// </summary>
        /// <param name="points"></param>
        /// <returns></returns>
        private static bool GetClockDirection(List<Point> points)
        {
            bool result = false; ;
            for (int i = 0; i < points.Count() - 2; i++)
            {
                //判断是否在一条直线上
                if (points[i].X == points[i + 1].X && points[i + 1].X == points[i + 2].X) continue;
                if (points[i].Y == points[i + 1].Y && points[i + 1].Y == points[i + 2].Y) continue;
 
                //检测中间点是否是凹点
                var newPoints = new List<Point>(points);
                newPoints.Remove(points[i + 1]);
                Geometry geometry = Geometry.Parse(GetPathByPoinits(newPoints));
                if (geometry.FillContains(new System.Windows.Point() { X = points[i + 1].X, Y = points[i + 1].Y }))
                {
                    continue;
                }
                //求向量积AC*AB 小于零顺时针 大于零逆时针
                if (VectorCross(points[i], points[i + 1], points[i + 2]) < 0)
                {
                    result = true;
                    break;
                }
                else
                {
                    result = false;
                    break;
                }
            }
            return result;
        }
 
        //获取Path路径
        private static string GetPathByPoinits(List<Point> points)
        {
            if (points == null || points.Count() == 0)
            {
                return "";
            }
            string result = "M";
            foreach (var item in points)
            {
                result += item.X + "," + item.Y + " ";
            }
            result += "Z";
            return result;
        }
        public struct Point
        {
            public double X;
            public double Y;
 
            public Point(double x, double y)
            {
                X = x;
                Y = y;
            }
        }
        private static double VectorCross(Point p1, Point p2, Point p3)
        {
            Vector vectorP1 = new Vector(p1.X, p1.Y);
            Vector vectorP2 = new Vector(p2.X, p2.Y);
            Vector vectorP3 = new Vector(p3.X, p3.Y);
            Vector vectorP1P2 = Vector.Subtract(vectorP2, vectorP1);
            Vector vectorP1P3 = Vector.Subtract(vectorP3, vectorP1);
            return Vector.CrossProduct(vectorP1P2, vectorP1P3);
        }
        //格林公式
        private static bool Green(List<Point> points)
        {
            double d = 0;
            for (int i = 0; i < points.Count - 1; i++)
            {
                d += -0.5 * (points[i + 1].Y + points[i].Y) * (points[i + 1].X - points[i].X);
            }
            if (d > 0)
                return false;
            else
                return true;
        }
    }
}

  参考:https://blog.csdn.net/henuyh/article/details/80378818

posted @   xiaoshuye  阅读(447)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示