3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
6 var components = [new OpenLayers.Geometry.Point(10,10),
7 new OpenLayers.Geometry.Point(0,0)];
9 function test_LinearRing_constructor (t) {
13 ring = new OpenLayers.Geometry.LinearRing();
14 t.ok( ring instanceof OpenLayers.Geometry.LinearRing, "new OpenLayers.Geometry.LinearRing returns ring object" );
15 t.eq( ring.CLASS_NAME, "OpenLayers.Geometry.LinearRing", "ring.CLASS_NAME is set correctly");
16 t.eq( ring.components, [], "ring.components is set correctly");
19 ring = new OpenLayers.Geometry.LinearRing(components);
20 t.ok( ring instanceof OpenLayers.Geometry.LinearRing, "new OpenLayers.Geometry.LinearRing returns ring object" );
21 t.eq( ring.CLASS_NAME, "OpenLayers.Geometry.LinearRing", "ring.CLASS_NAME is set correctly");
22 t.eq( ring.components.length, 3, "ring.components.length is set correctly");
25 function test_LinearRing_addComponent(t) {
28 var ring = new OpenLayers.Geometry.LinearRing();
30 var point = new OpenLayers.Geometry.Point(0,0);
31 t.ok(ring.addComponent(point),
32 "addComponent returns true for 1st point");
33 t.eq(ring.components.length, 2, "add first point, correct length");
34 t.ok(ring.components[0].equals(point), "point one correct");
35 t.ok(ring.components[0] === ring.components[ring.components.length - 1],
36 "first and last point are the same");
38 newPoint = new OpenLayers.Geometry.Point(10,10);
39 t.ok(ring.addComponent( newPoint ),
40 "addComponent returns true for unique point");
41 t.eq(ring.components.length, 3, "correctly adds 3rd point");
42 t.ok(ring.components[0].equals(point), "point one correct");
43 t.ok(ring.components[1].equals(newPoint), "point one correct");
44 t.ok(ring.components[0] === ring.components[ring.components.length - 1],
45 "first and last point are the same");
47 var length = ring.components.length;
48 var clone = ring.components[length - 1].clone();
49 t.ok(!ring.addComponent(clone),
50 "addComponent returns false for adding a duplicate last point");
51 t.eq(ring.components.length, length,
52 "components remains unchanged after trying to add duplicate point");
53 t.ok(ring.addComponent(clone, length - 1),
54 "addComponent returns true when adding a duplicate with an index");
55 t.eq(ring.components.length, length + 1,
56 "components increase in length after adding a duplicate point with index");
60 function test_LinearRing_removeComponent(t) {
63 var components = [new OpenLayers.Geometry.Point(0,0),
64 new OpenLayers.Geometry.Point(0,10),
65 new OpenLayers.Geometry.Point(15,15),
66 new OpenLayers.Geometry.Point(10,0)
68 var ring = new OpenLayers.Geometry.LinearRing(components);
70 ring.removeComponent( ring.components[2] );
71 t.eq(ring.components.length, 4, "removing from linear ring with 5 points: length ok");
72 t.ok(ring.components[0].equals(components[0]), "point one correct");
73 t.ok(ring.components[1].equals(components[1]), "point two correct");
74 t.ok(ring.components[2].equals(components[3]), "point one correct");
75 t.ok(ring.components[0] === ring.components[ring.components.length - 1],
76 "first and last point are the same");
78 var testBounds = new OpenLayers.Bounds(0,0,10,10);
79 var ringBounds = ring.getBounds();
80 t.ok(ringBounds.equals(testBounds), "bounds correctly recalculated");
82 ring.removeComponent( ring.components[2] );
83 t.eq(ring.components.length, 4, "cant remove from linear ring with only 4 points. new length ok (unchanged)");
84 t.ok(ring.components[0].equals(components[0]), "point one correct");
85 t.ok(ring.components[1].equals(components[1]), "point two correct");
86 t.ok(ring.components[2].equals(components[3]), "point one correct");
87 t.ok(ring.components[0] === ring.components[ring.components.length - 1],
88 "first and last point are the same");
92 function test_LinearRing_getArea(t) {
94 var components = [new OpenLayers.Geometry.Point(0,0),
95 new OpenLayers.Geometry.Point(0,10),
96 new OpenLayers.Geometry.Point(10,10),
97 new OpenLayers.Geometry.Point(10,0)
99 var ring = new OpenLayers.Geometry.LinearRing(components);
101 t.eq(ring.getArea(), 100, "getArea works lovely");
104 function test_LinearRing_getLength(t) {
107 new OpenLayers.Geometry.Point(0,0),
108 new OpenLayers.Geometry.Point(0,10),
109 new OpenLayers.Geometry.Point(10,10),
110 new OpenLayers.Geometry.Point(10,0)
112 var ring = new OpenLayers.Geometry.LinearRing(components);
113 t.eq(ring.getLength(), 40, "getLength returns the correct perimiter");
116 function test_LinearRing_move(t) {
119 x = new Array(nvert),
120 y = new Array(nvert),
121 components = new Array(nvert);
123 t.plan(2 * (nvert + 1));
125 for(var i=0; i<nvert; ++i) {
126 x[i] = Math.random();
127 y[i] = Math.random();
128 components[i] = new OpenLayers.Geometry.Point(x[i], y[i]);
133 var ring = new OpenLayers.Geometry.LinearRing(components);
135 var dx = Math.random();
136 var dy = Math.random();
140 for(var j=0; j<nvert + 1; ++j) {
141 t.eq(ring.components[j].x, x[j] + dx,
142 "move correctly adjust x coord of " + j + " component");
143 t.eq(ring.components[j].y, y[j] + dy,
144 "move correctly adjust y coord of " + j + " component");
148 function test_LinearRing_rotate(t) {
152 new OpenLayers.Geometry.Point(10,10),
153 new OpenLayers.Geometry.Point(11,10),
154 new OpenLayers.Geometry.Point(11,11),
155 new OpenLayers.Geometry.Point(10,11)
158 var ring = new OpenLayers.Geometry.LinearRing(components);
160 // rotate a quarter turn around the origin
161 var origin = new OpenLayers.Geometry.Point(0, 0);
164 ring.rotate(angle, origin);
166 function withinTolerance(i, j) {
167 return Math.abs(i - j) < 1e-9;
170 t.ok(withinTolerance(ring.components[0].x , -10),
171 "rotate correctly adjusts x of component 0");
172 t.ok(withinTolerance(ring.components[0].y, 10),
173 "rotate correctly adjusts y of component 0");
174 t.ok(withinTolerance(ring.components[1].x, -10),
175 "rotate correctly adjusts x of component 1");
176 t.ok(withinTolerance(ring.components[1].y, 11),
177 "rotate correctly adjusts y of component 1");
178 t.ok(withinTolerance(ring.components[2].x, -11),
179 "rotate correctly adjusts x of component 2");
180 t.ok(withinTolerance(ring.components[2].y, 11),
181 "rotate correctly adjusts y of component 2");
182 t.ok(withinTolerance(ring.components[3].x, -11),
183 "rotate correctly adjusts x of component 3");
184 t.ok(withinTolerance(ring.components[3].y, 10),
185 "rotate correctly adjusts y of component 3");
186 t.ok(withinTolerance(ring.components[4].x, -10),
187 "rotate correctly adjusts x of component 4");
188 t.ok(withinTolerance(ring.components[4].y, 10),
189 "rotate correctly adjusts y of component 4");
192 function test_LinearRing_resize(t) {
196 new OpenLayers.Geometry.Point(10,10),
197 new OpenLayers.Geometry.Point(11,10),
198 new OpenLayers.Geometry.Point(11,11),
199 new OpenLayers.Geometry.Point(10,11)
202 var ring = new OpenLayers.Geometry.LinearRing(components);
204 // rotate a quarter turn around the origin
205 var origin = new OpenLayers.Geometry.Point(0, 0);
206 var scale = Math.random();
208 ring.resize(scale, origin);
210 function withinTolerance(i, j) {
211 return Math.abs(i - j) < 1e-9;
214 t.ok(withinTolerance(ring.components[0].x , 10 * scale),
215 "resize correctly adjusts x of component 0");
216 t.ok(withinTolerance(ring.components[0].y, 10 * scale),
217 "resize correctly adjusts y of component 0");
218 t.ok(withinTolerance(ring.components[1].x, 11 * scale),
219 "resize correctly adjusts x of component 1");
220 t.ok(withinTolerance(ring.components[1].y, 10 * scale),
221 "resize correctly adjusts y of component 1");
222 t.ok(withinTolerance(ring.components[2].x, 11 * scale),
223 "resize correctly adjusts x of component 2");
224 t.ok(withinTolerance(ring.components[2].y, 11 * scale),
225 "resize correctly adjusts y of component 2");
226 t.ok(withinTolerance(ring.components[3].x, 10 * scale),
227 "resize correctly adjusts x of component 3");
228 t.ok(withinTolerance(ring.components[3].y, 11 * scale),
229 "resize correctly adjusts y of component 3");
230 t.ok(withinTolerance(ring.components[4].x, 10 * scale),
231 "resize correctly adjusts x of component 4");
232 t.ok(withinTolerance(ring.components[4].y, 10 * scale),
233 "resize correctly adjusts y of component 4");