Repository navigation
Expand file tree
/
Copy pathAlgorithmBase.cs
More file actions
123 lines (103 loc) · 3.18 KB
/
Copy pathAlgorithmBase.cs
File metadata and controls
123 lines (103 loc) · 3.18 KB
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ConvexHullRectangleApp
{
public abstract class AlgorithmBase
{
protected PointF[] points;
protected AlgorithmBase(PointF[] points)
{
this.points = points;
}
public abstract PointF[] Run();
public abstract IEnumerable<PointF[]> RunYield();
public double Perimeter
{
get
{
double d = 0;
for (int i = 0; i < points.Length - 1; i++)
d += Distance(points[i], points[i + 1]);
return d;
}
}
public double Area
{
get
{
double a = 0;
for (int i = 0; i < points.Length - 1; i++)
a += Math.Abs(points[i].X * points[i + 1].Y - points[i + 1].X * points[i].Y);
return a / 2;
}
}
public static double Distance(PointF p1, PointF p2)
{
double dx = p2.X - p1.X;
double dy = p2.Y - p1.Y;
return Math.Sqrt(dx * dx + dy * dy);
}
protected int GetPointLocation(PointF p, PointF l1, PointF l2)
{
l2.X -= l1.X;
l2.Y -= l1.Y;
p.X -= l1.X;
p.Y -= l1.Y;
float cross = Cross(p, l2);
if (cross < 0) return -1;
if (cross > 0) return 1;
return 0;
}
protected PointF GetLeftMost(PointF[] pts)
{
float minX = float.MaxValue;
int leftMost = -1;
for (int i = 0; i < pts.Length; i++)
{
if (pts[i].X < minX)
{
minX = pts[i].X;
leftMost = i;
}
}
return pts[leftMost];
}
protected PointF GetRightMost(PointF[] pts)
{
float maxX = float.MinValue;
int rightMost = -1;
for (int i = 0; i < pts.Length; i++)
{
if (pts[i].X > maxX)
{
maxX = pts[i].X;
rightMost = i;
}
}
return pts[rightMost];
}
public static float Cross(PointF p1, PointF p2)
{
return p2.X * p1.Y - p2.Y * p1.X;
}
public static float Cross(PointF p1, PointF p2, PointF p3)
{
return (p1.X - p3.X) * (p2.Y - p3.Y) - (p1.Y - p3.Y) * (p2.X - p3.X);
}
public static List<(string Name, AlgorithmBase Algorithm)> GetAlgorithms(PointF[] points)
{
List<(string, AlgorithmBase)> algos = new List<(string, AlgorithmBase)>();
Type type = typeof(AlgorithmBase);
Assembly asm = Assembly.GetAssembly(type);
foreach (Type t in asm.GetExportedTypes())
if (t.BaseType == type)
algos.Add((t.Name.Split('.').Last(), (AlgorithmBase)Activator.CreateInstance(t, points)));
return algos;
}
}
}