1
+ package com.nodename.Delaunay
2
+ {
3
+ import com.nodename.geom.Circle ;
4
+ import com.nodename.utils.IDisposable ;
5
+
6
+ import flash.display.BitmapData ;
7
+ import flash.geom.Point ;
8
+ import flash.geom.Rectangle ;
9
+
10
+ internal final class SiteList implements IDisposable
11
+ {
12
+ private var _sites : Vector .< Site>;
13
+ private var _currentIndex : uint ;
14
+
15
+ private var _sorted : Boolean ;
16
+
17
+ public function SiteList ()
18
+ {
19
+ _sites = new Vector .< Site> ();
20
+ _sorted = false ;
21
+ }
22
+
23
+ public function dispose ():void
24
+ {
25
+ if (_sites )
26
+ {
27
+ for each (var site: Site in _sites )
28
+ {
29
+ site. dispose ();
30
+ }
31
+ _sites . length = 0 ;
32
+ _sites = null ;
33
+ }
34
+ }
35
+
36
+ public function push (site :Site ):uint
37
+ {
38
+ _sorted = false ;
39
+ return _sites . push (site);
40
+ }
41
+
42
+ public function get length ():uint
43
+ {
44
+ return _sites . length ;
45
+ }
46
+
47
+ public function next ():Site
48
+ {
49
+ if (_sorted == false )
50
+ {
51
+ throw new Error ("SiteList::next(): sites have not been sorted" );
52
+ }
53
+ if (_currentIndex < _sites . length )
54
+ {
55
+ return _sites [ _currentIndex ++];
56
+ }
57
+ else
58
+ {
59
+ return null ;
60
+ }
61
+ }
62
+
63
+ internal function getSitesBounds ():Rectangle
64
+ {
65
+ if (_sorted == false )
66
+ {
67
+ Site. sortSites(_sites );
68
+ _currentIndex = 0 ;
69
+ _sorted = true ;
70
+ }
71
+ var xmin: Number , xmax: Number , ymin: Number , ymax: Number ;
72
+ if (_sites . length == 0 )
73
+ {
74
+ return new Rectangle (0 , 0 , 0 , 0 );
75
+ }
76
+ xmin = Number . MAX_VALUE ;
77
+ xmax = Number . MIN_VALUE ;
78
+ for each (var site: Site in _sites )
79
+ {
80
+ if (site. x < xmin)
81
+ {
82
+ xmin = site. x ;
83
+ }
84
+ if (site. x > xmax)
85
+ {
86
+ xmax = site. x ;
87
+ }
88
+ }
89
+ // here's where we assume that the sites have been sorted on y:
90
+ ymin = _sites [ 0 ] . y ;
91
+ ymax = _sites [ _sites . length - 1 ] . y ;
92
+
93
+ return new Rectangle (xmin, ymin, xmax - xmin, ymax - ymin);
94
+ }
95
+
96
+ public function siteColors (referenceImage :BitmapData = null ):Vector.<uint >
97
+ {
98
+ var colors : Vector .< uint > = new Vector .< uint > ();
99
+ for each (var site: Site in _sites )
100
+ {
101
+ colors . push (referenceImage ? referenceImage. getPixel (site. x , site. y ) : site. color );
102
+ }
103
+ return colors ;
104
+ }
105
+
106
+ public function siteCoords ():Vector.<Point >
107
+ {
108
+ var coords: Vector .< Point> = new Vector .< Point> ();
109
+ for each (var site: Site in _sites )
110
+ {
111
+ coords. push (site. coord);
112
+ }
113
+ return coords;
114
+ }
115
+
116
+ /**
117
+ *
118
+ * @return the largest circle centered at each site that fits in its region;
119
+ * if the region is infinite, return a circle of radius 0.
120
+ *
121
+ */
122
+ public function circles ():Vector.<Circle>
123
+ {
124
+ var circles: Vector .< Circle> = new Vector .< Circle> ();
125
+ for each (var site: Site in _sites )
126
+ {
127
+ var radius : Number = 0 ;
128
+ var nearestEdge: Edge = site. nearestEdge();
129
+
130
+ ! nearestEdge. isPartOfConvexHull() && (radius = nearestEdge. sitesDistance() * 0.5 );
131
+ circles. push (new Circle(site. x , site. y , radius ));
132
+ }
133
+ return circles;
134
+ }
135
+
136
+ public function regions (plotBounds :Rectangle ):Vector.<Vector.<Point >>
137
+ {
138
+ var regions: Vector .< Vector.<Point>> = new Vector .< Vector.<Point>> ();
139
+ for each (var site: Site in _sites )
140
+ {
141
+ regions. push (site. region(plotBounds));
142
+ }
143
+ return regions;
144
+ }
145
+
146
+ /**
147
+ *
148
+ * @param proximityMap a BitmapData whose regions are filled with the site index values; see PlanePointsCanvas::fillRegions()
149
+ * @param x
150
+ * @param y
151
+ * @return coordinates of nearest Site to (x, y)
152
+ *
153
+ */
154
+ public function nearestSitePoint (proximityMap :BitmapData , x :Number , y :Number ):Point
155
+ {
156
+ var index : uint = proximityMap. getPixel (x , y );
157
+ if (index > _sites . length - 1 )
158
+ {
159
+ return null ;
160
+ }
161
+ return _sites [ index ] . coord;
162
+ }
163
+
164
+ }
165
+ }
0 commit comments