Detecting mouse hover over the circle


#1

I need to detect is my mouse pointer above the circle that I drew.
But for now I need an insane precision to do that.
Is there any way to get my circle area and detect the collide event, or something?

Screenshot_2018-07-11_14-59-29


#2

Hi, an easy way to detect collision with circles is to use distance function!

(https://openframeworks.cc/documentation/math/ofMath/#show_ofDist)

So, in draw or update, you can get the mouse coordinates (OF stores that in variables called mouseX and mouseY for you) and then for every single circle that you have you loop through it and (lets say you have an array of structures that have the x,y ,color and radius stored in them called circles)

if (ofDist(mouseX,mouseY, circles]i].x,circles[i].y) <= circles[i].radius) {
    circles[i].color = ofColor::red;
}
else{
  circles[i].color = ofColor::blue; // or else it will stay red after your mouse leaves
}

Let me know if I misunderstood your question, I’m not sure what kind of precision you are in need of. More information about how it needs to be precise would help :slight_smile:


#3

Here is my code: https://github.com/Regeneric/sraca


#4

I would solve that using ofPolyline or ofPath, drawing a circle there and checking with a function “inside”
https://openframeworks.cc//documentation/graphics/ofPolyline/#!show_inside

with polyline you have to build your circle (polar coordinates), and with ofPath you can use “arc” function to make the circle.


#5

I’ve done something like that: https://imgur.com/a/iW7DEys
And for now it’s working fine.