We are about to switch to a new forum software. Until then we have removed the registration on this forum.
So here I have a program that generates a matrix of hearts. What I want it to do is to have it determine the distance between my mouse and the centerpoint of that object (Heart.xLoc, Heart.yLoc) and if the distance is 30 pixels or less to call Heart.count() and Heart.setColor().
Right now its doing something, but not quite what I intended, and I can't quite figure out whats going on. Thanks!
public Heart[] hArray = new Heart[49];
public color[] cArray = {
color(83, 244, 66), color(66, 244, 223), color(66, 80, 244), color(155, 66, 244), color(244, 66, 244), color(244, 66, 83), color(244, 95, 66), color(244, 176, 66), color(244, 241, 66), color(188, 244, 66)
};
void setup() {
size(500, 500);
background(0);
int k = 0;
for (int i = 1; i < 8; i++)
for (int p = 1; p < 8; p++)
hArray[k++] = new Heart(60*p, 60*i);
}
void draw() {
for (int i = 0; i < 49; i++)
hArray[i].display();
}
class Heart {
int xLoc, yLoc;
color currentColor;
int counter;
Heart(int x, int y) {
xLoc = x;
yLoc = y;
currentColor = cArray[0];
counter = 0;
}
void setColor() {
currentColor = cArray[counter];
}
void display() {
fill(currentColor);
noStroke();
beginShape();
vertex(xLoc, yLoc-10);
vertex(xLoc+5, yLoc-10);
vertex(xLoc+5, yLoc-15);
vertex(xLoc+10, yLoc-15);
vertex(xLoc+10, yLoc-20);
vertex(xLoc+20, yLoc-20);
vertex(xLoc+20, yLoc-15);
vertex(xLoc+25, yLoc-15);
vertex(xLoc+25, yLoc-10);
vertex(xLoc+30, yLoc-10);
vertex(xLoc+30, yLoc+5);
vertex(xLoc+25, yLoc+5);
vertex(xLoc+25, yLoc+10);
vertex(xLoc+20, yLoc+10);
vertex(xLoc+20, yLoc+15);
vertex(xLoc+15, yLoc+15);
vertex(xLoc+15, yLoc+20);
vertex(xLoc+10, yLoc+20);
vertex(xLoc+10, yLoc+25);
vertex(xLoc-5, yLoc+25);
vertex(xLoc-5, yLoc+20);
vertex(xLoc-10, yLoc+20);
vertex(xLoc-10, yLoc+15);
vertex(xLoc-15, yLoc+15);
vertex(xLoc-15, yLoc+10);
vertex(xLoc-20, yLoc+10);
vertex(xLoc-20, yLoc+5);
vertex(xLoc-25, yLoc+5);
vertex(xLoc-25, yLoc-10);
vertex(xLoc-20, yLoc-10);
vertex(xLoc-20, yLoc-15);
vertex(xLoc-15, yLoc-15);
vertex(xLoc-15, yLoc-20);
vertex(xLoc-5, yLoc-20);
vertex(xLoc-5, yLoc-15);
vertex(xLoc, yLoc-15);
endShape(CLOSE);
}
void count() {
if (counter < 9)
counter++;
else
counter = 0;
}
}
void mouseReleased() {
boolean hover = false;
int q = 0;
int distance = 60;
while (hover != true) {
print ("MOUSE: (" + mouseX + ", " + mouseY + ") and hArray[" + q + "]: (" + hArray[q].xLoc + ", " + hArray[q].yLoc + ") Distance: " + sqrt(pow((mouseY - mouseX), 2) + pow((hArray[q].yLoc - hArray[q].xLoc), 2)) + " ");
if (sqrt(pow((mouseY - mouseX), 2) + pow((hArray[q].yLoc - hArray[q].xLoc), 2)) <= 30) {
hArray[q].count();
hArray[q].setColor();
hover = true;
}
if (q < 48)
q++;
else {
print("NOT FOUND");
hover = true;
}
}
}
//if mouseX and mouseY are blah blah
//myHeart.changeColor(cArray[i++]);
Answers
Oh, wow. Sudden clarity after I looked at the code in my post. Just had distance formula a bit mixed up! facepalm
@Codedheart -- could you share the fix you made in your formula line(s)? It might help future forum-goers searching for distance formulas.
oh yeah! so distance formula is Sqrt(pow((x2 - x1), 2) + ppw((y2 - y1), 2))
what i had was y1-x1 .... y2 - x2
so
becomes
sqrt(pow((mouseY - hArray[q].yLoc), 2) + pow((mouseX - hArray[q].xLoc), 2)
dist() is a built in method that does this.
If you don't need the actual distance then skip the expensive sqrt and use something like
Or more succinctly:
if (sq(x - a) + sq(y - b) <= 30*30) {}
:ar!ha, yes, sq() exists and is MUCH better than pow(x, 2)