David David - 15 days ago 7
Java Question

Processing - ScreenToWorldCoordinate function?

I'm working on a project with Processing that requires the ability to determine whether or not the mouse is inside of a circle. Therefore, I need to obtain the position of the circle and the position of the mouse. However, the position of the circle has been modified using matrix functions such as

translate
and
scale
. For example:

float circle_x;
float circle_y;
float circle_radius;

void setup() {
circle_x = 10.0;
circle_y = 17.0;
circle_radius = 15.0;
}

void draw() {
pushMatrix();

/* ... arbitrary number of calls to modify the matrix ... */
translate(THING, THING);
scale(THING);
translate(THING);
/* ... */

/* draw the circle */
ellipse(circle_x, circle_y, circle_radius, circle_radius);

/* now I want to detect whether or not my mouse is inside of the
circle. In order to do that, I need to modify the coordinates
of the mouse in the same fashion as circle_x and circle_y. I'm
hoping to do something like this: */
float world_x = screenToWorld_X(mouseX);
float world_y = screenToWorld_Y(mouseY);

/* ... check if [world_x, world_y] is inside the circle ... */


popMatrix();

}


Is there any way to do this in Processing? I was looking at the documentation but I couldn't seem to find any functions. If not, how can I achieve my goal?

Thanks.

Answer

Check out the coordinates section of the reference.

Specifically, you're looking for the modelX() and modelY() functions. Or you could use the screenX() and screenY() functions to go the other way and convert your world coordinates into screen coordinates.