xxxxxxxxxx
// Ribbon class modified from 'ContinuousLines', Sketch 392781 by Varnoss.
// Move the mouse and the ribbons will chase the cursor.
// ArrayList, PVector, class, mouseX, mouseY, dist, atan2, cos, sin
Ribbon r, r2, r3;
int numSegs = 100;
float x, y, a, b, c, d, ang;
void setup() {
size(1200, 600);
strokeWeight(1);
r = new Ribbon();
r2 = new Ribbon();
r3 = new Ribbon();
}
void draw() {
background(20);
x = mouseX;
y = mouseY;
r.update(x, y, 1);
ang += .09;
a = x + cos(ang) * 100;
b = y + sin(ang) * 100;
r2.update(a, b, 2);
c = x + cos(-ang) * 50;
d = y + sin(-ang) * 50;
r3.update(c, d, 3);
}
class Ribbon {
Segment seg;
ArrayList<Segment> segments = new ArrayList<Segment>();
PVector destPos = new PVector();
PVector currPos = new PVector();
PVector prevPos = new PVector();
PVector leadLeft = new PVector();
PVector leadRight = new PVector();
PVector trailLeft = new PVector();
PVector trailRight = new PVector();
void update(float x, float y, int cc) {
destPos.x = x;
destPos.y = y;
currPos.x += (destPos.x-currPos.x) * 0.15;
currPos.y += (destPos.y-currPos.y) * 0.15;
float size = dist(destPos.x, destPos.y, currPos.x, currPos.y) * .04;
float angle = atan2(currPos.y-prevPos.y, currPos.x-prevPos.x)+PI/2;
float angleX = cos(angle);
float angleY = sin(angle);
trailLeft.x = leadLeft.x;
trailLeft.y = leadLeft.y;
trailRight.x = leadRight.x;
trailRight.y = leadRight.y;
leadLeft.x = currPos.x-size*angleX;
leadLeft.y = currPos.y-size*angleY;
leadRight.x = currPos.x+size*angleX;
leadRight.y = currPos.y+size*angleY;
seg = new Segment(leadLeft, leadRight, trailLeft, trailRight );
segments.add(seg);
if (segments.size() > numSegs) segments.remove(0);
prevPos.x = currPos.x;
prevPos.y = currPos.y;
display(cc);
}
void display(int chsclr) {
for (int i = segments.size ()-1; i > 0; i--) {
Segment s = segments.get(i);
if (chsclr == 1) {
stroke(255, i*2.5, 255);
fill(255, i*2.5, 255);
} else if (chsclr == 2) {
stroke(i*2.5, 255, 255);
fill( i*2.5, 255, 255);
} else if (chsclr == 3) {
stroke(255, 255, i*2.5);
fill(255, 255, i*2.5);
}
beginShape();
vertex(s.tl.x, s.tl.y);
vertex(s.tr.x, s.tr.y);
vertex(s.br.x, s.br.y);
vertex(s.bl.x, s.bl.y);
endShape(CLOSE);
}
}
}
class Segment {
PVector tl, tr, bl, br;
Segment(PVector tlin, PVector trin, PVector blin, PVector brin) {
tl = tlin.get();
tr = trin.get();
bl = blin.get();
br = brin.get();
}
}