Detecting the mouse being withing a confined range of a point
in
Programming Questions
•
3 years ago
I've an array of n coordinates that are drawn out and a path running through them. I need one of the coordinates to highlight when a mouse is within a given number of pixels to it. Using this formula:
- if (mouseX < xCoords[i] + 7 && mouseX > xCoords[i] - 7 && ypos < yCoords[i] + 7 && mouseY > yCoords[i] - 7) {
- selectedPoint = i;
- }
It works close to perfect but not quite perfect. There's strange quirks like being able to select a point using MouseX alone (e.g. if I line up the mouse under a point, it'll select fine if mouseX is within the distance I specified as if ignoring the clause that mouseY also needs to be within a set distance).
I'm going to guess I'm misunderstanding how && works.
Interestingly, I tried adding a
- } else {
- selectedPoint = 0;
- }
after it just to try. This had strange results also and seemed to only let one point out of 10 or so be highlighted and flick back to point 0 if I tried anything else.
1