Hi,

There are some function to know if mouse is inside or outside?

For example a ofCircle.

Thank you

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