| 1 |
<html> |
|---|
| 2 |
<head> |
|---|
| 3 |
<script src="../../lib/OpenLayers.js"></script> |
|---|
| 4 |
<script type="text/javascript"> |
|---|
| 5 |
var point; |
|---|
| 6 |
|
|---|
| 7 |
function test_Point_constructor (t) { |
|---|
| 8 |
t.plan( 8 ); |
|---|
| 9 |
|
|---|
| 10 |
//empty |
|---|
| 11 |
point = new OpenLayers.Geometry.Point(); |
|---|
| 12 |
t.ok( point instanceof OpenLayers.Geometry.Point, "new OpenLayers.Geometry.Point returns point object" ); |
|---|
| 13 |
t.eq( point.CLASS_NAME, "OpenLayers.Geometry.Point", "point.CLASS_NAME is set correctly"); |
|---|
| 14 |
|
|---|
| 15 |
//valid |
|---|
| 16 |
var x = 10; |
|---|
| 17 |
var y = 20; |
|---|
| 18 |
point = new OpenLayers.Geometry.Point(x, y); |
|---|
| 19 |
t.ok( point instanceof OpenLayers.Geometry.Point, "new OpenLayers.Geometry.Point returns point object" ); |
|---|
| 20 |
t.eq( point.CLASS_NAME, "OpenLayers.Geometry.Point", "point.CLASS_NAME is set correctly"); |
|---|
| 21 |
t.eq( point.x, x, "point.x is set correctly"); |
|---|
| 22 |
t.eq( point.y, y, "point.y is set correctly"); |
|---|
| 23 |
t.eq( point.lon, null, "point.lon is not set"); |
|---|
| 24 |
t.eq( point.lat, null, "point.lat is not set"); |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
function test_Point_calculateBounds (t) { |
|---|
| 28 |
t.plan(4); |
|---|
| 29 |
|
|---|
| 30 |
var x = 10; |
|---|
| 31 |
var y = 20; |
|---|
| 32 |
point = new OpenLayers.Geometry.Point(x, y); |
|---|
| 33 |
point.calculateBounds(); |
|---|
| 34 |
t.eq( point.bounds.left, x, "bounds.left is 10" ); |
|---|
| 35 |
t.eq( point.bounds.right, x, "bounds.right is 10" ); |
|---|
| 36 |
t.eq( point.bounds.top, y, "bounds.top is 20" ); |
|---|
| 37 |
t.eq( point.bounds.bottom, y, "bounds.bottom is 20" ); |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
function test_Point_transform_getBounds (t) { |
|---|
| 42 |
t.plan(2); |
|---|
| 43 |
|
|---|
| 44 |
var x = 10; |
|---|
| 45 |
var y = 20; |
|---|
| 46 |
point = new OpenLayers.Geometry.Point(x, y); |
|---|
| 47 |
point.calculateBounds(); |
|---|
| 48 |
t.ok( point.bounds != null, "bounds calculated by calcBounds" ); |
|---|
| 49 |
point.transform(new OpenLayers.Projection("EPSG:4326"), |
|---|
| 50 |
new OpenLayers.Projection("EPSG:900913")); |
|---|
| 51 |
t.eq(point.bounds, null, "Point bounds cleared after transform"); |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
function test_Point_distanceTo(t) { |
|---|
| 55 |
t.plan(2); |
|---|
| 56 |
|
|---|
| 57 |
var x1 = 10; |
|---|
| 58 |
var y1 = 20; |
|---|
| 59 |
point1 = new OpenLayers.Geometry.Point(x1, y1); |
|---|
| 60 |
|
|---|
| 61 |
var x2 = 100; |
|---|
| 62 |
var y2 = 200; |
|---|
| 63 |
point2 = new OpenLayers.Geometry.Point(x2, y2); |
|---|
| 64 |
|
|---|
| 65 |
var dist = point1.distanceTo(point2) |
|---|
| 66 |
t.eq( dist, 201.24611797498107267682563018581, "distances calculating correctly"); |
|---|
| 67 |
t.eq( dist, Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)), "distance calculation correct"); |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
function test_Point_toString(t) { |
|---|
| 71 |
t.plan(1); |
|---|
| 72 |
|
|---|
| 73 |
var x = 10; |
|---|
| 74 |
var y = 20; |
|---|
| 75 |
point = new OpenLayers.Geometry.Point(x, y); |
|---|
| 76 |
t.eq(point.toString(), "POINT(" + x + " " + y + ")", |
|---|
| 77 |
"toString() returns WKT" ); |
|---|
| 78 |
|
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
function test_Point_move(t) { |
|---|
| 82 |
t.plan(3); |
|---|
| 83 |
|
|---|
| 84 |
var x = 10; |
|---|
| 85 |
var y = 20; |
|---|
| 86 |
point = new OpenLayers.Geometry.Point(x, y); |
|---|
| 87 |
|
|---|
| 88 |
var dx = 10 * Math.random(); |
|---|
| 89 |
var dy = 10 * Math.random(); |
|---|
| 90 |
point.bounds = "foo"; |
|---|
| 91 |
point.move(dx, dy); |
|---|
| 92 |
t.eq(point.x, x + dx, "move() correctly modifies x"); |
|---|
| 93 |
t.eq(point.y, y + dy, "move() correctly modifies y"); |
|---|
| 94 |
|
|---|
| 95 |
t.ok(point.bounds == null, "bounds is cleared after a move()"); |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
function test_Point_rotate(t) { |
|---|
| 99 |
t.plan(5); |
|---|
| 100 |
|
|---|
| 101 |
var tolerance = 1e-10; |
|---|
| 102 |
var x = 10; |
|---|
| 103 |
var y = 20; |
|---|
| 104 |
var point = new OpenLayers.Geometry.Point(x, y); |
|---|
| 105 |
var origin = new OpenLayers.Geometry.Point(5, 10); |
|---|
| 106 |
|
|---|
| 107 |
// rotate a full revolution |
|---|
| 108 |
point.bounds = "foo"; |
|---|
| 109 |
point.rotate(360, origin); |
|---|
| 110 |
t.ok(((point.x - x) / x) < tolerance, |
|---|
| 111 |
"rotate by 360 returns to the same y"); |
|---|
| 112 |
t.ok(((point.y - y) / y) < tolerance, |
|---|
| 113 |
"rotate by 360 returns to the same y"); |
|---|
| 114 |
|
|---|
| 115 |
t.ok(point.bounds == null, "bounds is cleared after a rotate()"); |
|---|
| 116 |
|
|---|
| 117 |
// rotate an 1/8 turn |
|---|
| 118 |
point.rotate(45, origin); |
|---|
| 119 |
t.ok(((point.x - 1.4644660940672636) / 1.4644660940672636) < tolerance, |
|---|
| 120 |
"rotate 1/8 turn correctly"); |
|---|
| 121 |
t.ok(((point.y - 20.606601717798213) / 20.606601717798213) < tolerance, |
|---|
| 122 |
"rotate 1/8 turn correctly"); |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
function test_Point_resize(t) { |
|---|
| 126 |
t.plan(5); |
|---|
| 127 |
|
|---|
| 128 |
var tolerance = 1e-10; |
|---|
| 129 |
var x = 100 * Math.random(); |
|---|
| 130 |
var y = 100 * Math.random(); |
|---|
| 131 |
var point = new OpenLayers.Geometry.Point(x, y); |
|---|
| 132 |
point.bounds = "foo"; |
|---|
| 133 |
|
|---|
| 134 |
var i = 100 * Math.random(); |
|---|
| 135 |
var j = 100 * Math.random(); |
|---|
| 136 |
var origin = new OpenLayers.Geometry.Point(i, j); |
|---|
| 137 |
|
|---|
| 138 |
var scale = 10 * Math.random(); |
|---|
| 139 |
var oldDistance = origin.distanceTo(point); |
|---|
| 140 |
|
|---|
| 141 |
point.resize(scale, origin); |
|---|
| 142 |
var newDistance = origin.distanceTo(point); |
|---|
| 143 |
|
|---|
| 144 |
t.ok((origin.x == i) && (origin.y == j), |
|---|
| 145 |
"resize leaves the origin untouched"); |
|---|
| 146 |
t.ok((((newDistance / oldDistance) - scale) / scale) < tolerance, |
|---|
| 147 |
"resize moves points the correct distance from the origin"); |
|---|
| 148 |
|
|---|
| 149 |
t.ok(point.bounds == null, "bounds is correctly cleared after a resize()"); |
|---|
| 150 |
|
|---|
| 151 |
// resize with non uniform scaling (ratio != 1) |
|---|
| 152 |
point = new OpenLayers.Geometry.Point(10, 10); |
|---|
| 153 |
origin = new OpenLayers.Geometry.Point(0, 0); |
|---|
| 154 |
point.resize(2, origin, 4); |
|---|
| 155 |
t.eq(point.x, 80, "non-uniform scaling correctly applied in x dim"); |
|---|
| 156 |
t.eq(point.y, 20, "non-uniform scaling correctly applied in y dim"); |
|---|
| 157 |
|
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
function test_Point_equals(t) { |
|---|
| 161 |
t.plan(3); |
|---|
| 162 |
|
|---|
| 163 |
var x = Math.random() * 100; |
|---|
| 164 |
var y = Math.random() * 100; |
|---|
| 165 |
var geometry = new OpenLayers.Geometry.Point(x, y); |
|---|
| 166 |
var equal = new OpenLayers.Geometry.Point(x, y); |
|---|
| 167 |
var offX = new OpenLayers.Geometry.Point(x + 1, y); |
|---|
| 168 |
var offY = new OpenLayers.Geometry.Point(x, y + 1); |
|---|
| 169 |
t.ok(geometry.equals(equal), |
|---|
| 170 |
"equals() returns true for a geometry with equivalent coordinates"); |
|---|
| 171 |
t.ok(!geometry.equals(offX), |
|---|
| 172 |
"equals() returns false for a geometry with offset x"); |
|---|
| 173 |
t.ok(!geometry.equals(offY), |
|---|
| 174 |
"equals() returns false for a geometry with offset y"); |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
function test_Point_clone(t) { |
|---|
| 178 |
t.plan(2); |
|---|
| 179 |
|
|---|
| 180 |
var x = Math.random() * 100; |
|---|
| 181 |
var y = Math.random() * 100; |
|---|
| 182 |
var geometry = new OpenLayers.Geometry.Point(x, y); |
|---|
| 183 |
var clone = geometry.clone(); |
|---|
| 184 |
t.ok(clone instanceof OpenLayers.Geometry.Point, |
|---|
| 185 |
"clone() creates an OpenLayers.Geometry.Point"); |
|---|
| 186 |
t.ok(geometry.equals(clone), "clone has equivalent coordinates"); |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
</script> |
|---|
| 191 |
</head> |
|---|
| 192 |
<body> |
|---|
| 193 |
</body> |
|---|
| 194 |
</html> |
|---|