Intersection Point on an Ellipse

I am attempting to find the point of intersection between a given point (x, y) and an ellipse.
The purpose of which would be to outline a maximum value that a Vector2 can be on the user’s screen.

The ellipse is the green outline, with R1 being its diameterX/2 (semi-major axis) and R2 being its diameterY/2. (semi-minor axis)
Its center is screenWidth/2, screenHeight/2.
Basically, this ellipse fills the user’s entire screen.

The blue and red points are the Vector2’s value and the desired point respectively.

The yellow line is a ray from the center of the ellipse towards the Vector2’s value.

example

The best that I have been able to achieve is determining whether the Vector2’s value is within the shown ellipse.

I would appreciate somebody helping me solve this.
Thanks!

1 Like

I made this interactive demo for you :slight_smile:

You’ll need to define edge cases for when x=0

For a more practical approach, you can use the following:

--[[
a and b are the semi-major and semi-minor axis, respectively
'point' represents the blue Vector2
]]
local function GetIntersection(a: number,b: number,point: Vector2): Vector2
    local theta = math.atan2(point.Y,point.X);
    local r = (a*b)/math.sqrt((b*math.cos(theta))^2+(a*math.sin(theta))^2);
    return point.Unit*r;
end

This works really well, thanks.
However, how would I go about changing the center of (r)?
Currently, as it’s centered at (0, 0) and I’m applying this to the screen, only one quadrant of the ellipse is considered.

I tried playing around with the equation to no avail.

Whoops, I accidentally set the radii to the dimensions of the screen :sweat:
Anyway, here’s a modified function to calculate the intersection while defining the center as well:

--[[
a and b are the semi-major and semi-minor axis, respectively
'center' represents the coordinates of the center of the ellipse
'point' represents the blue Vector2
]]
local function GetIntersection(a: number,b: number,center: Vector2,point: Vector2): Vector2
    point -= center;
    local theta = math.atan2(point.Y,point.X);
    local r = (a*b)/math.sqrt((b*math.cos(theta))^2+(a*math.sin(theta))^2);
    return center+point.Unit*r;
end
1 Like