今天公司要求实现一个算法,在给出一堆离散点中,画出最外层的边界。实际就是从离散点中找出最外面的点(或称边界点),这些点用线连接起来即可。
一、实现的原理:
1.任意点(point1)找出最远距离的一个点(point2),该点point2即为其中一个边界点。
2.point1与point2组成的向量v1(point1 - point2)中找出与其最大夹角的一个向量v2。
3.重复:v2中找与其最大夹角的向量v3.....
4.直到最后找到的向量方向为点point2的向量时结束。
二、这里实现的代码为C#,只要搞懂原理,可以转为各种语言的实现
1.首先是平面向量类 PlaneVector
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
///
<summary>
2 /// 平面向量类
3 /// </summary>
4 public struct PlaneVector
5 {
6 public static readonly PlaneVector Empty;
7
8 /// <summary>
9 /// 向量的X值
10 /// </summary>
11 private double _X;
12
13 /// <summary>
14 /// 向量的Y值
15 /// </summary>
16 private double _Y;
17
18 public double X
19 {
20 get
21 {
22 return _X;
23 }
24 set
25 {
26 _X = value;
27 }
28 }
29
30 public double Y
31 {
32 get
33 {
34 return _Y;
35 }
36 set
37 {
38 _Y = value;
39 }
40 }
41
42 public PlaneVector( double x, double y)
43 {
44 _X = x;
45 _Y = y;
46 }
47
48 public PlaneVector(Point destPoint, Point sourcePoint)
49 {
50 _X = destPoint.X - sourcePoint.X;
51 _Y = destPoint.Y - sourcePoint.Y;
52 }
53
54 public PlaneVector(PointF destPoint, PointF sourcePoint)
55 {
56 _X = destPoint.X - sourcePoint.X;
57 _Y = destPoint.Y - sourcePoint.Y;
58 }
59
60 public PlaneVector(PlaneVector destPlaneVector, PlaneVector sourcePlaneVector)
61 {
62 _X = destPlaneVector._X - sourcePlaneVector._X;
2 /// 平面向量类
3 /// </summary>
4 public struct PlaneVector
5 {
6 public static readonly PlaneVector Empty;
7
8 /// <summary>
9 /// 向量的X值
10 /// </summary>
11 private double _X;
12
13 /// <summary>
14 /// 向量的Y值
15 /// </summary>
16 private double _Y;
17
18 public double X
19 {
20 get
21 {
22 return _X;
23 }
24 set
25 {
26 _X = value;
27 }
28 }
29
30 public double Y
31 {
32 get
33 {
34 return _Y;
35 }
36 set
37 {
38 _Y = value;
39 }
40 }
41
42 public PlaneVector( double x, double y)
43 {
44 _X = x;
45 _Y = y;
46 }
47
48 public PlaneVector(Point destPoint, Point sourcePoint)
49 {
50 _X = destPoint.X - sourcePoint.X;
51 _Y = destPoint.Y - sourcePoint.Y;
52 }
53
54 public PlaneVector(PointF destPoint, PointF sourcePoint)
55 {
56 _X = destPoint.X - sourcePoint.X;
57 _Y = destPoint.Y - sourcePoint.Y;
58 }
59
60 public PlaneVector(PlaneVector destPlaneVector, PlaneVector sourcePlaneVector)
61 {
62 _X = destPlaneVector._X - sourcePlaneVector._X;