Finding element ID by coordinates
in
Programming Questions
•
1 year ago
I'm trying to make an interactive map of the US that can recognize what state the mouse is over when it clicks. To do this, I need to be able to find if mouseX and mouseY are within a state. I can figure out how to do this for any reasonable geometric shape (circle, rect, triangle, etc) but don't know how it can be done with a state. I'm using a blank SVG map of the US and I've found two things that might help but I can't figure out how to make them work. One is this code:
public boolean contains(float x, float y) {
if (family == PATH) {
boolean c = false;
for (int i = 0, j = vertexCount-1; i < vertexCount; j = i++) {
if (((vertices[i][Y] > y) != (vertices[j][Y] > y)) &&
(x <
(vertices[j][X]-vertices[i][X]) *
(y-vertices[i][Y]) /
(vertices[j][1]-vertices[i][Y]) +
vertices[i][X])) {
c = !c;
}
}
return c;
} else {
throw new IllegalArgumentException("The contains() method is only implemented for paths.");
}
}
from http://code.google.com/p/processing/source/browse/tags/processing-1.2.1/core/src/processing/core/PShape.java?r=7108.
I don't understand what the family/PATH thing is talking about though and don't know how to make it read my SVG file.
I've also found this:
http://doc.4d.com/4D-Language-Reference-12.3/SVG/SVG-Find-element-ID-by-coordinates.301-756041.en.html
which would seem to be a solution but can't find that code in any Processing library.
Does anyone have any suggestions?
public boolean contains(float x, float y) {
if (family == PATH) {
boolean c = false;
for (int i = 0, j = vertexCount-1; i < vertexCount; j = i++) {
if (((vertices[i][Y] > y) != (vertices[j][Y] > y)) &&
(x <
(vertices[j][X]-vertices[i][X]) *
(y-vertices[i][Y]) /
(vertices[j][1]-vertices[i][Y]) +
vertices[i][X])) {
c = !c;
}
}
return c;
} else {
throw new IllegalArgumentException("The contains() method is only implemented for paths.");
}
}
from http://code.google.com/p/processing/source/browse/tags/processing-1.2.1/core/src/processing/core/PShape.java?r=7108.
I don't understand what the family/PATH thing is talking about though and don't know how to make it read my SVG file.
I've also found this:
http://doc.4d.com/4D-Language-Reference-12.3/SVG/SVG-Find-element-ID-by-coordinates.301-756041.en.html
which would seem to be a solution but can't find that code in any Processing library.
Does anyone have any suggestions?
1