-
Notifications
You must be signed in to change notification settings - Fork 2
/
Polygon.cs
45 lines (40 loc) · 1.02 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
using System;
using System.Collections.Generic;
public class Polygon
{
private List<Point> vertices;
public Polygon(List<Point> vertices)
{
this.vertices = vertices;
}
public double Area()
{
return Math.Abs(SignedDoubleArea() * 0.5);
}
public Winding GetWinding()
{
double signedDoubleArea = SignedDoubleArea();
if (signedDoubleArea < 0)
return Winding.CLOCKWISE;
if (signedDoubleArea > 0)
return Winding.COUNTERCLOCKWISE;
return Winding.NONE;
}
private double SignedDoubleArea()
{
int index;
int nextIndex;
int n = vertices.Count;
Point p;
Point next;
double signedDoubleArea = 0;
for (index = 0; index < n; ++index)
{
nextIndex = (index + 1) % n;
p = vertices[index];
next = vertices[nextIndex];
signedDoubleArea += ((p.x * next.y) - (next.x * p.y));
}
return signedDoubleArea;
}
}