OpenLayers OpenLayers

Changeset 4205

Show
Ignore:
Timestamp:
09/10/07 16:24:27 (1 year ago)
Author:
tschaub
Message:

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).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers.js

    r4128 r4205  
    120120            "OpenLayers/Handler/Feature.js", 
    121121            "OpenLayers/Handler/Drag.js", 
     122            "OpenLayers/Handler/RegularPolygon.js", 
    122123            "OpenLayers/Handler/Box.js", 
    123124            "OpenLayers/Handler/MouseWheel.js", 
  • trunk/openlayers/lib/OpenLayers/Geometry/Polygon.js

    r4110 r4205  
    6060    CLASS_NAME: "OpenLayers.Geometry.Polygon" 
    6161}); 
     62 
     63/** 
     64 * APIMethod: createRegularPolygon 
     65 * Create a regular polygon around a radius. Useful for creating circles  
     66 * and the like. 
     67 * 
     68 * Parameters: 
     69 * origin - {<OpenLayers.Geometry.Point>} center of polygon. 
     70 * radius - {Float} distance to vertex, in map units. 
     71 * sides - {Integer} Number of sides. 20 approximates a circle. 
     72 * rotation - {Float} original angle of rotation, in degrees. 
     73 */ 
     74OpenLayers.Geometry.Polygon.createRegularPolygon = function(origin, radius, sides, rotation) {   
     75    var angle = Math.PI * ((1/sides) - (1/2)); 
     76    if(rotation) { 
     77        angle += (rotation / 180) * Math.PI; 
     78    } 
     79    var rotateAngle, x, y; 
     80    var points = []; 
     81    for(var i=0; i<sides; ++i) { 
     82        rotatedAngle = angle + (i * 2 * Math.PI / sides); 
     83        x = origin.x + (radius * Math.cos(rotatedAngle)); 
     84        y = origin.y + (radius * Math.sin(rotatedAngle)); 
     85        points.push(new OpenLayers.Geometry.Point(x, y)); 
     86    } 
     87    var ring = new OpenLayers.Geometry.LinearRing(points); 
     88    return new OpenLayers.Geometry.Polygon([ring]); 
     89} 
  • trunk/openlayers/tests/Geometry/test_Polygon.html

    r4059 r4205  
    172172        } 
    173173         
     174    } 
     175 
     176    function test_Polygon_createRegular(t) { 
     177        t.plan(22); 
     178        var sides = 40; 
     179        var poly = OpenLayers.Geometry.Polygon.createRegularPolygon(new OpenLayers.Geometry.Point(5,0), 6, sides); 
     180        var polyBounds = poly.getBounds(); 
     181        t.eq(polyBounds.toBBOX(), "-0.981504,-5.981504,10.981504,5.981504", sides + " sided figure generates correct bbox."); 
     182        t.eq(poly.components.length, 1, "Poly has one linear ring"); 
     183        t.eq(poly.components[0].components.length, sides + 1, "ring has 41 components"); 
     184        t.eq(poly.components[0].components[0].id, poly.components[0].components[sides].id, "ring starts and ends with same geom"); 
     185        t.eq(Math.round(poly.getArea()), Math.round(Math.PI * 36), "area of "+sides+" sided poly rounds to same area as a circle."); 
     186         
     187        var sides = 3; 
     188        var poly = OpenLayers.Geometry.Polygon.createRegularPolygon(new OpenLayers.Geometry.Point(5,0), 6, sides); 
     189        var polyBounds = poly.getBounds(); 
     190        t.eq(polyBounds.toBBOX(), "-0.196152,-3,10.196152,6", sides + " sided figure generates correct bbox."); 
     191        t.eq(poly.components.length, 1, "Poly has one linear ring"); 
     192        t.eq(poly.components[0].components.length, sides + 1, "ring has  correct count of  components"); 
     193        t.eq(poly.components[0].components[0].id, poly.components[0].components[sides].id, "ring starts and ends with same geom"); 
     194        t.eq(Math.round(poly.getArea()), 47, "area of 3 sided poly is correct"); 
     195         
     196        var sides = 3; 
     197        var poly3 = OpenLayers.Geometry.Polygon.createRegularPolygon(new OpenLayers.Geometry.Point(10,0), 15, sides); 
     198        var polyBounds = poly3.getBounds(); 
     199        t.eq(polyBounds.toBBOX(), "-2.990381,-7.5,22.990381,15", sides + " sided figure generates correct bbox."); 
     200        t.eq(Math.round(polyBounds.getCenterLonLat().lon), 10, "longitude of center of bounds is same as origin"); 
     201        t.eq(poly3.components.length, 1, "Poly has one linear ring"); 
     202        t.eq(poly3.components[0].components.length, sides + 1, "ring has  correct count of  components"); 
     203        t.eq(poly3.components[0].components[0].id, poly3.components[0].components[sides].id, "ring starts and ends with same geom"); 
     204        t.ok(poly3.getArea() > poly.getArea(), "area with radius 15 > poly with radius 6");  
     205         
     206        var sides = 4; 
     207        var poly4 = OpenLayers.Geometry.Polygon.createRegularPolygon(new OpenLayers.Geometry.Point(10,0), 15, sides); 
     208        var polyBounds = poly4.getBounds(); 
     209        t.eq(polyBounds.toBBOX(), "-0.606602,-10.606602,20.606602,10.606602", sides + " sided figure generates correct bbox."); 
     210        t.eq(Math.round(polyBounds.getCenterLonLat().lon), 10, "longitude of center of bounds is same as origin"); 
     211        t.eq(poly4.components.length, 1, "Poly has one linear ring"); 
     212        t.eq(poly4.components[0].components.length, sides + 1, "ring has  correct count of  components"); 
     213        t.eq(poly4.components[0].components[0].id, poly4.components[0].components[sides].id, "ring starts and ends with same geom"); 
     214        t.ok(poly4.getArea() > poly3.getArea(), "square with radius 15 > triangle with radius 15");  
    174215    } 
    175216 
  • trunk/openlayers/tests/list-tests.html

    r4188 r4205  
    8181    <li>Handler/test_Path.html</li> 
    8282    <li>Handler/test_Polygon.html</li> 
     83    <li>Handler/test_RegularPolygon.html</li> 
    8384    <li>test_Map.html</li> 
    8485</ul>