Skip to content

Instantly share code, notes, and snippets.

@lydonchandra
Created January 10, 2013 03:04
Show Gist options
  • Save lydonchandra/4499081 to your computer and use it in GitHub Desktop.
Save lydonchandra/4499081 to your computer and use it in GitHub Desktop.
Calculate Radius of a Circle in OpenLayers. Circle is implemented as a LinearRing.
OpenLayers.Geometry.LinearRing.prototype.getRadius = function() {
//http://gis.stackexchange.com/questions/20982/how-to-get-the-radius-of-a-circle-in-openlayers
//Use the formula for the area of a regular polygon.
//With a radius of r and k vertices (where in practice k typically varies from 4 through 360),
//the area now is A = r^2 * k Sin(360 / k) / 2. Solving for r yields r = Sqrt(2 A / (k Sin(360/k))).
//For k = 40 that's r = 0.565352 * Sqrt(A).
var area = Math.abs( this.getArea() );
var polygonSides = 100;
var radius = Math.sqrt( 2*area/(polygonSides*Math.sin(2*Math.PI/polygonSides)) );
return radius;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment