3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
7 function test_Point_constructor (t) {
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");
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");
27 function test_Point_calculateBounds (t) {
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" );
41 function test_Point_transform_getBounds (t) {
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");
54 function test_Point_distanceTo(t) {
59 point1 = new OpenLayers.Geometry.Point(x1, y1);
63 point2 = new OpenLayers.Geometry.Point(x2, y2);
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");
69 // test that details are returned (though trivial in this case)
70 var result = point1.distanceTo(point2, {details: true});
71 t.eq(result.distance, point1.distanceTo(point2), "[details] distance property is same as return without details");
72 t.eq(result.x0, x1, "[details] x0 property is correct");
73 t.eq(result.y0, y1, "[details] y0 property is correct");
74 t.eq(result.x1, x2, "[details] x1 property is correct");
75 t.eq(result.y1, y2, "[details] y1 property is correct");
79 function test_Point_toString(t) {
84 point = new OpenLayers.Geometry.Point(x, y);
85 t.eq(point.toString(), "POINT(" + x + " " + y + ")",
86 "toString() returns WKT" );
90 function test_Point_move(t) {
95 point = new OpenLayers.Geometry.Point(x, y);
97 var dx = 10 * Math.random();
98 var dy = 10 * Math.random();
101 t.eq(point.x, x + dx, "move() correctly modifies x");
102 t.eq(point.y, y + dy, "move() correctly modifies y");
104 t.ok(point.bounds == null, "bounds is cleared after a move()");
107 function test_Point_rotate(t) {
110 var tolerance = 1e-10;
113 var point = new OpenLayers.Geometry.Point(x, y);
114 var origin = new OpenLayers.Geometry.Point(5, 10);
116 // rotate a full revolution
117 point.bounds = "foo";
118 point.rotate(360, origin);
119 t.ok(((point.x - x) / x) < tolerance,
120 "rotate by 360 returns to the same y");
121 t.ok(((point.y - y) / y) < tolerance,
122 "rotate by 360 returns to the same y");
124 t.ok(point.bounds == null, "bounds is cleared after a rotate()");
126 // rotate an 1/8 turn
127 point.rotate(45, origin);
128 t.ok(((point.x - 1.4644660940672636) / 1.4644660940672636) < tolerance,
129 "rotate 1/8 turn correctly");
130 t.ok(((point.y - 20.606601717798213) / 20.606601717798213) < tolerance,
131 "rotate 1/8 turn correctly");
134 function test_Point_resize(t) {
137 var tolerance = 1e-10;
138 var x = 100 * Math.random();
139 var y = 100 * Math.random();
140 var point = new OpenLayers.Geometry.Point(x, y);
141 point.bounds = "foo";
143 var i = 100 * Math.random();
144 var j = 100 * Math.random();
145 var origin = new OpenLayers.Geometry.Point(i, j);
147 var scale = 10 * Math.random();
148 var oldDistance = origin.distanceTo(point);
150 var ret = point.resize(scale, origin);
151 var newDistance = origin.distanceTo(point);
153 t.ok(ret === point, "resize returns geometry");
154 t.ok((origin.x == i) && (origin.y == j),
155 "resize leaves the origin untouched");
156 t.ok((((newDistance / oldDistance) - scale) / scale) < tolerance,
157 "resize moves points the correct distance from the origin");
159 t.ok(point.bounds == null, "bounds is correctly cleared after a resize()");
161 // resize with non uniform scaling (ratio != 1)
162 point = new OpenLayers.Geometry.Point(10, 10);
163 origin = new OpenLayers.Geometry.Point(0, 0);
164 point.resize(2, origin, 4);
165 t.eq(point.x, 80, "non-uniform scaling correctly applied in x dim");
166 t.eq(point.y, 20, "non-uniform scaling correctly applied in y dim");
170 function test_Point_equals(t) {
173 var x = Math.random() * 100;
174 var y = Math.random() * 100;
175 var geometry = new OpenLayers.Geometry.Point(x, y);
176 var equal = new OpenLayers.Geometry.Point(x, y);
177 var offX = new OpenLayers.Geometry.Point(x + 1, y);
178 var offY = new OpenLayers.Geometry.Point(x, y + 1);
179 t.ok(geometry.equals(equal),
180 "equals() returns true for a geometry with equivalent coordinates");
181 t.ok(!geometry.equals(offX),
182 "equals() returns false for a geometry with offset x");
183 t.ok(!geometry.equals(offY),
184 "equals() returns false for a geometry with offset y");
187 function test_getVertices(t) {
190 var point = new OpenLayers.Geometry.Point(10, 20);
191 var verts = point.getVertices();
192 t.ok(verts instanceof Array, "got back an array");
193 t.eq(verts.length, 1, "of length 1");
194 t.geom_eq(verts[0], point, "with correct geometry");
197 function test_Point_clone(t) {
200 var x = Math.random() * 100;
201 var y = Math.random() * 100;
202 var geometry = new OpenLayers.Geometry.Point(x, y);
203 var clone = geometry.clone();
204 t.ok(clone instanceof OpenLayers.Geometry.Point,
205 "clone() creates an OpenLayers.Geometry.Point");
206 t.ok(geometry.equals(clone), "clone has equivalent coordinates");