Arctangent.

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);
  noStroke();
  e1 = new Eye( 250,  16, 120);
  e2 = new Eye( 164, 185,  80);  
  e3 = new Eye( 420, 230, 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, 204, 0);
    ellipse(size/4, 0, size/2, size/2);
    popMatrix();
  }
}

Functions Used

fill()

Sets the color used to fill shapes

Learn More
size()

Defines the dimension of the display window width and height in units of pixels

Learn More
ellipse()

Draws an ellipse (oval) to the screen

Learn More
pushMatrix()

Pushes the current transformation matrix onto the matrix stack

Learn More
popMatrix()

Pops the current transformation matrix off the matrix stack

Learn More
setup()

The setup() function is run once, when the program starts

Learn More
rotate()

Rotates the amount specified by the angle parameter

Learn More
translate()

Specifies an amount to displace objects within the display window

Learn More
draw()

Called directly after setup(), the draw() function continuously executes the lines of code contained inside its block until the program is stopped or noLoop() is called

Learn More
noStroke()

Disables drawing the stroke (outline)

Learn More
atan2()

Calculates the angle (in radians) from a specified point to the coordinate origin as measured from the positive x-axis

Learn More
background()

The background() function sets the color used for the background of the Processing window

Learn More