如果我有一个点列表
顺时针方向是指相对于形状中心的顺时针方向。
[(x1,y1), (x2,y2), (x3,y3)...]
,是否有任何方法可以将它们排列成顺时针方向?顺时针方向是指相对于形状中心的顺时针方向。
[(x1,y1), (x2,y2), (x3,y3)...]
,是否有任何方法可以将它们排列成顺时针方向?首先,你需要找到由你的点定义的形状的中心,因为旋转将相对于该点进行定义。
然后,你需要计算点与中心以及 x
轴之间的角度。
要计算角度,可以使用 Math.atan2(y - center.y, x - center.x)
。
接下来,使用 Array.sort
按角度对点进行排序。
一旦点正确排序,你应该能够绘制连接点且不相交的线。我使用画布制作了一个演示。起始点用正方形表示。我已经绘制了以你的点质心为原点的 x/y
轴。我还添加了连接点到质心的线条,以显示它们的角度。
Original Answer 翻译成 "最初的回答"。
const width = 250;
const height = 250;
// Random points
const points = Array.from({ length: 20 }, () =>
({ x: Math.random() * width, y: Math.random() * height })
);
// Get the center (mean value) using reduce
const center = points.reduce((acc, { x, y }) => {
acc.x += x / points.length;
acc.y += y / points.length;
return acc;
}, { x: 0, y: 0 });
// Add an angle property to each point using tan(angle) = y/x
const angles = points.map(({ x, y }) => {
return { x, y, angle: Math.atan2(y - center.y, x - center.x) * 180 / Math.PI };
});
// Sort your points by angle
const pointsSorted = angles.sort((a, b) => a.angle - b.angle);
// Draw them
const canvas = document.querySelector('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
let lastPoint = pointsSorted[0];
ctx.fillRect(lastPoint.x, lastPoint.y, 5, 5);
ctx.beginPath();
ctx.moveTo(0, center.y);
ctx.lineTo(width, center.y);
ctx.strokeStyle = 'black';
ctx.stroke();
ctx.beginPath();
ctx.moveTo(center.x, 0);
ctx.lineTo(center.x, height);
ctx.strokeStyle = 'black';
ctx.stroke();
pointsSorted.forEach(({ x, y }) => {
ctx.beginPath();
ctx.moveTo(lastPoint.x, lastPoint.y);
ctx.lineTo(x, y);
ctx.strokeStyle = 'red';
ctx.stroke();
ctx.fillRect(x, y, 2, 2);
ctx.beginPath();
ctx.moveTo(center.x, center.y);
ctx.lineTo(x, y);
ctx.strokeStyle = 'grey';
ctx.stroke();
lastPoint = { x, y };
});
canvas {
border: 1px solid black;
}
<canvas></canvas>