Distance from point to ellipse
<p>I need to somehow compute the distance between a point and an Ellipse. 
I describe the Ellipse in my program as coordinates x = a cos phi and y = b sin phi (where a,b are constants and phi the changing angle).</p>

<p>I want to compute the shortest distance between a point P and my ellipse.
My thought were to calculate the vector from the center of my ellipse and the point P and then find the vector that start from the center and reaches the end of the ellipse in the direction of the point P and at the end subtract both vectors to have the distance (thi may not give the shortest distance but it's still fine for what I need. 
The problem is I don't know how to compute the second vector. 
Does someone has a better Idea or can tell me how I can find the second vetor?</p>

<p>Thanks in advance!</p>

<hr>

<p><strong>EDIT1:</strong></p>

<p><strong>ISSUE:COMPUTED ANGLE DOESN'T SEEM TO GIVE RIGHT POINT ON ELLIPSE</strong></p>

<p>Following the suggestion of <em>MARTIN R</em>, I get this result:</p>

<p><a href="https://i.stack.imgur.com/BnEVP.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/BnEVP.jpg" alt="enter image description here"></a></p>

<p>The white part is created by the program of how he calculates the distance. I compute the angle <code>phi</code> using the vector from the center P (of ellipse) to the center of the body. But as I use the angle in the equation of my ellipse to get the point that should stay on the ellipse BUT also having same direction of first calculated vector (if we consider that point as a vector) it actually gives the "delayed" vector shown above.</p>

<p>What could be the problem? I cannot really understand this behavior (could it have something to do with atan2??)</p>

<p><strong>EDIT2:</strong>
I show also that in the other half of the ellipse it gives this result: </p>

<p><a href="https://i.stack.imgur.com/nwEVW.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/nwEVW.jpg" alt="enter image description here"></a></p>

<p>So we can see that the only case where this works is when we have <code>phi = -+pi/2</code> and <code>phi = -+pi</code></p>

<hr>

<p><strong>IMPLEMENTATION FAILED</strong></p>

<p>I tried using the implementation of <strong>MARTIN R</strong> but I still get the things wrong.</p>

<p>At first I thought it could be the center (that is not always the same) and I changed the implementation this way:</p>

<pre><code>func pointOnEllipse(ellipse: Ellipse, p: CGPoint) -&gt; CGPoint {

    let maxIterations = 10
    let eps = CGFloat(0.1/max(ellipse.a, ellipse.b))

    // Intersection of straight line from origin to p with ellipse
    // as the first approximation:
    var phi = atan2(ellipse.a*p.y, ellipse.b*p.x)

    // Newton iteration to find solution of
    // f(θ) := (a^2 − b^2) cos(phi) sin(phi) − x a sin(phi) + y b cos(phi) = 0:
    for _ in 0..&lt;maxIterations {
        // function value and derivative at phi:
        let (c, s) = (cos(phi), sin(phi))
        let f = (ellipse.a*ellipse.a - ellipse.b*ellipse.b)*c*s - p.x*ellipse.a*s + p.y*ellipse.b*c - ellipse.center.x*ellipse.a*s + ellipse.center.y*ellipse.b*c
        //for the second derivative
        let f1 = (ellipse.a*ellipse.a - ellipse.b*ellipse.b)*(c*c - s*s) - p.x*ellipse.a*c - p.y*ellipse.b*s - ellipse.center.x*ellipse.a*c - ellipse.center.y*ellipse.b*s

        let delta = f/f1
        phi = phi - delta
        if abs(delta) &lt; eps { break }
    }

  return CGPoint(x: (ellipse.a * cos(phi)) + ellipse.center.x, y: (ellipse.b * sin(phi)) + ellipse.center.y)

}
</code></pre>

<p>We can see what happens here:</p>

<p><a href="https://i.stack.imgur.com/vMwqU.png" rel="nofollow"><img src="https://i.stack.imgur.com/vMwqU.png" alt="enter image description here"></a></p>

<p>This is pretty strange, all points stay in that "quadrant". But I also noticed when I move the green box far far away from the ellipse it seems to get the right vector for the distance.</p>

<p>What could it be?</p>

<hr>

<p><strong>END RESULT</strong></p>

<p>Using updated version of <strong>MARTIN R</strong> (with 3 iterations)</p>

<p><a href="https://i.stack.imgur.com/O2MWa.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/O2MWa.jpg" alt="enter image description here"></a></p>