I’m drawing a triangle between three wandering points.
I want to invert the fill color when the triangle “flattens,” ie when all three points are on the same line.
I thought I would be able to do this by matching the slopes.
Sometimes the triangle flattens and nothing happens, such as around frameCount 132 and 586.
Sometimes the color inverts when the triangle does not flatten, such as at frameCount 1187 and 2167.
What gives?
I’ve been futzing around with this for a spell, and I’ve made progress, but now I’m ready to be shown the right way to do it. Or at least better search terms! I’ve searched “slope,” “match slope,” and “compare slope” here and at stack overflow without much luck.
Thanks!
float red, green, blue;
void setup() {
red = 255;
green = 100;
blue = 180;
stroke(255);
size(800, 800);
frameRate(30);
}
int count = 0;
int mult = 33;
void draw() {
float t = (float)frameCount;
background(0);
translate(width/2, height/2);
fill(red, green, blue);
int x1 = int(sin(t/137) * width/3);
int y1 = int(cos(t/149) * height/3);
int x2 = int(cos(t/151) * width/3);
int y2 = int(sin(t/131) * height/3);
int x3 = int(-sin(t/173) * width/3);
int y3 = int(-cos(t/179) * height/3);
triangle(x1, y1, x2, y2, x3, y3);
float slope1 = sq(y1-y2)/sq(x1-x2);
float slope2 = sq(y1-y3)/sq(x1-x3);
float slope3 = sq(y3-y2)/sq(x3-x2);
if (count ==0) {
if (int(slope1 * mult) == int(slope2 * mult)) {
if (int(slope1 * mult) == int(slope3 * mult)); { //previously tried this with && instead of nested ifs
count = 5;
red = 255 - red;
green = 255- green;
blue = 255 - blue;
println("ding ding ding, ", int(slope1), int(slope2), int(slope3), count, ", the frame count is ", frameCount);
}
}
}
if (count > 0) { //so I don't get a bunch of inversions at once
count = count -1;
}
if (mousePressed) { //this is how I know when the extra color inversions happen
println("OOPS, the framecount is ", frameCount);
}
}