# Serenity of the Firefly Class

My apologies if you were hoping for an entry on the amazing but canceled-too-early TV show. But if knowing that I am a fan makes you more likely to read this blog post, then that’s awesome. For ICM class, the assignment was to write a program using objects. I designed a sketch with fireflies that dim when they approach your mouse cursor. (Click here if the embedded version doesn’t work for you.)

I am fascinated by randomness as a mathematical concept, and its relationship to the natural world. I used the random() and randomSeed() functions to approximate the the distribution of stars and grass in this scene. I also used the random() function to determine the speed and direction at which each firefly moves. I would have liked to use the actual mathematical model for swarming to determine the movement of the fireflies, but this requires some programming tools (like PVector) that I don’t quite know how to use yet. At some point I would like to redo this sketch with swarming, so it looks more natural.

Here is the main code:

```[code lang="java"]Fly[] flies;

void setup()
{
size(650, 650);

flies = new Fly;

for(int i=0; i
And this is the code for the Firefly class:
[code lang="java"]
class Fly
{
float x, y;
float xSpeed, ySpeed;

color c = color(255, 245, 134);

//constructor
Fly(int _x, int _y, float _xSpeed, float _ySpeed)
{
x = _x;
y = _y;
xSpeed = _xSpeed;
ySpeed = _ySpeed;
}

void drawFly()
{
fill(c);
ellipse(x, y, 8, 16);
fill(255, 255, 255, 127);
ellipse(x+4, y-8, 16, 6);
ellipse(x-4, y-8, 16, 6);
fill(0);
ellipse(x, y-8, 8, 8);
}

void moveFly()
{
x += xSpeed;
y += ySpeed;

println(frameCount + " " + xSpeed);

if(x > width || x < 0)
{
xSpeed *= -1;
}

if(y > 3*height/4 || y < 50)
{
ySpeed *= -1;
}

}

void dimFly()
{
if (dist(x, y, mouseX, mouseY) <=50)
{
c = color(126, 120, 3);
}
else
{
c = color(255, 245, 134);
}
}

}[/code]

```