Files
processing4/java/examples/Basics/Math/Arctangent/Arctangent.pde
2011-08-05 18:26:51 +00:00

62 lines
925 B
Plaintext

/**
* Arctangent.
*
* Move the mouse to change the direction of the eyes.
* The atan2() function computes the angle from each eye
* to the cursor.
*/
Eye e1, e2, e3;
void setup()
{
size(640, 360);
smooth();
noStroke();
e1 = new Eye( 190, 16, 120);
e2 = new Eye( 164, 185, 80);
e3 = new Eye( 390, 200, 220);
}
void draw()
{
background(102);
e1.update(mouseX, mouseY);
e2.update(mouseX, mouseY);
e3.update(mouseX, mouseY);
e1.display();
e2.display();
e3.display();
}
class Eye
{
int x, y;
int size;
float angle = 0.0;
Eye(int tx, int ty, int ts) {
x = tx;
y = ty;
size = ts;
}
void update(int mx, int my) {
angle = atan2(my-y, mx-x);
}
void display() {
pushMatrix();
translate(x, y);
fill(255);
ellipse(0, 0, size, size);
rotate(angle);
fill(153);
ellipse(size/4, 0, size/2, size/2);
popMatrix();
}
}