I have an old plotter that can’t handle printing true spirals. I need help with developing an algorithm to approximate a spiral.
Here’s a LISP sub-script that I have written which uses semicircles of varying radii to draw a spiral:
(setq inner_radius 1e3) ; The spiral grows from inner radius to outer radius
(setq coil_thickness 35) ; This is the half-pitch of the spiral, pitch is 70
(setq outer_radius (+ 2.4e3 (* 2 coil_thickness)))
(while (< inner_radius outer_radius)
(setq P1 (list 0 inner_radius))
(setq P2 (list inner_radius 0))
(setq P3 (list 0 (* -1 inner_radius)))
(setq P4 (list (- (- inner_radius) coil_thickness) coil_thickness))
(setq P5 (list 0 (+ inner_radius (* coil_thickness 2))))
(command "ARC" P1 P2 P3) ; Draws a 3 point arc through P1, P2 and P3
(command "ARC" P3 P4 P5) ; Draws a 3 point arc through P3, P4 and P5
(setq inner_radius (+ inner_radius (* coil_thickness 2)))
)
I am using AutoCAD
to implement the script. The code here is a cop-out – I can implement a true spiral in AutoCAD
but the plotter, which will plot this, is dated and is not able to handle a true spiral. It can handle just circle arcs and line segments. I am seeking a method which can implement a better approximation of the spiral. For instance, I tried thinking about quartcircles (instead of the semicircles I have used in the above sub-script) but couldn’t come up with a viable scheme.
My first choice would be to just use short line segments to approximate the spiral. A spiral in polar coordinates is just:
R = a + b * Theta
Rough pseudo-code for a spiral generation using line segments should be something like:
LastPoint = (0, 0)
DeltaTheta = 0.01 //Radians, Make smaller for a finer resolution
NumTurns = 4
CONSTANT_A = 0
CONSTANT_B = 1
For Theta = 0 to NumTurns*2*PI STEP DeltaTheta
ThisR = CONSTANT_A + CONSTANT_B * Theta
ThisPoint = (ThisR * cos(Theta), ThisR * sin(Theta))
DrawLine(LastPoint, ThisPoint)
LastPoint = ThisPoint
Next Theta
If you can make the DeltaTheta
parameter small enough you should be able to get a relatively decent looking spiral. For a smoother spiral you may want to look at bezier curves, assuming your plotter supports them. Another option would be to use arbitrary arcs that match the slope of the spiral on each end.
1
Currently you are alternating the center between two points, you can improve your approximation by increasing the number of points. You are currently using two points separated from pitch/2, you can use three points in an equilateral triangle whose edge is pitch/3, four points in a square with edge pitch/4, … n points in a regular polygon with n sides whose edges are pitch/n. Your spiral will then be formed of arc of 360/n degrees with a radius increasing by pitch/n by step.