-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polygon.cs
360 lines (334 loc) · 11.8 KB
/
Polygon.cs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using System;
using System.Collections.Generic;
using System.Diagnostics.SymbolStore;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using cg_proj2.enums;
namespace cg_proj2
{
public class Polygon : IShape
{
public List<Point> Vertices { get; set; }
public Point InitialPoint { get; set; }
public Color ColorPoly { get; set; }
public int PolyThickness { get; set; }
public bool IsFilled { get; set; }
public bool IsClipping { get; set; }
public Color FillColor { get; set; }
protected static int selectedVertexIndex;
public Polygon(int x, int y, Color color, int _thickness)
{
InitialPoint = new Point(x, y);
this.ColorPoly = color;
PolyThickness = _thickness;
Vertices = new List<Point>();
Vertices.Add(InitialPoint);
}
public bool AddVertex(int x, int y)
{
if (InitialPoint == null)
{
return false;
}
int count = Vertices.Count;
if (count >= 3)
{
int Vx = Math.Abs((int)x - (int)InitialPoint.X);
int Vy = Math.Abs((int)y - (int)InitialPoint.Y);
double len = Math.Sqrt(Math.Pow(Vy, 2) + Math.Pow(Vx, 2));
if (len <= 10)
{
Vertices.Add(InitialPoint);
MainWindow.DrawLine((int)Vertices[count - 1].X, (int)Vertices[count - 1].Y, (int)InitialPoint.X, (int)InitialPoint.Y, PolyThickness, ColorPoly);
return true;
}
}
Vertices.Add(new Point(x, y));
MainWindow.DrawLine((int)Vertices[count - 1].X, (int)Vertices[count - 1].Y, x, y, PolyThickness, ColorPoly);
return false;
}
public void DeleteShape()
{
if (Vertices.Where(x => Vertices.Contains(x)).Count() >= 2)
{
for (int i = 1; i < Vertices.Count; i++)
{
MainWindow.DrawLine((int)Vertices[i - 1].X, (int)Vertices[i - 1].Y, (int)Vertices[i].X, (int)Vertices[i].Y, PolyThickness, ColorPoly, true);
}
}
if (IsFilled)
{
EdgeTableFill(FillColor, true);
}
}
public void DrawShape()
{
if (Vertices.Where(x => Vertices.Contains(x)).Count() >= 2)
{
//MessageBox.Show(Vertices.Count.ToString());
for (int i = 1; i < Vertices.Count; i++)
{
MainWindow.DrawLine((int)Vertices[i - 1].X, (int)Vertices[i - 1].Y, (int)Vertices[i].X, (int)Vertices[i].Y, PolyThickness, ColorPoly);
}
}
if (IsFilled)
{
EdgeTableFill(FillColor);
}
}
public bool WasClicked(int x, int y)
{
int Vx, Vy;
double len;
foreach (Point pt in Vertices)
{
Vx = Math.Abs((int)x - (int)pt.X);
Vy = Math.Abs((int)y - (int)pt.Y);
len = Math.Sqrt(Math.Pow(Vy, 2) + Math.Pow(Vx, 2));
//MessageBox.Show(len.ToString());
//MessageBox.Show(Vertices.Count.ToString());
if (len < 10)
{
selectedVertexIndex = Vertices.IndexOf(pt);
return true;
}
}
return false;
}
public virtual void MovePolygon(int x, int y, PolyMoveModes mode)
{
if (mode == PolyMoveModes.ByVertex)
{
DeleteShape();
if (Vertices[selectedVertexIndex] == InitialPoint)
{
InitialPoint = new Point(x, y);
Vertices[0] = Vertices[Vertices.Count - 1] = InitialPoint;
}
else
{
Vertices[selectedVertexIndex] = new Point(x, y);
}
DrawShape();
}
else if (mode == PolyMoveModes.WholePoly)
{
int Vx, Vy;
List<Point> tmp = new List<Point>();
Vx = ((int)x - (int)Vertices[selectedVertexIndex].X);
Vy = ((int)y - (int)Vertices[selectedVertexIndex].Y);
foreach (Point pt in Vertices.ToList())
{
if (pt.X + Vx > MainWindow.actualWidth || pt.X + Vx < 0 ||
pt.Y + Vy > MainWindow.actualHeight || pt.Y + Vy < 0)
{
MessageBox.Show("Poly would be out of bounds");
return;
}
else
{
tmp.Add(new Point(pt.X + Vx, pt.Y + Vy));
}
}
DeleteShape();
Vertices = tmp;
InitialPoint = tmp[0];
DrawShape();
}
}
public void ReColour(Color color)
{
if (color == ColorPoly)
{
return;
}
DeleteShape();
ColorPoly = color;
DrawShape();
}
public void Resize(int _thickness)
{
DeleteShape();
PolyThickness = _thickness;
DrawShape();
}
private double CrossProduct(double Ax, double Ay, double Bx, double By, double Cx, double Cy)
{
double BAx = Ax - Bx;
double BAy = Ay - By;
double BCx = Cx - Bx;
double BCy = Cy - By;
return (BAx * BCy - BAy * BCx);
}
public bool CheckIfConvex()
{
bool gotNegative = false;
bool gotPositive = false;
int n = Vertices.Count;
int B, C;
double crossProduct;
for (int A = 0; A < n; A++)
{
B = (A + 1) % n;
C = (B + 1) % n;
crossProduct = CrossProduct(Vertices[A].X, Vertices[A].Y, Vertices[B].X, Vertices[B].Y, Vertices[C].X, Vertices[C].Y);
if (crossProduct < 0)
{
gotNegative = true;
}
else if (crossProduct > 0)
{
gotPositive = true;
}
if (gotNegative && gotPositive) return false;
}
return true;
}
private double PolygonArea()
{
List<Point> tmp = new List<Point>();
for (int i = 0; i < Vertices.Count - 1; i++)
{
tmp.Add(Vertices[i]);
}
int n = Vertices.Count - 1;
Point[] points = new Point[n + 1];
tmp.CopyTo(points, 0);
points[n] = Vertices[0];
double area = 0;
for (int i = 0; i < n; i++)
{
area += (points[i + 1].X - points[i].X) * (points[i + 1].Y + points[i].Y) / 2;
}
return area;
}
private Point FindCentroid()
{
List<Point> tmp = new List<Point>();
for (int i = 0; i < Vertices.Count - 1; i++)
{
tmp.Add(Vertices[i]);
}
int n = tmp.Count;
Point[] points = new Point[n + 1];
tmp.CopyTo(points, 0);
points[n] = tmp[0];
double X = 0;
double Y = 0;
double tmpDbl;
for (int i = 0; i < n; i++)
{
tmpDbl = points[i].X * points[i + 1].Y - points[i + 1].X * points[i].Y;
X = X + (points[i].X + points[i + 1].X) * tmpDbl;
Y = Y + (points[i].Y + points[i + 1].Y) * tmpDbl;
}
double polygon_area = PolygonArea();
X = X / (6 * polygon_area);
Y = Y / (6 * polygon_area);
if (X < 0)
{
X = -X;
Y = -Y;
}
return new Point(X, Y);
}
public void SortClockwise()
{
if (this is Rectangle)
{
return;
}
Point center = FindCentroid();
List<Point> tmp = new List<Point>();
for (int i = 0; i < Vertices.Count - 1; i++)
{
tmp.Add(Vertices[i]);
}
Vertices = new List<Point>();
tmp.OrderBy(x => Math.Atan2(x.X - center.X, x.Y - center.Y)).ToList();
Vertices = tmp;
Vertices.Add(Vertices[0]);
}
internal class EdgeBucket
{
internal int YMax { get; set; }
internal double XOfYMin { get; set; }
internal double M { get; set; }
internal EdgeBucket(Point p0, Point p1)
{
YMax = (int)p1.Y;
XOfYMin = p0.X;
M = (p1.X - p0.X) / (p1.Y - p0.Y);
}
}
internal class EdgeTable
{
internal int NumOfBuckets;
internal List<EdgeBucket> Buckets { get; set; }
internal EdgeTable()
{
Buckets = new List<EdgeBucket>();
NumOfBuckets = Buckets.Count;
}
internal void RemoveElems(int val)
{
foreach (EdgeBucket edgeBucket in Buckets.ToList())
{
if(edgeBucket.YMax == val)
{
Buckets.Remove(edgeBucket);
}
}
}
internal void Increment()
{
foreach(EdgeBucket edge in Buckets.ToList())
{
edge.XOfYMin = edge.XOfYMin + edge.M;
}
}
}
public void EdgeTableFill(Color? color, bool del = false)
{
EdgeTable activeEdgeTable = new EdgeTable();
List<Point> vertices = new List<Point>();
for (int i = 0; i < Vertices.Count - 1; i++)
{
vertices.Add(Vertices[i]);
}
List<int> indexList = Enumerable.Range(0, vertices.Count).OrderBy(x => vertices[x].Y).ToList();
int tmpIndex = 0;
int beg = indexList[0];
int yMin = (int)vertices.Min(x => x.Y), yMax = (int)vertices.Max(x => x.Y);
while (yMin < yMax)
{
if (vertices[beg].Y == yMin)
{
if (vertices[(beg + 1) % vertices.Count].Y > vertices[beg].Y)
{
activeEdgeTable.Buckets.Add(new EdgeBucket(vertices[beg], vertices[(beg + 1 + vertices.Count) % vertices.Count]));
}
if (vertices[(beg - 1 + vertices.Count) % vertices.Count].Y > vertices[beg].Y)
{
activeEdgeTable.Buckets.Add(new EdgeBucket(vertices[beg], vertices[(beg - 1 + vertices.Count) % vertices.Count]));
}
beg = indexList[++tmpIndex];
}
activeEdgeTable.Buckets = activeEdgeTable.Buckets.OrderBy(x => x.XOfYMin).ToList();
for (int i = 0; i < activeEdgeTable.Buckets.Count - 1; i++)
{
EdgeBucket start = activeEdgeTable.Buckets[i], end = activeEdgeTable.Buckets[i + 1];
MainWindow.DrawLine((int)start.XOfYMin, yMin, (int)end.XOfYMin, yMin, 1, color == null ? ColorPoly : (Color)color, del);
}
yMin++;
activeEdgeTable.RemoveElems(yMin);
activeEdgeTable.Increment();
}
IsFilled = true;
FillColor = (Color)color;
}
}
}