对于第一个近似值 - 这对于绘制足够近的块来说可能足够好 - 螺旋是一个圆,并按比例增加角度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;
}
However, for a looser spiral you will have to solve the path distance more accurately as spaces too wide where the difference between away
for successive points is significant compared with 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 值,其中计算的距离在合适的公差范围内。