9
$\begingroup$

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

Archimedean spiral

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);    
}

EDIT: More info

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.

$\endgroup$
1
  • $\begingroup$ The problem seems to be that all variables are int. In particular, dx and dy will probably get 0. $\endgroup$
    – lhf
    Commented Dec 22, 2015 at 1:50

2 Answers 2

7
$\begingroup$

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

$\endgroup$
2
  • 5
    $\begingroup$ This answer would be even better if you explained what the problem turned out to be and what you changed to fix it... $\endgroup$ Commented Dec 21, 2015 at 20:25
  • 1
    $\begingroup$ Ill update the question and answer now with more info. $\endgroup$
    – David
    Commented Dec 22, 2015 at 21:22
4
$\begingroup$

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:

enter image description here

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
    S.objects.link( o )

    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 )
$\endgroup$
2
  • $\begingroup$ Does this do the same thing except with an increase on the Z height on each iteration of the for loop? $\endgroup$
    – David
    Commented Dec 24, 2015 at 9:37
  • $\begingroup$ Pretty much, except its always symmetrical (istead of the original a and b, I used a uniform size param). $\endgroup$
    – TLousky
    Commented Dec 24, 2015 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.