I want to create a function that generates a polygon with N
edges and with different size L1
for even edges and L2
for odd edges.
This is my code so far:
function generatePolygon(n, L1, L2) {
let longerEdges = Math.ceil(n / 2);
let shorterEdges = n - longerEdges;
let Lavg = (L1 * longerEdges + L2 * shorterEdges) / n;
const r = Lavg / (2 * Math.sin(Math.PI / n));
let startAngleRad = Math.PI * (90 - (2 - 1) * 360 / (2 * n)) / 180;
let angle = 0;
let vertices = [];
for (let i = 0; i < n; i++) {
let x = Math.round((r * Math.cos(angle + startAngleRad)) * 10) / 10;
let y = Math.round((r * Math.sin(angle + startAngleRad)) * 10) / 10;
vertices.push([x, y]);
if ((i + 1) % 2 === 0) {
angle += 2 * Math.PI * L1 / (Lavg * n);
} else {
angle += 2 * Math.PI * L2 / (Lavg * n);
}
}
return vertices;
}
It works ok but i now want to have the base (bottom edge) of the polygon always perfectly horizontal.
This is the result of running my function with N = 10
, L1 = 180
, L2 = 144
:
The problem as you can see is that the base is not perfectly horizontal.
I should also mention that I want the first or the last edges to be the base and I also need this function to work with any N
.