Skip to content

Instantly share code, notes, and snippets.

@volfegan
Last active February 12, 2021 22:06
Show Gist options
  • Save volfegan/e9707997a14a8fb621a794f6167241a5 to your computer and use it in GitHub Desktop.
Save volfegan/e9707997a14a8fb621a794f6167241a5 to your computer and use it in GitHub Desktop.
Sperm swimming outwards from a centre in form of a heart
//based from https://twitter.com/ntsutae/status/1227550470001946624
//https://www.openprocessing.org/sketch/841004
//resource http://mathworld.wolfram.com/HeartCurve.html
float C=0, F;
Sperm[] Sperm;
long time;
void setup() {
size(1280, 720);
Sperm=new Sperm[width];
for (int i=0; i<Sperm.length; i++) {
Sperm[i]=new Sperm(i);
}
time = millis();
}
void draw() {
fill(0, 50);
noStroke();
rect(0, 0, width, height);
stroke(255);
C+=.03; // counter
F=sin(C)*17;// gravity force
for (int i=0; i<width; i++) {
Sperm[i].update();
if (time + 1000 < millis()) Sperm[i].changeFlowDirection();
}
if (time + 1000 < millis()) time = millis();
}
class Sperm {
float index,x, y;
boolean outboundMove;
boolean nearCentre;
Sperm(float i) {
x=width/2;
y=height/2;
index=i;
outboundMove = true;
nearCentre = false;
}
void update() {
float X, Y;
x=(x+width)%width;
y=(y+height)%height;
if (x-width/2 < 20 && y-height/2 < 20) nearCentre = true;
else nearCentre = false;
X=x;
Y=y;
float n=noise((float)x/width-index, (float)y/height, C)*30;
x+=cos(n)*3;
y+=sin(n)*3;
float d=dist(x, y, width/2, height/2)/F; // I dont care of zero divided.
if (outboundMove == true) {
x+=16*pow(sin(radians(index)), 3);
y+=-1*(13*cos(radians(index))-5*cos(radians(2*index))-2*cos(radians(3*index))-cos(radians(4*index)));
} else {
x+=(x-width/2)/d;
y+=(y-height/2)/d;
}
line(X, Y, x, y);
}
void changeFlowDirection() {
if (outboundMove) outboundMove = false;
if (!outboundMove && nearCentre) outboundMove = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment