按顺时针方向排序点
Sorting points in a clockwise direction
如果我有一个点列表 [(x1,y1), (x2,y2), (x3,y3)...]
有什么办法可以让它们按顺时针方向排列吗?
顺时针方向是指相对于形状中心的顺时针方向。
首先,您必须找到由您的点定义的形状的中心,因为旋转将相对于该点定义。
然后你必须计算点相对于中心和 x
轴的角度。
要计算角度,可以使用 Math.atan2(y - center.y, x - center.x)
.
然后使用 Array.sort
.
按角度对点进行排序
正确排序点后,您应该能够绘制一条连接点且不与自身相交的线。我使用 canvas 制作了一个演示。起点用正方形显示。我画了 x/y
轴翻译成你的点的质心。我还添加了将点连接到质心的线以具体化它们的角度。
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>
希望对您有所帮助!
如果我有一个点列表 [(x1,y1), (x2,y2), (x3,y3)...]
有什么办法可以让它们按顺时针方向排列吗?
顺时针方向是指相对于形状中心的顺时针方向。
首先,您必须找到由您的点定义的形状的中心,因为旋转将相对于该点定义。
然后你必须计算点相对于中心和 x
轴的角度。
要计算角度,可以使用 Math.atan2(y - center.y, x - center.x)
.
然后使用 Array.sort
.
正确排序点后,您应该能够绘制一条连接点且不与自身相交的线。我使用 canvas 制作了一个演示。起点用正方形显示。我画了 x/y
轴翻译成你的点的质心。我还添加了将点连接到质心的线以具体化它们的角度。
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>
希望对您有所帮助!