| 1 |
<html> |
|---|
| 2 |
<head> |
|---|
| 3 |
<script src="../../lib/OpenLayers.js"></script> |
|---|
| 4 |
<script type="text/javascript"> |
|---|
| 5 |
var line; |
|---|
| 6 |
var components = [new OpenLayers.Geometry.Point(10,10), |
|---|
| 7 |
new OpenLayers.Geometry.Point(0,0)]; |
|---|
| 8 |
|
|---|
| 9 |
function test_LinearRing_constructor (t) { |
|---|
| 10 |
t.plan( 6 ); |
|---|
| 11 |
|
|---|
| 12 |
//null |
|---|
| 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"); |
|---|
| 17 |
|
|---|
| 18 |
//valid components |
|---|
| 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"); |
|---|
| 23 |
} |
|---|
| 24 |
|
|---|
| 25 |
function test_LinearRing_addComponent(t) { |
|---|
| 26 |
t.plan(13); |
|---|
| 27 |
|
|---|
| 28 |
var ring = new OpenLayers.Geometry.LinearRing(); |
|---|
| 29 |
|
|---|
| 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"); |
|---|
| 37 |
|
|---|
| 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"); |
|---|
| 46 |
|
|---|
| 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"); |
|---|
| 57 |
|
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
function test_LinearRing_removeComponent(t) { |
|---|
| 61 |
t.plan(11); |
|---|
| 62 |
|
|---|
| 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) |
|---|
| 67 |
]; |
|---|
| 68 |
var ring = new OpenLayers.Geometry.LinearRing(components); |
|---|
| 69 |
|
|---|
| 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"); |
|---|
| 77 |
|
|---|
| 78 |
var testBounds = new OpenLayers.Bounds(0,0,10,10); |
|---|
| 79 |
var ringBounds = ring.getBounds(); |
|---|
| 80 |
t.ok(ringBounds.equals(testBounds), "bounds correctly recalculated"); |
|---|
| 81 |
|
|---|
| 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"); |
|---|
| 89 |
|
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
function test_LinearRing_getArea(t) { |
|---|
| 93 |
t.plan(1); |
|---|
| 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) |
|---|
| 98 |
]; |
|---|
| 99 |
var ring = new OpenLayers.Geometry.LinearRing(components); |
|---|
| 100 |
|
|---|
| 101 |
t.eq(ring.getArea(), 100, "getArea works lovely"); |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
function test_LinearRing_getLength(t) { |
|---|
| 105 |
t.plan(1); |
|---|
| 106 |
var components = [ |
|---|
| 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) |
|---|
| 111 |
]; |
|---|
| 112 |
var ring = new OpenLayers.Geometry.LinearRing(components); |
|---|
| 113 |
t.eq(ring.getLength(), 40, "getLength returns the correct perimiter"); |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
function test_LinearRing_move(t) { |
|---|
| 117 |
|
|---|
| 118 |
var nvert = 4, |
|---|
| 119 |
x = new Array(nvert), |
|---|
| 120 |
y = new Array(nvert), |
|---|
| 121 |
components = new Array(nvert); |
|---|
| 122 |
|
|---|
| 123 |
t.plan(2 * (nvert + 1)); |
|---|
| 124 |
|
|---|
| 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]); |
|---|
| 129 |
} |
|---|
| 130 |
x.push(x[0]); |
|---|
| 131 |
y.push(y[0]); |
|---|
| 132 |
|
|---|
| 133 |
var ring = new OpenLayers.Geometry.LinearRing(components); |
|---|
| 134 |
|
|---|
| 135 |
var dx = Math.random(); |
|---|
| 136 |
var dy = Math.random(); |
|---|
| 137 |
|
|---|
| 138 |
ring.move(dx, dy); |
|---|
| 139 |
|
|---|
| 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"); |
|---|
| 145 |
} |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
function test_LinearRing_rotate(t) { |
|---|
| 149 |
t.plan(10); |
|---|
| 150 |
|
|---|
| 151 |
var components = [ |
|---|
| 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) |
|---|
| 156 |
]; |
|---|
| 157 |
|
|---|
| 158 |
var ring = new OpenLayers.Geometry.LinearRing(components); |
|---|
| 159 |
|
|---|
| 160 |
// rotate a quarter turn around the origin |
|---|
| 161 |
var origin = new OpenLayers.Geometry.Point(0, 0); |
|---|
| 162 |
var angle = 90; |
|---|
| 163 |
|
|---|
| 164 |
ring.rotate(angle, origin); |
|---|
| 165 |
|
|---|
| 166 |
function withinTolerance(i, j) { |
|---|
| 167 |
return Math.abs(i - j) < 1e-9; |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 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"); |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
function test_LinearRing_resize(t) { |
|---|
| 193 |
t.plan(10); |
|---|
| 194 |
|
|---|
| 195 |
var components = [ |
|---|
| 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) |
|---|
| 200 |
]; |
|---|
| 201 |
|
|---|
| 202 |
var ring = new OpenLayers.Geometry.LinearRing(components); |
|---|
| 203 |
|
|---|
| 204 |
// rotate a quarter turn around the origin |
|---|
| 205 |
var origin = new OpenLayers.Geometry.Point(0, 0); |
|---|
| 206 |
var scale = Math.random(); |
|---|
| 207 |
|
|---|
| 208 |
ring.resize(scale, origin); |
|---|
| 209 |
|
|---|
| 210 |
function withinTolerance(i, j) { |
|---|
| 211 |
return Math.abs(i - j) < 1e-9; |
|---|
| 212 |
} |
|---|
| 213 |
|
|---|
| 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"); |
|---|
| 234 |
} |
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
</script> |
|---|
| 238 |
</head> |
|---|
| 239 |
<body> |
|---|
| 240 |
</body> |
|---|
| 241 |
</html> |
|---|