Actually, it is pretty simple to draw basic archimedean spiral. The mathematical formula for archimedean spiral is
r = a + bθ,
with r is radius at each spiral point, a is offset of starting point of spiral and b is the coefficient of angle θ. Then for the point at spiral, it is the same like parametric circle
x coordinate = r * cos(theta) and
y coordinate = r * sin(theta).
Here is my code implementation of simple basic archimedean spiral.
#include<stdio.h>
#include<math.h>
#define M_PI 3.14159265358979323846
//spiral definition r = a + bθ
void main()
{
int numTurns = 5; //number of spiral cycle
double stepover = 0.1; //distance between one spiral cycle
double distanceBetweenTurns = stepover * (1/(2*M_PI)); //b 2*PI is 360deg
double theta = 0.0; //starting angle, all in radian
double offset = 0.0; //a (offset of starting point)
double pointsPerTurn = 20; //number of point in each spiral cycle
printf("rad, theta, r, x, y\n");
for(int i=0; i<(pointsPerTurn*numTurns+1); i++)
{
double r = offset + (distanceBetweenTurns*theta);
double point[2];
point[0] = r * cos(theta);
point[1] = r * sin(theta);
//print out the point to be plotted
printf("%f, %f, %f, %f, %f\n",theta, theta*(180.0/M_PI), r, point[0], point[1]);
theta += 2*M_PI / pointsPerTurn;
}
}
After running the code, it will print out the angle (radian), angle (degree), r of each point(interpreted as 1/curvature), x and y coordinate of each point.
The plotted spiral from the code in excel is as shown bellow.
Pretty simple right!
*NB bonus:This kind of spiral are used for making spiral toolpath. Usually, projected onto target surface.
But, of course we have to modify the point at the spiral, because distance between each point gets larger and larger as the spiral grow. For this I am going to save the code for the next post.
No comments