forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJarvisMarch.cs
58 lines (47 loc) · 2.24 KB
/
JarvisMarch.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
// submitted by Julian Schacher (jspp) with great help by gustorn
using System;
using System.Collections.Generic;
using System.Linq;
namespace JarvisMarch
{
public struct Vector
{
public readonly int x;
public readonly int y;
public Vector(int xValue, int yValue)
{
this.x = xValue;
this.y = yValue;
}
public override bool Equals(object obj) => obj is Vector v && this.x == v.x && this.y == v.y;
public override int GetHashCode() => (17 * 23 + this.x) * 23 + this.y;
public static bool operator==(Vector a, Vector b) => a.Equals(b);
public static bool operator!=(Vector a, Vector b) => !(a == b);
}
public class JarvisMarch
{
public List<Vector> Run(List<Vector> points)
{
var convexHull = new List<Vector>();
// Set the intial pointOnHull to the point of the list, where the x-position is the lowest.
var pointOnHull = points.Aggregate((leftmost, current) => leftmost.x < current.x ? leftmost : current);
// Continue searching for the next pointOnHull until the next pointOnHull is equal to the first point of the convex hull.
do
{
convexHull.Add(pointOnHull);
// Search for the next pointOnHull by looking which of the points is the next most outer point.
pointOnHull = points.Aggregate((potentialNextPointOnHull, current) =>
{
// Returns true, if potentialNextPointOnHull is equal to the current pointOnHull or if the current point is left of the line defined by pointOnHull and potentialNextPointOnHull.
if (potentialNextPointOnHull == pointOnHull || IsLeftOf(pointOnHull, potentialNextPointOnHull, current))
return current;
return potentialNextPointOnHull;
});
// Check if the gift wrap is completed.
} while (pointOnHull != convexHull[0]);
return convexHull;
}
// Returns true, if p is left of the line defined by a and b.
private bool IsLeftOf(Vector a, Vector b, Vector p) => (b.x - a.x) * (p.y - a.y) > (p.x - a.x) * (b.y - a.y);
}
}