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:
@@ -59,3 +59,31 @@ OpenLayers.Geometry.Polygon = OpenLayers.Class(
|
||||
|
||||
CLASS_NAME: "OpenLayers.Geometry.Polygon"
|
||||
});
|
||||
|
||||
/**
|
||||
* APIMethod: createRegularPolygon
|
||||
* Create a regular polygon around a radius. Useful for creating circles
|
||||
* and the like.
|
||||
*
|
||||
* Parameters:
|
||||
* origin - {<OpenLayers.Geometry.Point>} center of polygon.
|
||||
* radius - {Float} distance to vertex, in map units.
|
||||
* sides - {Integer} Number of sides. 20 approximates a circle.
|
||||
* rotation - {Float} original angle of rotation, in degrees.
|
||||
*/
|
||||
OpenLayers.Geometry.Polygon.createRegularPolygon = function(origin, radius, sides, rotation) {
|
||||
var angle = Math.PI * ((1/sides) - (1/2));
|
||||
if(rotation) {
|
||||
angle += (rotation / 180) * Math.PI;
|
||||
}
|
||||
var rotateAngle, x, y;
|
||||
var points = [];
|
||||
for(var i=0; i<sides; ++i) {
|
||||
rotatedAngle = angle + (i * 2 * Math.PI / sides);
|
||||
x = origin.x + (radius * Math.cos(rotatedAngle));
|
||||
y = origin.y + (radius * Math.sin(rotatedAngle));
|
||||
points.push(new OpenLayers.Geometry.Point(x, y));
|
||||
}
|
||||
var ring = new OpenLayers.Geometry.LinearRing(points);
|
||||
return new OpenLayers.Geometry.Polygon([ring]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user