我很难将这个优雅地融入算法中。
所以我给出了一个直边形状(即正方形,但最终形状与端点无关)。我在笛卡尔坐标系上得到了边界端点:(2,-2)(2,2)(-2,2)( - 2,-2)
我给出了任意数量的点(即7),我想沿着形状的边缘(在这种情况下是正方形)均匀地展开这些点(x,y)。
我目前的想法是获取所有端点的总长度,将其除以获得段长度的点数(然后我对边缘进行标准化)。然后我从端点到端点找到这个量之间的点并累积规范化的切片,当这个总数超过1.0时,我迭代端点并取出余数并从那里开始...或类似的东西。
有人可以帮助我把它放入算法(最好是C#),或者如果你有更好的解决方案,请告诉我。我想有一个排序或分配/分区算法可能会产生相同的影响,但我找不到任何。我希望这不是显而易见的。
答案 0 :(得分:2)
这有多普遍?另外,如何表示你的形状和分数?你的算法似乎没问题;你需要帮助把它变成代码吗?
好的,这是我想出的东西。 关于代码的注释:
我正在使用Point类来表示向量。
我没有对此进行测试,因此可能存在错误。此算法如何处理精确区域可能存在问题(例如,正方形上有4个点的正方形)。如果有问题或者您有任何疑问,请告诉我们! :)
Point[] shapePoints; //already initialized
int numPoints; //already initialized
Point[] retPoints = new Point[numPoints];
int totalLength;
for(int i = 1; i < shapePoints.length; i++){
totalLength += distance(shapePoints[i], (shapePoints[i-1]));
}
float segLength = ((float) totalLength) / numPoints);
Point currShape = shapePoints[0];
Point nextShape = shapePoints[1];
Point prev = currShape;
int counter = 2;
while(numPoints > 0){
Point norm = normalize(new Point(nextShape.x - currShape.x, nextShape.y - currShape.y));
if(distance(nextShape, prev) < segLength){
int tempLength = segLength;
tempLength -= distance(nextShape, prev);
currShape = nextShape;
nextShape = shapePoints[counter];
counter ++;
norm = normalize(new Point(nextShape.x - currShape.x, nextShape.y - currShape.y));
norm.multiply(tempLength);
}
else{
norm.multiply(segLength);
}
retPoints[numPoints - 1] = norm;
prev = retPoints[numPoints - 1];
numPoints --;
}
Point normalize(Point p){
int scale = Math.sqrt(p.x * p.x + p.y * p.y);
p.x = p.x / scale;
p.y = p.y / scale;
return p;
}