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

该算法的输入参数应为:

  • 循环的宽度(与最里面的循环的距离)
  • 点之间的固定距离
  • 绘制点数

  • 绘制的螺旋是阿基米德螺旋,获得的点彼此之间必须为等距

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

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

    但是,点的配置是错误的,这些点彼此之间不是等距的。

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

    最佳答案

    对于第一个近似值-这可能足以绘制足够接近的块-螺旋线是一个圆,并按比率chord / radius增大 Angular 。

    // 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的平均半径:
    // 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;
    

    为了在松散的螺旋上获得更好的结果,请使用数值迭代解决方案来找到增量值,其中计算出的距离在适当的公差范围内。

    关于algorithm - 在螺旋上画等距点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13894715/

    10-12 16:09