xxxxxxxxxx
// Global variables
Eye leftEye;
Eye rightEye;
float eyeOffset = 220;
void setup ()
{
size (800, 400);
smooth();
background(255);
Point p1 = new Point (width/4, height/2);
Point p2 = new Point (width/4+eyeOffset, height/2);
leftEye = new Eye(p1, 200, color(50,200,250));
rightEye = new Eye(p2, 200, color(50,200,250));
}
void draw()
{
background(255);
leftEye.drawEye();
rightEye.drawEye();
}
class Eye
{
// Instance variables/data
private Point loc;
private float eyeSize;
private color c;
// Constructors
Eye (Point locPoint, float sizeValue, color cValue)
{
loc = locPoint;
eyeSize = sizeValue;
c = cValue;
}
// Methods
void setLoc (Point p)
{
loc = p;
}
Point getLoc ()
{
return loc;
}
void setEyeSize (float sizeValue)
{
eyeSize = sizeValue;
}
float getEyeSize ()
{
return eyeSize;
}
void setColor (color eyeColor)
{
c = eyeColor;
}
color getColor ()
{
return c;
}
void drawEye()
{
pushMatrix();
fill(250);
stroke(0);
translate (loc.x, loc.y); // move origin to center of eye location
ellipse (0, 0, eyeSize, eyeSize); // draw outer eye
// draw pupil
fill (c); // set eye color
if (dist(loc.x,loc.y,mouseX,mouseY) < eyeSize/4)
{
ellipse (mouseX-loc.x,mouseY-loc.y, eyeSize/2, eyeSize/2);
fill (0);
ellipse (mouseX-loc.x,mouseY-loc.y,eyeSize/5,eyeSize/5);
}
else
{
// rotate matrix by angle of mouse relative to eye
rotate(atan2(mouseY-loc.y,mouseX-loc.x));
ellipse (eyeSize/4.2, 0, eyeSize/2, eyeSize/2); // draw eye
fill (0);
ellipse (eyeSize/4.2, 0, eyeSize/5, eyeSize/5); // draw pupil
}
popMatrix();
}
}
class Point
{
private float x;
private float y;
Point (float xValue, float yValue)
{
x = xValue;
y = yValue;
}
void setPoint (float xValue, float yValue)
{
x = xValue;
y = yValue;
}
float getX ()
{
return x;
}
float getY()
{
return y;
}
}