Cleaned up calculateCenter function

While I still don't understand what it's doing exactly, I noticed that
the 2 iterations of the loop are unnecessary, and afterwards found out
that the change to area is equal to f. Furthermore I replaced the `~~`
flooring with Math.floor for better readability, since this function
get's executed once per submission and the speed benefits of `~~` are
neglectable.
This commit is contained in:
Fabian Wunsch 2022-04-09 21:04:08 +02:00
parent 733decc20f
commit bf834a6565

View file

@ -231,33 +231,25 @@ function initDraw(){
function calculateCenter(path){
var area = 0,
i,
j,
point1,
point2;
i,
j,
point1,
point2,
x = 0,
y = 0,
f;
for (i = 0, j = path.length - 1; i < path.length; j=i,i++) {
point1 = path[i];
point2 = path[j];
area += point1[0] * point2[1];
area -= point1[1] * point2[0];
}
area *= 3;
var x = 0,
y = 0,
f;
for (i = 0, j = path.length - 1; i < path.length; j=i,i++) {
point1 = path[i];
point2 = path[j];
f = point1[0] * point2[1] - point2[0] * point1[1];
area += f;
x += (point1[0] + point2[0]) * f;
y += (point1[1] + point2[1]) * f;
}
area *= 3;
for (i = 0, j = path.length - 1; i < path.length; j=i,i++) {
point1 = path[i];
point2 = path[j];
f = point1[0] * point2[1] - point2[0] * point1[1];
x += (point1[0] + point2[0]) * f;
y += (point1[1] + point2[1]) * f;
}
return [~~(x / area)+0.5, ~~(y / area)+0.5];
return [Math.min(x / area)+0.5, Math.max(y / area)+0.5];
}
function undo(){