Point-Ellipse collision test.

2.2k Views Asked by At

O.k, so basically i am writing some C++ program and I've encountered some problem. i need to check if a given Point $P$ is inside Ellipse or not.

Ellipse_Collison

So lets say points $(X_0,Y_0),(X_1,Y_1),P_0,P_1$ what i need to test is if the point $P_0$ (or $P_1$ in other occasion) is inside the ellipse or not. We can assume that the ellipse would be placed inside some rectangle with given points $(X_0,Y_0),(X_1,Y_1)$ and the rectangle's edges would be parallel to $X,Y$ axis (The ellipse wont be rotated but the height could be bigger than the width and the opposite).

any idea of how can i implement it ?

1

There are 1 best solutions below

3
On

Use the ellipse equation. A point $(x,y)$ is inside the ellipse if $$ \frac{(x- m_x)^2}{\sigma_x^2} + \frac{(y- m_y)^2}{\sigma_y^2} < 1 $$

The center of the ellipse is given by $(m_x, m_y)$ and the half-diameters are given by $(\sigma_x, \sigma_x)$ and you can read these off easily from your sketch:

$ m_x = \frac 12 (x_0 + x_1)$

$ m_y = \frac 12 (y_0 + y_1)$

$ \sigma_x = \frac 12 |- x_0 + x_1|$

$ \sigma_y = \frac 12 |- y_0 + y_1|$