我需要一种算法来计算螺旋路径上的点分布。
此算法的输入参数应为:
绘制的螺旋是阿基米德螺旋,获得的点必须等距彼此。
算法应打印出单点笛卡尔坐标的序列,例如:
第1点:(0.0) 第2点:( ......,...) ........ 点N(......,...)
编程语言并不重要,所有人都非常感谢!
编辑:
我已经从这个网站获得并修改了这个例子:
//
//
// centerX-- X origin of the spiral.
// centerY-- Y origin of the spiral.
// radius--- Distance from origin to outer arm.
// sides---- Number of points or sides along the spiral's arm.
// coils---- Number of coils or full rotations. (Positive numbers spin clockwise, negative numbers spin counter-clockwise)
// rotation- Overall rotation of the spiral. ('0'=no rotation, '1'=360 degrees, '180/360'=180 degrees)
//
void SetBlockDisposition(float centerX, float centerY, float radius, float sides, float coils, float rotation)
{
//
// How far to step away from center for each side.
var awayStep = radius/sides;
//
// How far to rotate around center for each side.
var aroundStep = coils/sides;// 0 to 1 based.
//
// Convert aroundStep to radians.
var aroundRadians = aroundStep * 2 * Mathf.PI;
//
// Convert rotation to radians.
rotation *= 2 * Mathf.PI;
//
// For every side, step around and away from center.
for(var i=1; i<=sides; i++){
//
// How far away from center
var away = i * awayStep;
//
// How far around the center.
var around = i * aroundRadians + rotation;
//
// Convert 'around' and 'away' to X and Y.
var x = centerX + Mathf.Cos(around) * away;
var y = centerY + Mathf.Sin(around) * away;
//
// Now that you know it, do it.
DoSome(x,y);
}
}
但是点的倾向是错误的,这些点彼此不等。
正确的分布示例是左侧的图像:
答案 0 :(得分:17)
对于第一个近似 - 这可能足以绘制足够接近的块 - 螺旋是一个圆,并按角度chord / radius
增加角度。
// value of theta corresponding to end of last coil
final double thetaMax = coils * 2 * Math.PI;
// How far to step away from center for each side.
final double awayStep = radius / thetaMax;
// distance between points to plot
final double chord = 10;
DoSome ( centerX, centerY );
// For every side, step around and away from center.
// start at the angle corresponding to a distance of chord
// away from centre.
for ( double theta = chord / awayStep; theta <= thetaMax; ) {
//
// How far away from center
double away = awayStep * theta;
//
// How far around the center.
double around = theta + rotation;
//
// Convert 'around' and 'away' to X and Y.
double x = centerX + Math.cos ( around ) * away;
double y = centerY + Math.sin ( around ) * away;
//
// Now that you know it, do it.
DoSome ( x, y );
// to a first approximation, the points are on a circle
// so the angle between them is chord/radius
theta += chord / away;
}
然而,对于更松散的螺旋,你必须更准确地求解路径距离,因为空间太宽,连续点away
之间的差异与chord
相比显着:
上面的第二个版本使用基于使用theta和theta + delta的平均半径求解delta的步骤:
// take theta2 = theta + delta and use average value of away
// away2 = away + awayStep * delta
// delta = 2 * chord / ( away + away2 )
// delta = 2 * chord / ( 2*away + awayStep * delta )
// ( 2*away + awayStep * delta ) * delta = 2 * chord
// awayStep * delta ** 2 + 2*away * delta - 2 * chord = 0
// plug into quadratic formula
// a= awayStep; b = 2*away; c = -2*chord
double delta = ( -2 * away + Math.sqrt ( 4 * away * away + 8 * awayStep * chord ) ) / ( 2 * awayStep );
theta += delta;
要在松散螺旋上获得更好的结果,请使用数值迭代解决方案来查找delta的值,其中计算的距离在合适的公差范围内。
答案 1 :(得分:3)
贡献Python生成器(OP未请求任何特定语言)。它使用与Pete Kirkham的答案类似的圆近似。
arc
是沿路径所需的点距离,separation
是螺旋臂所需的分离。
def spiral_points(arc=1, separation=1):
"""generate points on an Archimedes' spiral
with `arc` giving the length of arc between two points
and `separation` giving the distance between consecutive
turnings
- approximate arc length with circle arc at given distance
- use a spiral equation r = b * phi
"""
def p2c(r, phi):
"""polar to cartesian
"""
return (r * math.cos(phi), r * math.sin(phi))
# yield a point at origin
yield (0, 0)
# initialize the next point in the required distance
r = arc
b = separation / (2 * math.pi)
# find the first phi to satisfy distance of `arc` to the second point
phi = float(r) / b
while True:
yield p2c(r, phi)
# advance the variables
# calculate phi that will give desired arc length at current radius
# (approximating with circle)
phi += float(arc) / r
r = b * phi
答案 2 :(得分:3)
在Swift中(基于liborm的回答),将三个输入作为OP请求:
func drawSpiral(arc: Double, separation: Double, var numPoints: Int) -> [(Double,Double)] {
func p2c(r:Double, phi: Double) -> (Double,Double) {
return (r * cos(phi), r * sin(phi))
}
var result = [(Double(0),Double(0))]
var r = arc
let b = separation / (2 * M_PI)
var phi = r / b
while numPoints > 0 {
result.append(p2c(r, phi: phi))
phi += arc / r
r = b * phi
numPoints -= 1
}
return result
}
答案 3 :(得分:2)
我发现这篇文章很有用,所以我添加了上面代码的Matlab版本。
function [sx, sy] = spiralpoints(arc, separation, numpoints)
%polar to cartesian
function [ rx,ry ] = p2c(rr, phi)
rx = rr * cos(phi);
ry = rr * sin(phi);
end
sx = zeros(numpoints);
sy = zeros(numpoints);
r = arc;
b = separation / (2 * pi());
phi = r / b;
while numpoints > 0
[ sx(numpoints), sy(numpoints) ] = p2c(r, phi);
phi = phi + (arc / r);
r = b * phi;
numpoints = numpoints - 1;
end
end