makeConvexPolygon = n => {
const xs = [], ys = [];
for (let i = 0; i < n; i++) {
xs.push(Math.random());
ys.push(Math.random());
}
xs.sort();
ys.sort();
const minX = xs[0], maxX = xs[n-1];
const minY = ys[0], maxY = ys[n-1];
let lastTop = minX, lastBot = minX;
let lastLeft = minY, lastRight = minY;
const vx = [], vy = [];
for (let i = 1; i < n - 1;i++) {
if (Math.random() < 0.5) {
vx.push(xs[i] - lastTop);
lastTop = xs[i];
} else {
vx.push(lastBot - xs[i]);
lastBot = xs[i];
}
if (Math.random() < 0.5) {
vy.push(ys[i] - lastLeft);
lastLeft = ys[i];
} else {
vy.push(lastRight - ys[i]);
lastRight = ys[i];
}
}
vx.push(maxX - lastTop);
vx.push(lastBot - maxX);
vy.push(maxY - lastLeft);
vy.push(lastRight - maxY);
d3.shuffle(vy);
const vec = [];
for (let i = 0; i < n; i++) {
vec.push([vx[i], vy[i]]);
}
vec.sort((a, b) => Math.atan2(a[1],a[0]) - Math.atan2(b[1],b[0]));
const points = [];
let x = 0, y = 0, mpx = 0, mpy = 0;
for (let i = 0; i < n; i++) {
points.push([x, y])
x += vec[i][0];
y += vec[i][1];
mpx = Math.min(mpx, x);
mpy = Math.min(mpy, y);
}
const xShift = minX - mpx, yShift = minY - mpy;
for (let i = 0; i < n; i++) {
points[i][0] += xShift;
points[i][1] += yShift;
}
return points;
}