#807 - give geometries a resize method - examples and tests for demonstration

git-svn-id: http://svn.openlayers.org/trunk/openlayers@3631 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2007-07-06 20:32:22 +00:00
parent 0d596fbb03
commit c2fcb22d98
5 changed files with 141 additions and 0 deletions

View File

@@ -260,6 +260,24 @@ OpenLayers.Geometry.Collection.prototype =
} }
}, },
/**
* APIMethod: resize
* Resize a geometry relative to some origin. Use this method to apply
* a uniform scaling to a geometry.
*
* Parameters:
* scale - {Float} Factor by which to scale the geometry. A scale of 2
* doubles the size of the geometry in each dimension
* (lines, for example, will be twice as long, and polygons
* will have four times the area).
* origin - {OpenLayers.Geometry.Point} Point of origin for resizing
*/
resize: function(scale, origin) {
for(var i=0; i<this.components.length; ++i) {
this.components[i].resize(scale, origin);
}
},
/** /**
* APIMethod: equals * APIMethod: equals
* Tests for equivalent geometries * Tests for equivalent geometries

View File

@@ -148,6 +148,23 @@ OpenLayers.Geometry.Point.prototype =
this.y = origin.y + (radius * Math.sin(theta)); this.y = origin.y + (radius * Math.sin(theta));
}, },
/**
* APIMethod: resize
* Resize a point relative to some origin. For points, this has the effect
* of scaling a vector (from the origin to the point). This method is
* more useful on geometry collection subclasses.
*
* Parameters:
* scale - {Float} Ratio of the new distance from the origin to the old
* distance from the origin. A scale of 2 doubles the
* distance between the point and origin.
* origin - {OpenLayers.Geometry.Point} Point of origin for resizing
*/
resize: function(scale, origin) {
this.x = origin.x + (scale * (this.x - origin.x));
this.y = origin.y + (scale * (this.y - origin.y));
},
/** @final @type String */ /** @final @type String */
CLASS_NAME: "OpenLayers.Geometry.Point" CLASS_NAME: "OpenLayers.Geometry.Point"
}); });

View File

@@ -106,6 +106,48 @@
} }
} }
function test_LineString_resize(t) {
t.plan(7);
var tolerance = 1e-10;
var components = [new OpenLayers.Geometry.Point(10 * Math.random(),
10 * Math.random()),
new OpenLayers.Geometry.Point(10 * Math.random(),
10 * Math.random())];
var geometry = new OpenLayers.Geometry.LineString(components);
var origin = new OpenLayers.Geometry.Point(10 * Math.random(),
10 * Math.random());
var scale = 10 * Math.random();
var oldLength = geometry.getLength();
geometry.resize(scale, origin);
var newLength = geometry.getLength();
t.ok((((newLength / oldLength) - scale) / scale) < tolerance,
"resize correctly changes the length of a linestring")
var originals = [];
var comp;
for(var i=0; i<geometry.components.length; ++i) {
comp = geometry.components[i];
originals[i] = comp.resize;
comp.resize = function(s, o) {
t.ok(true, "resize called for component " + i);
t.ok(s == scale, "resize called with correct scale");
t.ok(o == origin, "resize called with correct origin");
}
}
geometry.resize(scale, origin);
// restore the original resize defs
for(var i=0; i<geometry.components.length; ++i) {
comp.resize = originals[i];
}
}
function test_LineString_equals(t) { function test_LineString_equals(t) {
t.plan(3); t.plan(3);

View File

@@ -103,6 +103,30 @@
"rotate 1/8 turn correctly") "rotate 1/8 turn correctly")
} }
function test_Point_resize(t) {
t.plan(2);
var tolerance = 1e-10;
var x = 100 * Math.random();
var y = 100 * Math.random();
var point = new OpenLayers.Geometry.Point(x, y);
var i = 100 * Math.random();
var j = 100 * Math.random();
var origin = new OpenLayers.Geometry.Point(i, j);
var scale = 10 * Math.random();
var oldDistance = origin.distanceTo(point);
point.resize(scale, origin);
var newDistance = origin.distanceTo(point);
t.ok((origin.x == i) && (origin.y == j),
"resize leaves the origin untouched");
t.ok((((newDistance / oldDistance) - scale) / scale) < tolerance,
"resize moves points the correct distance from the origin")
}
function test_Point_equals(t) { function test_Point_equals(t) {
t.plan(3); t.plan(3);

View File

@@ -133,6 +133,46 @@
} }
} }
function test_Polygon_resize(t) {
t.plan(8);
var tolerance = 1e-10;
var geometry = new OpenLayers.Geometry.Polygon([linearRing, linearRing2]);
var origin = new OpenLayers.Geometry.Point(10 * Math.random(),
10 * Math.random());
var scale = 10 * Math.random();
var oldArea = geometry.getArea();
var oldPerimeter = geometry.getLength();
geometry.resize(scale, origin);
var newArea = geometry.getArea();
var newPerimeter = geometry.getLength();
t.ok((((newArea / oldArea) - (scale * scale)) / (scale * scale)) < tolerance,
"resize correctly changes the area of a polygon")
t.ok((((newPerimeter / oldPerimeter) - scale) / scale) < tolerance,
"resize correctly changes the perimeter of a polygon")
var originals = [];
var comp;
for(var i=0; i<geometry.components.length; ++i) {
comp = geometry.components[i];
originals[i] = comp.resize;
comp.resize = function(s, o) {
t.ok(true, "resize called for component " + i);
t.ok(s == scale, "resize called with correct scale");
t.ok(o == origin, "resize called with correct origin");
}
}
geometry.resize(scale, origin);
// restore the original resize defs
for(var i=0; i<geometry.components.length; ++i) {
comp.resize = originals[i];
}
}
function test_Polygon_equals(t) { function test_Polygon_equals(t) {
t.plan(3); t.plan(3);