How to know if mouse is inside / outside of a shape

Hi,

There are some function to know if mouse is inside or outside?
For example a ofCircle.
Thank you

For a circle, no, but the code is easy to write. A circle is defined by it’s center, let’s call it (cx, xy), and it’s radius, r. Any point that is less than or equal to r away from the center is inside of the circle.

if (ofDist(mouseX, mouseY, cx, cy) <= r) {
    // Mouse is inside of circle
}
else {
    // Mouse is outside of circle
}

For a rectangle, yup - check out ofRectangle’s inside(…) method.

For a the more general case of a polygon, some googling will bring you to different solutions. Wiki is one place to start looking.

Thank you @mikewesthad!!

And is there a function to determine wether a point x,y is inside a defined polygon? I know i can implement it by checking all points inside a polygon, but was looking for a more general predefined function working for every shape/manually defined polygon.

Thank you in advance!

@damia You might want to look through the addons for something like that.

ofxBox2D might have what you are looking for. I think that if you define your polygon as a fixture, you can then use the TestPoint method to check if the mouse is inside the polygon. [ref] Granted, I’ve never used box2D before.

ofxCV might also have what you are looking for. openCV has a pointPolygonTest method (with a tutorial).

Alternatively, since this is a pretty general question, you should be able to google something like “C++ check if point is in polygon” and get some source code for different methods pretty easily.

1 Like

Ok, I will investigate.

Thank you for all @mikewesthad!