# Archimedean spiral in C++

Im trying to plot the x and y positions of an Archimedean spiral in C++.

So far I've been trying something like this, but no luck:

int dx = 0;
int dy = 0;
int x = 0;
int y = 0;

for (int i = 0; i < maxPoints; i++)
{
dx = sin(i * PI / 2);
dy = cos(-i * PI / 2);
x += dx;
y += dy;

plot(x, y);
}


I'm developing a 3D game application that demonstrates the use of the Bullet physics engine by simulating dominos. Instead of placing the dominos in the scene manually I want to use some math to do it for me :)

For anyone who is interested here it is on GitHub.

• The problem seems to be that all variables are int. In particular, dx and dy will probably get 0. – lhf Dec 22 '15 at 1:50

Figured it out :) The dominos are now being placed along the X and Y coordinates generated by the function.

The original code in the question was plotting a wave of points outwards from the centre position or origin and was not what I wanted. What I needed was for each point to follow the Archimedean spiral with a certain space between the spirals.

Initially I used integer values to store the x and y coordinates but this was causing a precision error by truncating the floating point value in order to store it in the integer data type.

The below example generates points along the spiral continuously, relative to the maxPoints value.

float x = 0;
float y = 0;
float angle = 0.0f;

// Space between the spirals
int a = 2, b = 2;

for (int i = 0; i < maxPoints; i++)
{
angle = 0.1 * i;
x = (a + b * angle) * cos(angle);
y = (a + b * angle) * sin(angle);

plot(x, y);
}


Code for the project is on GitHub, you will need Bullet and freeglut

• This answer would be even better if you explained what the problem turned out to be and what you changed to fix it... – trichoplax Dec 21 '15 at 20:25
• Ill update the question and answer now with more info. – David Dec 22 '15 at 21:22

This isn't really a direct answer to this question (that already has an answer anyway), but might interest people who want to implement this algorithm in 3D.

I had to try implementing this algorithm to generate 3D spirals in blender using Python (could easily be converted to drawing with PIL or Matplotlib in 2D). So here's the algorithm and result:

import bpy
from math import cos, sin
S = bpy.context.scene

def add_archimedian_spiral( size = 0.1, length = 500, height = 1, name = 'archispiral' ):
mesh = bpy.data.meshes.new( name = name )

o = bpy.data.objects.new(name, mesh)
o.location = (0,0,0) # place at object origin

z     = 0
verts = []
for i in range( length ):
angle = 0.1 * i
x     = ( 2 * size * angle ) * cos( angle )
y     = ( 2 * size * angle ) * sin( angle )
z    += i / 10000 * height
verts.append((x,y,z))

edges = []
for i in range( len( verts ) ):
if i == len( verts ) - 1: break
edges.append((i, i+1))

mesh.from_pydata( verts, edges, [] )

add_archimedian_spiral( size = 0.2, length = 500, height = 6 )

• Does this do the same thing except with an increase on the Z height on each iteration of the for loop? – David Dec 24 '15 at 9:37
• Pretty much, except its always symmetrical (istead of the original a and b, I used a uniform size param). – TLousky Dec 24 '15 at 9:54