Adding a RegularPolygon handler for drawing squares, triangles, circles, etc. Demo in the regular-polygon.html example. Also adding a createRegularPolygon class method to the Polygon geometry class. Thanks to crschmidt for all the tests and help getting this in (closes #828).

git-svn-id: http://svn.openlayers.org/trunk/openlayers@4205 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2007-09-10 20:24:27 +00:00
parent 623e6e7112
commit 35dae2bdb0
7 changed files with 724 additions and 0 deletions

View File

@@ -173,6 +173,47 @@
}
function test_Polygon_createRegular(t) {
t.plan(22);
var sides = 40;
var poly = OpenLayers.Geometry.Polygon.createRegularPolygon(new OpenLayers.Geometry.Point(5,0), 6, sides);
var polyBounds = poly.getBounds();
t.eq(polyBounds.toBBOX(), "-0.981504,-5.981504,10.981504,5.981504", sides + " sided figure generates correct bbox.");
t.eq(poly.components.length, 1, "Poly has one linear ring");
t.eq(poly.components[0].components.length, sides + 1, "ring has 41 components");
t.eq(poly.components[0].components[0].id, poly.components[0].components[sides].id, "ring starts and ends with same geom");
t.eq(Math.round(poly.getArea()), Math.round(Math.PI * 36), "area of "+sides+" sided poly rounds to same area as a circle.");
var sides = 3;
var poly = OpenLayers.Geometry.Polygon.createRegularPolygon(new OpenLayers.Geometry.Point(5,0), 6, sides);
var polyBounds = poly.getBounds();
t.eq(polyBounds.toBBOX(), "-0.196152,-3,10.196152,6", sides + " sided figure generates correct bbox.");
t.eq(poly.components.length, 1, "Poly has one linear ring");
t.eq(poly.components[0].components.length, sides + 1, "ring has correct count of components");
t.eq(poly.components[0].components[0].id, poly.components[0].components[sides].id, "ring starts and ends with same geom");
t.eq(Math.round(poly.getArea()), 47, "area of 3 sided poly is correct");
var sides = 3;
var poly3 = OpenLayers.Geometry.Polygon.createRegularPolygon(new OpenLayers.Geometry.Point(10,0), 15, sides);
var polyBounds = poly3.getBounds();
t.eq(polyBounds.toBBOX(), "-2.990381,-7.5,22.990381,15", sides + " sided figure generates correct bbox.");
t.eq(Math.round(polyBounds.getCenterLonLat().lon), 10, "longitude of center of bounds is same as origin");
t.eq(poly3.components.length, 1, "Poly has one linear ring");
t.eq(poly3.components[0].components.length, sides + 1, "ring has correct count of components");
t.eq(poly3.components[0].components[0].id, poly3.components[0].components[sides].id, "ring starts and ends with same geom");
t.ok(poly3.getArea() > poly.getArea(), "area with radius 15 > poly with radius 6");
var sides = 4;
var poly4 = OpenLayers.Geometry.Polygon.createRegularPolygon(new OpenLayers.Geometry.Point(10,0), 15, sides);
var polyBounds = poly4.getBounds();
t.eq(polyBounds.toBBOX(), "-0.606602,-10.606602,20.606602,10.606602", sides + " sided figure generates correct bbox.");
t.eq(Math.round(polyBounds.getCenterLonLat().lon), 10, "longitude of center of bounds is same as origin");
t.eq(poly4.components.length, 1, "Poly has one linear ring");
t.eq(poly4.components[0].components.length, sides + 1, "ring has correct count of components");
t.eq(poly4.components[0].components[0].id, poly4.components[0].components[sides].id, "ring starts and ends with same geom");
t.ok(poly4.getArea() > poly3.getArea(), "square with radius 15 > triangle with radius 15");
}
function test_Polygon_equals(t) {
t.plan(3);

View File

@@ -0,0 +1,135 @@
<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_Handler_RegularPolygon_constructor(t) {
t.plan(3);
var control = new OpenLayers.Control();
control.id = Math.random();
var callbacks = {foo: "bar"};
var options = {bar: "foo"};
var oldInit = OpenLayers.Handler.prototype.initialize;
OpenLayers.Handler.prototype.initialize = function(con, call, opt) {
t.eq(con.id, control.id,
"constructor calls parent with the correct control");
t.eq(call, callbacks,
"constructor calls parent with the correct callbacks");
t.eq(opt, options,
"regular polygon constructor calls parent with the correct options");
}
var handler = new OpenLayers.Handler.RegularPolygon(control, callbacks, options);
OpenLayers.Handler.prototype.initialize = oldInit;
}
function test_Handler_RegularPolygon_activation(t) {
t.plan(3);
var map = new OpenLayers.Map('map');
var control = new OpenLayers.Control();
map.addControl(control);
var handler = new OpenLayers.Handler.RegularPolygon(control);
handler.active = true;
var activated = handler.activate();
t.ok(!activated,
"activate returns false if the handler was already active");
handler.active = false;
activated = handler.activate();
t.ok(activated,
"activate returns true if the handler was not already active");
activated = handler.deactivate();
t.ok(activated,
"deactivate returns true if the handler was active already");
}
function test_Handler_RegularPolygon_four_corners(t) {
t.plan(7);
var map = new OpenLayers.Map('map');
map.addLayer(new OpenLayers.Layer.WMS("", "", {}));
map.zoomToMaxExtent();
var control = new OpenLayers.Control();
map.addControl(control);
var handler = new OpenLayers.Handler.RegularPolygon(control, {});
var activated = handler.activate();
var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
handler.down(evt);
var evt = {xy: new OpenLayers.Pixel(175, 75), which: 1};
handler.move(evt);
t.eq(handler.feature.geometry.getBounds().toBBOX(),
"-35.15625,-35.15625,35.15625,35.15625",
"correct bounds after move");
t.eq(handler.feature.geometry.components[0].components.length, 5,
"geometry has 5 components");
t.eq(handler.feature.geometry.CLASS_NAME,
"OpenLayers.Geometry.Polygon",
"geometry is a polygon");
t.eq(handler.radius, 25*1.40625, "feature radius as set on handler");
var evt = {xy: new OpenLayers.Pixel(175, 80), which: 1};
handler.move(evt);
t.eq(handler.feature.geometry.getBounds().toBBOX(),
"-35.15625,-35.15625,35.15625,35.15625",
"correct bounds after move with a fixed radius");
handler.cancel();
handler.setOptions({radius:2 / Math.sqrt(2)});
var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
handler.down(evt);
t.eq(handler.feature.geometry.getBounds().toBBOX(),
"-1,-1,1,1",
"bounds with manual radius setting");
var evt = {xy: new OpenLayers.Pixel(175, 90), which: 1};
handler.move(evt);
t.eq(handler.feature.geometry.getBounds().toBBOX(),
"34.15625,-22.09375,36.15625,-20.09375",
"bounds with manual radius setting and mousemove");
}
function test_Handler_RegularPolygon_circle(t) {
t.plan(7);
var map = new OpenLayers.Map('map');
map.addLayer(new OpenLayers.Layer.WMS("", "", {}));
map.zoomToMaxExtent();
var control = new OpenLayers.Control();
map.addControl(control);
var handler = new OpenLayers.Handler.RegularPolygon(control, {}, {'sides':40});
var activated = handler.activate();
var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
handler.down(evt);
var evt = {xy: new OpenLayers.Pixel(175, 75), which: 1};
handler.move(evt);
t.eq(handler.feature.geometry.getBounds().toBBOX(),
"-35.15625,-35.15625,35.15625,35.15625",
"correct bounds after move");
t.eq(handler.feature.geometry.components[0].components.length, 41,
"geometry has correct numbre of components");
t.eq(handler.feature.geometry.CLASS_NAME,
"OpenLayers.Geometry.Polygon",
"geometry is a polygon");
t.eq(handler.radius, 25*1.40625, "feature radius as set on handler");
var evt = {xy: new OpenLayers.Pixel(175, 80), which: 1};
handler.move(evt);
t.eq(handler.feature.geometry.getBounds().toBBOX(),
"-35.823348,-35.823348,35.823348,35.823348",
"correct bounds after move with fixed radius");
handler.cancel();
handler.setOptions({radius:1});
var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
handler.down(evt);
t.eq(handler.feature.geometry.getBounds().toBBOX(),
"-0.996917,-0.996917,0.996917,0.996917",
"bounds with manual radius setting");
var evt = {xy: new OpenLayers.Pixel(175, 80), which: 1};
handler.move(evt);
t.eq(handler.feature.geometry.getBounds().toBBOX(),
"34.159333,-8.028167,36.153167,-6.034333",
"bounds with manual radius setting and mousemove");
}
</script>
</head>
<body>
<div id="map" style="width: 300px; height: 150px;"/>
</body>
</html>

View File

@@ -80,5 +80,6 @@
<li>Handler/test_Point.html</li>
<li>Handler/test_Path.html</li>
<li>Handler/test_Polygon.html</li>
<li>Handler/test_RegularPolygon.html</li>
<li>test_Map.html</li>
</ul>