# Dist from mouse to ellipse

**URL:** https://discourse.processing.org/t/dist-from-mouse-to-ellipse/18157
**Category:** Coding Questions
**Created:** [February 26, 2020, 8:47pm UTC](https://discourse.processing.org/t/dist-from-mouse-to-ellipse/18157 "2020-02-26T20:47:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![NotConsumable](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@NotConsumable](https://discourse.processing.org/u/NotConsumable)
#### Post date: [February 26, 2020, 8:47pm UTC](https://discourse.processing.org/t/dist-from-mouse-to-ellipse/18157/1 "2020-02-26T20:47:34Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 26, 2020, 8:54pm UTC](https://discourse.processing.org/t/dist-from-mouse-to-ellipse/18157/2 "2020-02-26T20:54:15Z")

</div>

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

not fully sure what you want but maybe:

```auto
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

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [March 3, 2020, 6:55pm UTC](https://discourse.processing.org/t/dist-from-mouse-to-ellipse/18157/3 "2020-03-03T18:55:17Z")

</div>

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](http://www.jeffreythompson.org/collision-detection/index.php)

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

- [https://www.geometrictools.com/Documentation/DistancePointEllipseEllipsoid.pdf](https://www.geometrictools.com/Documentation/DistancePointEllipseEllipsoid.pdf)

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).
