PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

spiral

hello
Anyone know how to create a Ellipse or spiral function?
I just want to draw a spiral on screen,but not a "real" calculated one that is to slow i think
thanks in advance
Try http://membres.lycos.fr/abrobecker/text/circle.htm

This algorithm is for a circle routine, but if I understand it correctly, it should be able to produces ellipses or spirals with few modifications.

BTW, his site has a couple other interesting articles (written with RiscOS machines in mind, but since they use ARM processors even the assembly stuff could be useful).
laurnet · 27 Apr 2002, 16:07 UTC — perfect circle
I tried to translate to c,
it looks like this:
but don't seem to work..


Code:
void circle(int x,int y,int r)
{
int dx=r;
int dist;
int distmax;
int dy;
// if(dx=0) return;
dist=r*r;
distmax=dist+r; //:REMshould be (r%+0.5)^2=r%^2+r%+0.25
line(x-dx,y,x+dx,y,31); // :REMdraw central line
dist+=1;
dy=1;
while(dy<=r)
{
while(dist>distmax)
{
dist+=-2*dx+1 ; //:REMdist%=(dx%-1)^2+dy^2=dist%-2*dx%+1
dx-=1;
}


line(x-dx,y+dy,x+dx,y+dy,31); //:REMdraw double lines
line(x-dx,y-dy,x+dx,y-dy,31);
dist+=2*dy+1 ; // :REMdist%=dx%^2+(dy+1)^2=dist%+2*dy%+1
dy+=1;
}
}
The code you posted seemed to work.

Anyway, try this:
Code:

void circle(int x, int y, int r)
{
int dx=r;
int dy=0;
int r2=r*r;
int x2=r2;
int y2=0;
while (dx>dy)
{
plot(x+dx,y+dy,0xFFFF);
plot(x+dx,y-dy,0xFFFF);
plot(x-dx,y+dy,0xFFFF);
plot(x-dx,y-dy,0xFFFF);
plot(x+dy,y+dx,0xFFFF);
plot(x+dy,y-dx,0xFFFF);
plot(x-dy,y+dx,0xFFFF);
plot(x-dy,y-dx,0xFFFF);
dy--;
y2-=(dy<<1);
while (x2+y2 > r2)
{
dx--;
x2-=dx<<1;
}
}
}


Basically, this works the same way:
1. Start out at the right side of the circle
2. draw with symmetry
3. go up
4. does the increased y value make us too far from the center?
5. if yes, go left until it doesn't
6. repeat until y=x (the rest of the circle can be found using symmetry).

A couple notes, instead of squaring x and y each time, we just integrate the derivitive (if f(x)=x^2, then f'(x)=2*x, so instead of calculating x^2, we add 2*x to the last value).

If you want to draw something with a different radius, you will have to either:
a.) if you are scaling along an axis, split the loop into two, one for the first octant, and one for the second, use the symmetry of this to draw the other three quadrants.
b.) if the scaling is non-uniform, but the shape would still be a conic section (i.e. it is representably as a*x^2+b*x*y+c*y^2=r^2), you should be able to use the same theory to create a function for it (Some optimizations wouldn't be possibly). This may or may not be worth the extra trouble.
c.) if the scaling factor is not on an axis, you will have to find a way of figuring out the scale for x and y based on the x and y values of the current point. (the angle doesn't change at a constant rate, but you may be able to work out an approximation to atan(y/x) using calculus, but that probably wouldn't be worth the air you breath while doing it)
laurnet · 28 Apr 2002, 23:59 UTC — circle
I was using line function without Floating points..
like lewill :-)
your circle code work fast,
but i am trying to use randint on it and it bugs ,is that a floating point error?
Well I am trying to find how to make a elipse with what you told me .(but it seems too much math oriented for me..)
huhu that's what i thought looking at your code whith line in it :p - easyCE 1.6 trap ! -
ok this is the worst ellipse tracer i've ever made (but the only one huhu) it has advantages: u can change the function to trace a spiral or a "lissajoux"... and is easy to understand (?)

first u will have to writedown a FPcos and FPsin like this

Code:

// ** global header
int fpCos[360];
int fpSin[360];

void mathstuffinit()
{
for ( i=0; i<360; i++)
{
fpCos[i] = FP( cos( i * 2 * PI / 360 ) );
fpSin[i] = FP( sin( i * 2 * PI / 360 ) );
}
}


all my math stuff is somewhere in a single unit so i call this through functions:

Code:

int FPcos( int angle )
{
return( fpCos[ angle % 360 ] );
}
int FPsin( int angle )
{
return( fpSin[ angle % 360 ] );
}


ok that was the basic math stuff which is very usefull i think..

and this is the awfull ellipse code:

Code:

void ellipse(int x, int y, int r1, int r2, unsigned short col)
{
int px, py, oldpx, oldpy;
oldpx = IFP(x) + FPMUL( FPcos( 0 ), IFP(r1) );
oldpy = IFP(y) + FPMUL( FPsin( 0 ), IFP(r2) );
for (int angle = 1; angle <361; angle+=5)
{
//*** this is your function
px = IFP(x) + FPMUL( FPcos( angle ), IFP(r1) );
py = IFP(y) + FPMUL( FPsin( angle ), IFP(r2) );
line( oldpx, oldpy, px, py, col );
oldpx = px;
oldpy = py;
}
}


ok i'm not proud of it but this should give u ideas

oh by the way if someone can explain me why in
Code:

#define FP(x) ((int)((x)*FP_ONE+0.5f))

is there a + 0.5f ?!???
The +0.5 is a rounding thing. Because of the way the ISO standard for floating point describes rounding, if you round to an int, it will always round down. If you add 0.5, anything that would round up using "real" rounding will end up with one added to the integer part, and the operation will occur correctly.

An interesting side note, this is the only function (round-to-int) that ISO says should round down. This causes problems on an x86 processor, because the compiler generates code that tells the FPU to change rounding modes, and as a result, it completely screws with the pipeline. The only way around this is to write an inline assembly function that rounds without setting the rounding mode.

I think the rounding issue occurs with int-to-float too.
thx again Bill, very didactic post !
the more you explain, the more i feel newbie & the more i want to learn :')
Code:
int fpCos[360];
int fpSin[360];


Why not pretend there are 512 "degrees" in the circle / sin+cos tables? Have the rotation going from 0 to 511 and the modulus becomes much more optimisable (bitwise AND). Just scale your input and table values accordingly. :D

If you want to save space, you only have to store 768 entries 'cos COS starts half way through SIN, too :) You could do also do it with only a SIN table, of course, but that starts to make the code messier.

(written with RiscOS machines in mind, but since they use ARM processors even the assembly stuff could be useful)


My RiscOS machine has a StrongARM... which is handy for a development/theory testing environment. :D

Cheers,

Refractor
laurnet · 02 May 2002, 21:41 UTC — spi
lewill i'll try your ellipse code even if it is your worst ..
but i think using sin and cos will be too slow,or I'll have to precalculate and store in another buffer
thanks for helping
if you look closer, you will see that cos and sin are precalculated, and in FP if you please :roll: