-1

I am contributing to a project where I need to calculate the closest point on an ellipse to a fixed point. I have made another post about this where I accepted an answer that iterates through the coordinates of the ellipse and returns the closest point. But now I would like to know if it is possible to solve this equation without iteration. I have found two formulas for plotting ellipses. I used those to get a formula for the distance between any point on the ellipse circumference and the fixed point. Where the derivative of those formulas intersects the x axis is the x coordinate of the closest point. Either formula would work for me, I just need help solving for the x intersection. I need help to rearrange the formula so x is all alone on one side and not on the other. I am ultimately hoping to have a formula that can be processed in one hit without requiring further rearranging/calculus to solve.

The formulas have 6 variables representing properties of the ellipse and the fixed point.

  • $p_x$ = fixed point x value
  • $p_y$ = fixed point y value
  • $e_x$ = ellipse centre x value
  • $e_y$ = ellipse centre y value
  • $w$ = half of the ellipse width
  • $h$ = half of the ellipse height

I have made screenshots of the formulas / derivatives using desmos because I don't know how to type math functions into this forum.

Function and derivative A: $$y = (p_x-(e_x+x))^2 + (p_y-(e_y+h\sin(\arccos(\frac{x-e_x}{w}))))^2$$ $$0=\frac{2h(x-e_{x})(p_{y}-e_{y}-h\sqrt{(1-\frac{(x-e_{x})^{2}}{w^{2}})}}{\sqrt{1-(x-e_{x})^{2}}}-2(p_{x}-e_{x}-x)$$

Function and derivative B: $$y=\left(p_{x}-\left(e_{x}+x\right)\right)^{2}+\left(p_{y}-\left(e_{y}+\frac{h}{w}\sqrt{w^{2}-x^{2}}\right)\right)^{2}$$ $$0=\frac{2hx\left(p_{y}w-h\sqrt{w^{2}-x^{2}}\right)\sqrt{w^{2}-x^{2}}}{w^{4}-x^{2}w^{2}}-2\left(p_{x}-e_{x}-x\right)$$

Phedg1
  • 147

1 Answers1

0

Running a math AI on my pc I was able to solve for x and simplify the equation. I assumed that the ellipse centre is at the origin (0, 0) for a more simplified formula. The formula is repetitive in places so I broke it into pieces to reduce processing.

Variables $$Fixed Point =\left(p_{x},\ p_{y}\right)$$ $$Ellipse Centre =\left(e_{x},\ e_{y}\right)$$ $$Ellipse Radius =\left(r_{x},\ r_{y}\right)$$ $$Closest Ellipse Point =\left(c_{x},\ c_{y}\right)$$

Assumptions: $$p_{x}>e_{x}$$ $$p_{y}>e_{y}$$ $$e_{x}=e_{y}=0$$ $$p_{x}>0$$ $$p_{y}>0$$

Ellipse Function: $$D\ =\ r_{y}^{2}-r_{x}^{2}$$ $$C\ =\ r_{y}^{4}-2r_{x}^{2}r_{y}^{2}-p_{y}^{2}r_{y}^{2}+r_{x}^{4}-p_{x}^{2}r_{x}^{2}$$ $$B\ =\sqrt[3]{\ -54p_{x}^{4}D^{2}r_{x}^{10}+54p_{x}^{2}D^{4}r_{x}^{8}+54p_{x}^{2}D^{2}\left(-C\right)r_{x}^{8}-C^{3}r_{x}^{6}+\sqrt{r_{x}^{12}\left(\left(54p_{x}^{2}p_{y}^{2}r_{x}^{2}r_{y}^{6}-108p_{x}^{2}p_{y}^{2}r_{x}^{4}r_{y}^{4}+54p_{x}^{2}p_{y}^{2}r_{x}^{6}r_{y}^{2}-C^{3}\right)^{2}-C^{6}\right)}}$$ $$A\ =\ \sqrt{\frac{1}{D^{2}}\left(B+3p_{x}^{2}r_{x}^{4}+2r_{x}^{2}C+\frac{r_{x}^{4}C^{2}}{B}\right)}$$ $$c_{x}=\frac{\sqrt{3}A+\sqrt{3}\sqrt{\frac{1}{D^{3}}\left(-DB+6p_{x}^{2}r_{x}^{4}D+4r_{x}^{2}DC+\frac{r_{x}^{4}\left(-D\right)C^{2}}{B}+\frac{6\sqrt{3}p_{x}r_{x}^{4}\left(p_{y}^{2}r_{y}^{2}+D^{2}\right)}{A}\right)}+\frac{3p_{x}r_{x}^{2}}{-D}}{6}$$ $$c_{y}=\frac{r_{y}}{r_{x}}\sqrt{r_{x}^{2}-c_{x}^{2}}$$ $$d_{istance}\ =\ \sqrt{\left(\left(e_{x}+c_{x}\right)-p_{x}\right)^{2}+\left(\left(e_{y}+c_{y}\right)-p_{y}\right)^{2}}$$

Programming Optimization:

Assuming this calculation will need to be made regularly and that you are trying to determine if the ellipse lies within a specified distance of the fixed point, you can perform a check to determine if this formula is even worthwhile. Find the distance between the ellipse centre and the fixed point. Subtract from it the larger of the two radiuses and see if the distance is smaller than or equal to the distance you are checking against. If so, it is now worthwhile performing this check.

Assuming this calculation will need to be made regularly and that the ellipse dimensions do not change after its initial creation, the following calculations can be made on ellipse initialization to save performance.

$$r_{x}^{2}:r_{y}^{2}:r_{x}^{4}:r_{y}^{4}:r_{x}^{6}:r_{y}^{6}:r_{x}^{8}:r_{x}^{10}:r_{x}^{12}:r_{y}^{2}-r_{x}^{2}:\left(r_{y}^{2}-r_{x}^{2}\right)^{2}:\left(r_{y}^{2}-r_{x}^{2}\right)^{3}:r_{x}^{2}-r_{y}^{2}:\sqrt{3}$$

Exceptions:

This formula does not take into account ellipse rotation. If the ellipse is rotated by θ, assume the ellipse is not rotated and rotate the fixed point around the ellipse centre by .

This formula only works if the fixed point has larger coordinates in both axis than the ellipse centre. For each axis where this is not the case, mirror the fixed point relative to the ellipse centre. When the closest point is calculated remember to undo this mirroring before adding it to the ellipse centre coordinate.

This formula only works if the fixed point is not inside the ellipse. Determine if the fixed point x value is between the ellipse centre ± the ellipse x radius. If so, calculate the y value for the ellipse at that x value. Determine if the fixed point y value is between the ellipse centre ± the ellipse y value at the fixed point x value. If so, the fixed point is inside the ellipse.

This formula only works if the fixed point is not share a coordinate with the ellipse. If it does, the closest point will be the ellipse centre ± the relevant ellipse radius.

Closest Point on a Moving Ellipse:

If the ellipse is moving between two points then there is a chance that the closest point on the ellipse would be mid move, between the two points. First find the gradient from the start point to the finish point. Then find point on the ellipse that would be tangential to this gradient.

$$Start Point = \left(s_{x},\ s_{y}\right)$$ $$Finish Point = \left(f_{x},\ f_{y}\right)$$ $$Tangent Points = \left(t_{x},\ t_{y}\right)$$ $$Tangent Coordinates = \left(s_{x}+t_{x},\ s_{y}+t_{y}\right),\ \left(s_{x}-t_{x},\ s_{y}-t_{y}\right)$$

$$g_{a}=\frac{f_{y}-s_{y}}{f_{x}-s_{x}}$$ $$t_{x}=-\frac{r_{x}^{2}g_{a}}{\sqrt{r_{y}^{2}+r_{x}^{2}g_{a}^{2}}}$$ $$t_{y}=\frac{r_{y}}{r_{x}}\sqrt{r_{x}^{2}-t_{x}^{2}}$$ $$g_{b}=\frac{-1}{g_{a}}$$ $$c_{x}=\frac{g_{a}\left(s_{x}\pm t_{x}\right)-\left(s_{y}\pm t_{y}\right)-g_{b}p_{x}+p_{y}}{g_{a}-g_{b}}$$ $$\min\left(s_{x},\ f_{x}\right)\ \le\ c_{x}\le\max\left(s_{x},\ f_{x}\right)$$

If the closest x value falls between the start x and finish x, then calculate the closest x and y for both tangent points and use the one that is closest to the fixed point. Otherwise, use the non moving formulas on whichever of the start and finish points is closest to the fixed point.

$$c_{y}=g_{a}c_{x}-g_{a}\left(s_{x}\pm t_{x}\right)+\left(s_{y}\pm t_{y}\right)$$

Calculate the distance between the closest x and y for both tangent points. If the distance between both of these points and the fixed point is less than the distance between both of these points, the fixed point is inside the ellipse at its closest point.

Moving Ellipse Exception:

If the ellipse is traveling in only a single axis the above moving ellipse formula will not work. Instead determine if the fixed point value on the axis the ellipse is moving on falls between the start and finish points on the same axis. If it does then the closest distance to the ellipse is the distance between the start point coordinate for the axis that is not changing and the fixed point coordinate of the same axis, minus the radius of the ellipse on that axis.

Phedg1
  • 147