EDIT see my comment below (the issue has not been resolved). Anyone who wants to try my code can do it here: http://tinker.io/e0676
EDIT2 I think I have found the problem: http://www.geocomputation.org/1999/076/gc_076.htm Will try to solve it somehow...
So here's the thing. If you have the area and perimeter of a polygon you can measure how close is a polygon to a circle by getting the result of area/(perimeter*perimeter) . I know how to measure the perimeter:
function getPolySize(arrP){ //the first and last point in arrP is the same!
var S = 0;
for(var i=1, l=arrP.length;i<l;i++)
S += Math.sqrt(Math.pow(arrP[i-1].x-arrP[i].x,2)+Math.pow(arrP[i-1].y-arrP[i].y,2));
return S;
}
And the Area:
function getPolyArea(arrP){
var A = 0;
for(var i=0,l=arrP.length-1;i<l;i++)
A += arrP[i].x*arrP[i+1].y - arrP[i+1].x*arrP[i].y;
A = A/2;
return Math.abs(A);
}
Everything works fine, but if I make a polygon that is as close to a circle as possible than I start to get weird results. I use this to make circle polygons:
var arrCircle = [];
function setPixel(x,y){
arrCircle.push({x:x,y:y});
}
function rasterCircle(xCenter, yCenter, radius)
{
var x, y, r2;
r2 = radius * radius;
for (x = -radius+1; x <= radius; x++){
y = Math.floor(Math.sqrt(r2 - x*x) + 0.5);
setPixel(xCenter + x, yCenter + y);
}
for (x = radius-1; x >= -radius; x--) {
y = Math.floor(Math.sqrt(r2 - x*x) + 0.5);
setPixel(xCenter + x, yCenter - y);
}
}
arrCircle.push(arrCircle[0]);
Since circle polygons with a larger radius are closer to the ideal circle than the ones with a smaller radius I figured the larger the circle the greater area/(perimeter*perimeter) should be, but that doesn't seem to be the case and I don't know why. The 'roundness' of an ideal circle is 1/4PI (0,07957747154594766788444188168626). I made a few tests with (circle like polygons) and these were my results:
radius 10 roundness 0.0760194950578244
radius 20 roundness 0.07542738445429042
radius 30 roundness 0.07549530319982335
radius 40 roundness 0.07472106160782283
radius 50 roundness 0.07490614579701928
radius 60 roundness 0.0750939048274632
radius 70 roundness 0.07502892726254591
radius 80 roundness 0.07512064515249058
radius 90 roundness 0.0752191417813967
So why does it decrease when it should increase? Can anyone help?