Check if a point is inside, outside or on the ellipse

29 Jun, 2022
Given an ellipse centered at (h, k), with semi-major axis a, semi-minor axis b, both aligned with the Cartesian plane. The task is to determine if the point (x, y) is within the area bounded by the ellipse.Examples:  
Input: h = 0, k = 0, x = 2, y = 1, a = 4, b = 5 
Output: Inside

Input: h = 1, k = 2, x = 200, y = 100, a = 6, b = 5
Output: Outside
 
 
Approach: We have to solve the equation of ellipse for the given point (x, y),  
(x-h)^2/a^2 + (y-k)^2/b^2 <= 1
 If in the inequation, results comes less than 1 then the point lies within, else if it comes exact 1 then the point lies on the ellipse, and if the inequation is unsatisfied then point lies outside of the ellipse.Below is the implementation of the above approach:  
C++
Java
Python 3
C#
PHP
Javascript
// C++ Program to check if the point// lies within the ellipse or not#include <bits/stdc++.h>using namespace std; // Function to check the pointint checkpoint(int h, int k, int x, int y, int a, int b){     // checking the equation of    // ellipse with the given point    int p = (pow((x - h), 2) / pow(a, 2))            + (pow((y - k), 2) / pow(b, 2));     return p;} // Driver codeint main(){    int h = 0, k = 0, x = 2, y = 1, a = 4, b = 5;     if (checkpoint(h, k, x, y, a, b) > 1)        cout << "Outside" << endl;     else if (checkpoint(h, k, x, y, a, b) == 1)        cout << "On the ellipse" << endl;     else        cout << "Inside" << endl;     return 0;}
// Java Program to check if the point// lies within the ellipse or notimport java.util.*; class solution{  // Function to check the pointstatic int checkpoint(int h, int k, int x, int y, int a, int b){     // checking the equation of    // ellipse with the given point    int p = ((int)Math.pow((x - h), 2) / (int)Math.pow(a, 2))            + ((int)Math.pow((y - k), 2) / (int)Math.pow(b, 2));     return p;} //Driver codepublic static void main(String arr[]){       int h = 0, k = 0, x = 2, y = 1, a = 4, b = 5;     if (checkpoint(h, k, x, y, a, b) > 1)       System.out.println("Outside");     else if (checkpoint(h, k, x, y, a, b) == 1)        System.out.println("On the ellipse");     else       System.out.println("Inside"); }} //This code is contributed by Surendra_Gangwar
# Python 3 Program to check if# the point lies within the# ellipse or notimport math # Function to check the pointdef checkpoint( h, k, x, y, a, b):     # checking the equation of    # ellipse with the given point    p = ((math.pow((x - h), 2) // math.pow(a, 2)) +         (math.pow((y - k), 2) // math.pow(b, 2)))     return p # Driver codeif __name__ == "__main__":     h = 0    k = 0    x = 2    y = 1    a = 4    b = 5     if (checkpoint(h, k, x, y, a, b) > 1):        print ("Outside")     elif (checkpoint(h, k, x, y, a, b) == 1):        print("On the ellipse")     else:        print("Inside") # This code is contributed# by ChitraNayal
// C# Program to check if the point// lies within the ellipse or notusing System; class GFG{ // Function to check the pointstatic int checkpoint(int h, int k, int x,                      int y, int a, int b){     // checking the equation of    // ellipse with the given point    int p = ((int)Math.Pow((x - h), 2) /             (int)Math.Pow(a, 2)) +            ((int)Math.Pow((y - k), 2) /             (int)Math.Pow(b, 2));     return p;} // Driver codepublic static void Main(){    int h = 0, k = 0, x = 2,        y = 1, a = 4, b = 5;     if (checkpoint(h, k, x, y, a, b) > 1)    Console.WriteLine("Outside");     else if (checkpoint(h, k, x, y, a, b) == 1)        Console.WriteLine("On the ellipse");     else    Console.WriteLine("Inside");}} // This code is contributed by inder_verma
<?php// PHP Program to check if the point// lies within the ellipse or not // Function to check the pointfunction checkpoint($h, $k, $x,                    $y, $a, $b){     // checking the equation of    // ellipse with the given point    $p = (pow(($x - $h), 2) / pow($a, 2)) +         (pow(($y - $k), 2) / pow($b, 2));     return $p;} // Driver code$h = 0;$k = 0;$x = 2;$y = 1;$a = 4;$b = 5; if (checkpoint($h, $k, $x, $y, $a, $b) > 1)    echo ("Outside"); else if (checkpoint($h, $k, $x, $y, $a, $b) == 1)    echo("On the ellipse" ); else    echo ("Inside") ;     // This code is contributed by Shivi_Aggarwal?>
<script> // javascript Program to check if the point// lies within the ellipse or not // Function to check the pointfunction checkpoint(h , k , x , y , a , b){     // checking the equation of    // ellipse with the given point    var p = (parseInt(Math.pow((x - h), 2)) / parseInt(Math.pow(a, 2)))            + (parseInt(Math.pow((y - k), 2)) / parseInt(Math.pow(b, 2)));     return p;} // Driver code     var h = 0, k = 0, x = 2, y = 1, a = 4, b = 5;     if (checkpoint(h, k, x, y, a, b) > 1)       document.write("Outside");     else if (checkpoint(h, k, x, y, a, b) == 1)        document.write("On the ellipse");     else       document.write("Inside"); // This code is contributed by 29AjayKumar </script>
Inside
 
Time complexity: O(1)
Auxiliary Space: O(1)
Shivi_Aggarwal
SURENDRA_GANGWAR
inderDuMCA
ukasp
29AjayKumar
krishnav4
Technical Scripter 2018
Geometric
Mathematical
Technical Scripter
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.