Oh, thank you so much! Turn it into a generic function made possible work with loops, but I would like to repeat this three different lines randomly and I don’t know how to do it. I have tried with if and else without success. Could you please help me?
void setup()
{
size(1000, 1000, P3D);
frameRate(60);
background (255);
}
void draw()
{
strokeWeight(10);
color red = color(255, 0, 0);
color blue = color(0, 0, 255);
drawGradientLine1(0, 25, 1000, 25, red, blue);
color yellow = color(255,255,0);
color lime = color(0,255,0);
drawGradientLine2(0,75,1000,75,yellow,lime);
color pink = color(255,0,255);
color cyan = color(0,255,255);
drawGradientLine3(0,125,1000,125,pink,cyan);
for(int y = 0; y < 300; y=y+1) {
drawGradientLine1(0,y,1000,y,red, blue);
}
for(int y = 300; y < 600; y=y+1) {
drawGradientLine2(0,y,1000,y,yellow, lime);
}
for(int y = 600; y < 1000; y=y+1) {
drawGradientLine3(0,y,1000,y,pink,cyan);
}
}
void drawGradientLine1(float x1, float y1, float x2, float y2, color color1, color color2)
{
int numSegs = 300; // how many segment shall we divide our line up into?
for(int i = 0; i < numSegs; i++)
{
float tA = i/float(numSegs); // how far along the line are we? 0-1
float tB = (i+1)/float(numSegs); // how far along the line is the next one?
// beginning of the segment
float xA = lerp(x1, x2, tA); // same as float xA = x1 + (x2 - x1) * tA;
float yA = lerp(y1, y2, tA); // same as float yA = y1 + (y2 - y1) * tA;
// end of the segment
float xB = lerp(x1, x2, tB); // same as float xB = x1 + (x2 - x1) * tB;
float yB = lerp(y1, y2, tB); // same as float yB = y1 + (y2 - y1) * tB;
stroke(lerpColor(color1, color2, tA));
line(xA, yA, xB, yB);
}
}
void drawGradientLine2(float x1, float y1, float x2, float y2, color color1, color color2)
{
int numSegs = 300; // how many segment shall we divide our line up into?
for(int i = 0; i < numSegs; i++)
{
float tA = i/float(numSegs); // how far along the line are we? 0-1
float tB = (i+1)/float(numSegs); // how far along the line is the next one?
// beginning of the segment
float xA = lerp(x1, x2, tA); // same as float xA = x1 + (x2 - x1) * tA;
float yA = lerp(y1, y2, tA); // same as float yA = y1 + (y2 - y1) * tA;
// end of the segment
float xB = lerp(x1, x2, tB); // same as float xB = x1 + (x2 - x1) * tB;
float yB = lerp(y1, y2, tB); // same as float yB = y1 + (y2 - y1) * tB;
stroke(lerpColor(color1, color2, tA));
line(xA, yA, xB, yB);
}
}
void drawGradientLine3(float x1, float y1, float x2, float y2, color color1, color color2)
{
int numSegs = 300; // how many segment shall we divide our line up into?
for(int i = 0; i < numSegs; i++)
{
float tA = i/float(numSegs); // how far along the line are we? 0-1
float tB = (i+1)/float(numSegs); // how far along the line is the next one?
// beginning of the segment
float xA = lerp(x1, x2, tA); // same as float xA = x1 + (x2 - x1) * tA;
float yA = lerp(y1, y2, tA); // same as float yA = y1 + (y2 - y1) * tA;
// end of the segment
float xB = lerp(x1, x2, tB); // same as float xB = x1 + (x2 - x1) * tB;
float yB = lerp(y1, y2, tB); // same as float yB = y1 + (y2 - y1) * tB;
stroke(lerpColor(color1, color2, tA));
line(xA, yA, xB, yB);
}
}