forked from jfg8/csDelaunay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSiteList.cs
122 lines (105 loc) · 2.6 KB
/
SiteList.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
using csDelaunay.Geometries;
namespace csDelaunay.Delaunay;
public class SiteList
{
private readonly List<Site> sites = new();
private int currentIndex;
private bool sorted = false;
public void Dispose()
{
sites.Clear();
}
public int Add(Site site)
{
sorted = false;
sites.Add(site);
return sites.Count;
}
public int Count()
{
return sites.Count;
}
public Site? Next()
{
if (!sorted)
{
throw new Exception("SiteList.Next(): sites have not been sorted");
}
if (currentIndex < sites.Count)
{
return sites[currentIndex++];
}
else
{
return null;
}
}
public RectangleF GetSitesBounds()
{
if (!sorted)
{
SortList();
ResetListIndex();
}
float xmin, xmax, ymin, ymax;
if (sites.Count == 0)
{
return RectangleF.Empty;
}
xmin = float.MaxValue;
xmax = float.MinValue;
foreach (var site in sites)
{
if (site.x < xmin) xmin = site.x;
if (site.x > xmax) xmax = site.x;
}
// here's where we assume that the sites have been sorted on y:
ymin = sites[0].y;
ymax = sites[sites.Count - 1].y;
return new RectangleF(xmin, ymin, xmax - xmin, ymax - ymin);
}
public List<Vector2> SiteCoords()
{
var coords = new List<Vector2>();
foreach (var site in sites)
{
coords.Add(site.Coord);
}
return coords;
}
/*
*
* @return the largest circle centered at each site that fits in its region;
* if the region is infinite, return a circle of radius 0.
*/
public List<Circle> Circles()
{
var circles = new List<Circle>();
foreach (var site in sites)
{
float radius = 0;
var nearestEdge = site.NearestEdge();
if (!nearestEdge.IsPartOfConvexHull()) radius = nearestEdge.SitesDistance() * 0.5f;
circles.Add(new Circle(site.x, site.y, radius));
}
return circles;
}
public List<List<Vector2>> Regions(RectangleF plotBounds)
{
var regions = new List<List<Vector2>>();
foreach (var site in sites)
{
regions.Add(site.Region(plotBounds));
}
return regions;
}
public void ResetListIndex()
{
currentIndex = 0;
}
public void SortList()
{
Site.SortSites(sites);
sorted = true;
}
}