Title: Draw a logarithmic spiral in C#
The post Draw an Archimedes spiral in C# uses the equation r = A˙θ to generate the points on a spiral. This example is almost exactly the same except it uses the equation to r = A˙eB˙θ to generate its points.
That equation leads to another small issue. When θ is 0, the eB˙θ becomes 1 so r = A. That means the spiral isn't at its center point.
You can generate points closer to the center if you consider negative values for θ. The question then becomes, "How small does θ need to be?"
If we set θ small enough to make r equal to 0.1, then the resulting point will be less than one pixel away from the spiral's center. To do that, we solve the equation 0.1 = A˙eB˙min_theta for min_theta. The result is min_theta = ln(0.1 / A) / B.
The new example is almost exactly the same as the previous example's version. The only real change is in the GetSpiralPoints method, which uses the following code fragment to generate points on a spiral.
// Return points that define a spiral.
private List GetSpiralPoints(...)
for (float theta = min_theta; ; theta += dtheta)
// Calculate r.
float r = (float)(A * Math.Exp(B * theta));
// If we have gone far enough, stop.
if (r > max_r) break;
This code calculates min_theta as described above. It then makes variable theta loop starting from min_theta. The code uses the new formula to calculate r.
As in the previous example, the loop continues until the calculated value for r exceeds the maximum necessary value, at which point the method breaks out of its loop and returns the spiral's points.
The rest of the example is the same as the previous one.
Download the example to experiment with it and to see additional details.