Add functions to create regular polygons
This commit is contained in:
@@ -2,6 +2,7 @@ goog.provide('ol.geom.Polygon');
|
||||
|
||||
goog.require('goog.array');
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.math');
|
||||
goog.require('ol.extent');
|
||||
goog.require('ol.geom.GeometryLayout');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
@@ -422,3 +423,52 @@ ol.geom.Polygon.fromExtent = function(extent) {
|
||||
ol.geom.GeometryLayout.XY, flatCoordinates, [flatCoordinates.length]);
|
||||
return polygon;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Create a regular polygon from a circle.
|
||||
* @param {ol.geom.Circle} circle Circle geometry.
|
||||
* @param {number=} opt_sides Number of sides of the polygon. Default is 32.
|
||||
* @param {number=} opt_angle Start angle for the first vertex of the polygon in
|
||||
* radians. Default is 0.
|
||||
* @return {ol.geom.Polygon} Polygon geometry.
|
||||
*/
|
||||
ol.geom.Polygon.fromCircle = function(circle, opt_sides, opt_angle) {
|
||||
var sides = goog.isDef(opt_sides) ? opt_sides : 32;
|
||||
var stride = circle.getStride();
|
||||
var layout = circle.getLayout();
|
||||
var polygon = new ol.geom.Polygon(null, layout);
|
||||
var flatCoordinates = goog.array.repeat(0, stride * (sides + 1));
|
||||
var ends = [flatCoordinates.length];
|
||||
polygon.setFlatCoordinates(layout, flatCoordinates, ends);
|
||||
ol.geom.Polygon.makeRegular(
|
||||
polygon, circle.getCenter(), circle.getRadius(), opt_angle);
|
||||
return polygon;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Modify the coordinates of a polygon to make it a regular polygon.
|
||||
* @param {ol.geom.Polygon} polygon Polygon geometry.
|
||||
* @param {ol.Coordinate} center Center of the regular polygon.
|
||||
* @param {number} radius Radius of the regular polygon.
|
||||
* @param {number=} opt_angle Start angle for the first vertex of the polygon in
|
||||
* radians. Default is 0.
|
||||
*/
|
||||
ol.geom.Polygon.makeRegular = function(polygon, center, radius, opt_angle) {
|
||||
var flatCoordinates = polygon.getFlatCoordinates();
|
||||
var layout = polygon.getLayout();
|
||||
var stride = polygon.getStride();
|
||||
var ends = polygon.getEnds();
|
||||
goog.asserts.assert(ends.length === 1, 'only 1 ring is supported');
|
||||
var sides = flatCoordinates.length / stride - 1;
|
||||
var startAngle = goog.isDef(opt_angle) ? opt_angle : 0;
|
||||
var angle, coord, offset;
|
||||
for (var i = 0; i <= sides; ++i) {
|
||||
offset = i * stride;
|
||||
angle = startAngle + (goog.math.modulo(i, sides) * 2 * Math.PI / sides);
|
||||
flatCoordinates[offset] = center[0] + (radius * Math.cos(angle));
|
||||
flatCoordinates[offset + 1] = center[1] + (radius * Math.sin(angle));
|
||||
}
|
||||
polygon.setFlatCoordinates(layout, flatCoordinates, ends);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user