在螺旋上绘制等距点

Giu*_*cci 24 algorithm algebra spiral draw

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

该算法的输入参数应为:

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

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

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

第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);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是点的倾向是错误的,这些点彼此之间并不相等.

具有非等距分布的螺旋

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

Sirals

Pet*_*ham 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;
}
Run Code Online (Sandbox Code Playgroud)

10线圈螺旋

但是,对于更松散的螺旋线,您必须更准确地求解路径距离,因为空间太宽,away连续点之间的差异显着,与chord: 1线圈螺旋1近似 1线圈螺旋第2近似

上面的第二个版本使用基于使用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;
Run Code Online (Sandbox Code Playgroud)

为了在松散螺旋上获得更好的结果,使用数值迭代解决方案来查找delta的值,其中计算的距离在合适的公差范围内.


lib*_*orm 5

提供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
Run Code Online (Sandbox Code Playgroud)


sto*_*fen 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
}
Run Code Online (Sandbox Code Playgroud)