-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathExample2.cs
More file actions
83 lines (64 loc) · 2.28 KB
/
Example2.cs
File metadata and controls
83 lines (64 loc) · 2.28 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
namespace TriangleNet.Examples
{
using System.Collections.Generic;
using TriangleNet.Geometry;
using TriangleNet.Meshing;
using TriangleNet.Rendering.Text;
/// <summary>
/// Simple point set triangulation with convex hull.
/// </summary>
public class Example2 : IExample
{
public bool Run(bool print = false)
{
const int N = 50;
// Generate point set.
var points = Generate.RandomPoints(N, new Rectangle(0, 0, 100, 100));
// We use a polygon as input to enable segment insertion on the convex hull.
var poly = new Polygon(N);
poly.Points.AddRange(points);
// Set the 'convex' option to enclose the convex hull with segments.
var options = new ConstraintOptions() { Convex = true };
// Generate mesh.
var mesh = poly.Triangulate(options);
if (print) SvgImage.Save(mesh, "example-2.svg", 500);
return CheckConvexHull(mesh.Segments);
}
private static bool CheckConvexHull(IEnumerable<ISegment> segments)
{
int first = -1, prev = -1;
Point a = null, b, c;
var p = RobustPredicates.Default;
foreach (var s in segments)
{
// If first loop ...
if (first < 0)
{
// initialize vertex ids of first segment and ...
first = s.P1;
prev = s.P0;
// initialize first segment endpoint.
a = s.GetVertex(1);
continue;
}
// Check whether segments are returned in consecutive order.
if (prev != s.P1)
{
return false;
}
b = s.GetVertex(1);
c = s.GetVertex(0);
// Check whether the convex hull is traversed in counterclockwise.
if (p.CounterClockwise(a, b, c) < 0)
{
return false;
}
prev = s.P0;
a = b;
}
// Check whether the last segment connects to the first.
return prev == first;
}
}
}