We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › How do I graph a circle smoothly
Page Index Toggle Pages: 1
How do I graph a circle smoothly? (Read 610 times)
How do I graph a circle smoothly?
Oct 25th, 2009, 7:34pm
 
Here I have a cheep simulation of the moon orbiting the earth by graphing the location of the moon using the equation of an ellipse. My problem is that toward the edges the moon "speeds up" and then slows down.

How do I make it so that an image moves in a circular path smoothly?



/**Ellipse
*
*Created By: David A. Perez
*
*Notes:
*h = x of center point
*k = y of center point
*a = x +- a
*b = y +- b
*
*blank.x = h - a;
*/

PImage sun;
PImage moon;
PImage earth;
orbit a;

void setup(){
   sun = loadImage("sun.jpg");
   moon = loadImage("moon.gif");
   earth = loadImage("earthWest.gif");
   
   size(700, 600);
   frameRate(100);
   noStroke();
 
   a = new orbit();
      a.a = 200;
      a.b = 75;
      a.h = 500;
      a.k = 400;
      a.x = 500 - 100;
   }
   
void draw(){
   background(sun);
   a.orbit();  
}

class orbit{
   float a, b, h, k, x;
   int direction;
   
   void orbit(){
      fill(255,0,0);
     
      if(x == h-a){direction = 1;}
      else if(x == h+a){direction = 0;}
     
      if(direction == 0){float y = k - sqrt(sq(b) - sq(b)*sq(x - h)/sq(a));
                              image(moon, x, y, 50, 50);
                              x--;}
      image(earth, 500-100, 400-125, 250, 250);
      if(direction == 1){float y = k + sqrt(sq(b) - sq(b)*sq(x - h)/sq(a));
                         image(moon, x, y, 50, 50);
                         x++;}
   }
}
Re: How do I graph a circle smoothly?
Reply #1 - Oct 25th, 2009, 11:06pm
 
Note 1: Put size() call at the start of setup().
Note 2: Why don't you use sin() and cos() calls to compute the orbit?
Note 3: This is not really a "syntax" question (like, roughly, 70% of questions asked in this section...).
Page Index Toggle Pages: 1