Orbit fast and orbit slow...
in
Programming Questions
•
1 years ago
Hi guys;
I am trying to do a program to simulate an ordit of a planet around a sun. My code work, but why it going slow and sudden speed-up ( reach near the top of the orbit ) and slow and speed-up ( reach near the bottom of the orbit ) and slow.....and repeat. The debug show me no problem in the calculation and the co-ordinates values to draw the orbit. Slow and Fast ... ? <-- I wonder why ? My computer is a 3 GHz Pentium Dual-Core... 1 GB Ram... humm ?
Anybody have any idea ?
The formula I use is from a math book and it is the circle formula. I use a 4 quadrant system ( use a switch() - case ) to figure out which quadrant my orbit is, so I simply count 0 to 50 and 50 to 0 and the quadrant is the state.
Here my code :
- int x;
int y;
int r; // the radius of the orbit
int xtemp; // the x and y to draw the planet on the screen
int ytemp;
float corx; // a float x - int state; // 4 state = 4 quadrant - think the x axis ( +/- ) and y axis ( +/- )
- void setup()
{
size(400,400); - // init value
r=50;
y=0;
xtemp=250;
ytemp=200;
state=1;
}
void draw()
{
// The println is for debug
println(xtemp+" "+ytemp+" "+x+" "+y);
background(255);
// draw a sun
ellipse(200,200,50,50);
// draw and calculate the planet position
incircle();
ellipse(xtemp,ytemp,5,5);
}- // calculate the orbit position
void incircle()
{
corx=sqrt(sq(r)-sq(y));
x=(int)corx; - // in which quadrant is the planet is
switch(state)
{
case 1 : - // x+ y+
plusplus();
break;
case 2 : - // x- y+
negplus();
break;
case 3 : - // x- y-
negneg();
break;
case 4 : - // x+ y-
plusneg();
break;
}
} -
void plusplus()
{
ytemp=200+y;
xtemp=200+x;
y++;
if (y==51)
{
state=2;
y=49;
}
}
void negplus()
{
ytemp=200+y;
xtemp=200-x;
y--;
if (y==-1)
{
state=3;
y=1;
}
}
void negneg()
{
ytemp=200-y;
xtemp=200-x;
y++;
if (y==51)
{
state=4;
y=49;
}
}
void plusneg()
{
ytemp=200-y;
xtemp=200+x;
y--;
if (y==-1)
{
state=1;
y=1;
}
}
1