FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   how to draw an ellipse
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: how to draw an ellipse  (Read 4229 times)
st33d

WWW Email
how to draw an ellipse
« on: Apr 2nd, 2005, 3:19pm »

Slim chance of this being answered on this fine sunny day in London but...
 
How do I draw an ellipse? I've looked on the web and can't find out. The code below is a sketch I'm planning to port into Flash (hence the pointless lineTo command) and link up with the microphone object with an animated character. I want the circle to be traced out in an elliptical path though. How is it done?
Code:

int trailSize = 200;
float r = 100;
float theta = 0.0;
float [] x,y;
float mx,my;
void setup(){
size(400,400);
mx = 0;
my = 0;
x = new float[trailSize];
y = new float[trailSize];
}
void loop(){
background(255);
translate(200,200);
for(int i=1; i<trailSize; i++) {  
    x[i-1] = x[i];  
    y[i-1] = y[i];  
  }
  circleTrail();
  r = abs(mouseY-200);  
  for(int i=0; i<trailSize; i++) {
  stroke(0,0,0,(255/trailSize)*i);
    lineTo(x[i], y[i]);  
}
}
void lineTo(float xt, float yt){
line(mx,my,xt,yt);
mx = xt;
my = yt;
}
void circleTrail(){
x[trailSize-1] = cos(theta)*r;
y[trailSize-1] = sin(theta)*r;
theta = (theta+0.05)%TWO_PI;
}
 

I could murder a pint.
amoeba

WWW
Re: how to draw an ellipse
« Reply #1 on: Apr 2nd, 2005, 4:26pm »

Well, if you only want a non-rotated ellipse, then you simply multiply your X and Y values with the appropriate scaling:
 
Code:
x[trailSize-1] = cos(theta)*r*xscale;
y[trailSize-1] = sin(theta)*r*yscale;

 
If you want a rotated ellipse you first calculate the points as above, and then rotate the points afterwards. There is probably a more general math equation for it, but that would do the trick. I wrote a Vec2D for workshops, you can copy the code for rotating a point from there...  
 
Vec2D class
 

marius watz // amoeba
http://processing.unlekker.net/
st33d

WWW Email
Re: how to draw an ellipse
« Reply #2 on: Apr 2nd, 2005, 11:51pm »

Thanks. I've since realised that my character design would be best suited to director so I'm sketching in 3D and Processing's transformations are handling everything now. Thanks also for the vector class, it's given me an idea for some math approaches.
 
In the rare case that someone wants to know what the final sketch before moving to director is (and I'm going to have to learn lingo for the first time - what fun) it's a robot head firing a laser based on sound input. With no sound input he cuts a hole below himself and falls through the floor.
 
http://www.st33d.net/misc/index.html
 
It's nice to work this out before hand in a language that remotely makes sense to me.
« Last Edit: Apr 3rd, 2005, 1:17am by st33d »  

I could murder a pint.
Pages: 1 

« Previous topic | Next topic »