We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to make a simple sketch where depending on mouse location, the triangle changes colour.
I can do this for quarter squares by saying if (mouseX < 300 & mouseY < 300) { rect ... }
but am unable to work out how to define the triangle from two corners to the centre - shown in my sketch as coloured red.
void draw() {
background(145,180,70);
if (mouseX < 300 & mouseY < 300) {
triangle(0, 0, 0,600, 300,300); // Left
fill(190,75,90);
}
if (mouseX > 300 & mouseY > 300){
triangle(300, 300, 600,600, 600,0); // Left
fill(190,75,90); // Right
}
}
Answers
If i understand your goal correctly, you could use atan2() to calculate the angle of the mouse-position in relation to the window-center. Something like this:
oh dear... far too complicate...
imho, you could draw the same triangle in an invisible PGraphics of the same size like your screen (and with noSmooth() I guess). Here the color is unaltered and identifies the triangle. Now, on mouse click use pg.get() to retrieve the color of the point and compare it to the fixed color of your triangle. Thus without using heavy math you can comfortable identify different triangles.
Some fun with triangles. There is an adapted test for "insideness" of the triangle (credits on code). It works like a charm, but I dont really get all the math...
The code is a bit more complicated, but the TShape class can be hepfull, maybe.