26

我需要一种算法来计算螺旋路径上的点分布。

该算法的输入参数应为:

  • 环的宽度(距最内环的距离)
  • 点之间的固定距离
  • 要绘制的点数

要绘制的螺旋线是阿基米德螺旋线,获得的点必须彼此等距

该算法应打印出单点笛卡尔坐标的序列,例如:

第 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);
    }
}

但是点的配置是错误的,点之间的距离不是等距的。

非等距分布的螺旋

正确的分布示例是左边的图像:

西拉尔

4

4 回答 4

23

对于第一个近似值——这对于绘制足够接近的块来说可能已经足够好了——螺旋线是一个圆,并按比例增加角度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;
}

10线圈螺旋

但是,对于较松散的螺旋,您必须更准确地求解路径距离,因为空间太宽,其中away连续点之间的差异与 相比显着chord1 线圈螺旋 1 次逼近 1线圈螺旋第二近似

上面的第二个版本使用基于使用 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 值。

于 2012-12-16T12:12:50.823 回答
8

贡献一个 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
于 2014-12-17T15:08:01.143 回答
5

在 Swift 中(基于 liborm 的回答),按照 OP 的要求获取三个输入:

func drawSpiral(arc: Double, separation: Double, 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 * Double.pi)
  var phi = r / b

  var remaining = numPoints
  while remaining > 0 {
      result.append(p2c(r: r, phi: phi))
      phi += arc / r
      r = b * phi
      remaining -= 1
  }
  return result
}
于 2016-02-17T16:00:50.857 回答
3

我发现这篇文章很有用,所以我添加了上述代码的 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
于 2017-10-25T13:11:03.873 回答