Search code examples
mathluageometrylove2d

Position in ellipse formula


So let's say I got a coordinate grid. I need to know whether or not p0 is located on ellipse starting from p1 ending with p2.

Example with other geometric objects: -- Rectangle

function PositiongOnRectangle(posx, posy, x1, y1, x2, y2)
    return (posx >= x1 and posy >= y1 and posx <= x2 and posy <= y2)
end

-- circle

function PositionOnCircle(posx, posy, x1, y1, radius)
    local distance = math.sqrt((posx - x1)^2 + (posy - y1)^2)
    return (radius >= distance)
end

Exmaples above are witten in Lua, however pseudo code will do. I want to do the same but with ellipse.

Thanks in advance!


Solution

  • For ellipse, inscribed in axis-aligned rectangle, defined by two vertices p1, p2:

    PositionOnEllipse(posx, posy, x1, y1, x2, y2)
    
    ///center of ellipse coordinates:
    mx = (x1+x2)/2
    my = (y1+y2)/2
    
    ///ellipse semiaxes:
    ax = (x1-x2)/2
    ay = (y1-y2)/2 
    
    ////due to ellipse equation
     return = (posx-mx)^2/ax^2 + (posy-my)^2/ay^2 <= 1