Dist from mouse to ellipse

i am trying to make 4 different circles appear around my mouse depending how far away i am from a randomly occurring ellipse and i was wondering how i would implement a dist function within an if statement in order to make the circle appear around my mouse based on my mouse’s location in relation to how far away i am from the ellipse.

1 Like

look at the dist() command in the reference https://www.processing.org/reference/

not fully sure what you want but maybe:

float myDist = dist(....................

if(myDist>=0 && myDist<50)   {
  // draw one circle 
}
else if(myDist>=50 && myDist<150)   {
  // draw 2 circles 
}
else if(myDist>=150 && myDist<250)   {
  // draw 3 circles
}
else if(myDist>=250 )   {
  // draw 4 circles 
}

Chrisir

1 Like

Do you want distance from the center of the ellipse, or distance from the closest edge point on the ellipse? Distance from a circle is quite easy – you take the point distance and subtract the radius. Distance from an ellipse is surprisingly hard – so much so that it is not covered in most major coliision detection tutorials.

As with any book, there’s a lot more useful material than could be covered here. Things that aren’t discussed are mostly left out because the math gets too complicated. Three-dimensional space isn’t touched on. Ellipses, which seem like they should be pretty easy, are actually very difficult. Collision Detection

Although, if you want to implement it, there is a survey of the math with pseudocode here:

I implemented ellipse collision detection in a library a while back – but that expresses distance as a ratio >1 rather than as an absolute distance to the closest point on the ellipse (it doesn’t scale the same in x and y). This works fine for collision detection because you only care if the value is <1 or not (colliding).

2 Likes