using loops in processing

This project i did together with Hugo de Lucena (www.hugoluc.com),,

we decided to use sine function to code sperms trying to reach a ovary, and to make it more interactive something happen when you put the mouse inside the ovary.

The following examples help us to develop this code:

Daniel Schiffman sine wave (http://processing.org/examples/sinewave.html)

and example 5-16 (page 66) ¬†“The bounds of a Circle” from the first edition of the book Getting Started with Processing¬†of Casey Reas & Ben Fry

Here is the processing code :


// ovary position
int x = 400;
int y = 300;

float radius = 20; // radius of the tail circles
float angle = 0.0;
float anglevel = 0.2;
float amplitude = 10; // amplitud of the tail sine wave
// heads of the sperms
float headx= 200;
float headx2= 300;
int a=10;
void setup() {
size(800, 600);
smooth();
ellipseMode(RADIUS);
}
void draw() {
background(216, 80, 132);
//——————-sperm———————-
for (int y4 = 300; y4<=height-101; y4+=5) {// a for loop to make a delay of the tale
angle += anglevel;
}
for ( int x = 100; x<=200; x +=5) {//the length of the sperm tail
fill(255, 255, 255);

// 3 tails
float y4 = amplitude *sin(angle);
float y1 = amplitude *cos(angle);
float y2 = amplitude *sin(angle+0.2);
angle += anglevel;
ellipse(x, y4+height/4, 2, 2);
ellipse(x+100, y1+height/2, 2, 2);
ellipse(x, y2+height*3/4, 2, 2);
}

ellipse(headx, height/4, 15, 15);
ellipse(headx2, height/2, 15, 15);
ellipse(headx, height*3/4, 15, 15);

//—————–ovary————————
float circlecolor = map(radius, 20, 300, 0, 10);
float d = dist(mouseX, mouseY, x, y);
noStroke();
fill(193, 30, 90);
ellipse(x, y, radius, radius);
// conditional if mouse inside the circle
if ((d < radius) && (radius <= 350)) {
fill(213, 49, 100);
text(“GET OUT!!”, 310, 300);
radius = radius * 1.09;
fill(193, 39, 90-circlecolor);
textSize(40);
headx = 100;
headx2 = 200;
}
// conditional to grow till 350 radius
if (radius >= 350) {
radius = 350;
}
// if mouse out from ovary return to normal
if (d > radius && radius > 20) {
radius = radius – 5;
fill(193, 39, 90-circlecolor);
headx = 200;
headx2 = 300;
}
}

 

Leave a Reply