Given an ellipse centered at (h, k), with semi-major axis a, and 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 come to less than 1 then the point lies within, else if it comes to exactly 1 then the point lies on the ellipse, and if the inequation is unsatisfied then the point lies outside of the ellipse.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double checkpoint( double h, double k, double x, double y, double a, double b)
{
double p = ( pow ((x - h), 2) / pow (a, 2))
+ ( pow ((y - k), 2) / pow (b, 2));
return p;
}
int main()
{
double 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
import java.util.*;
class solution {
static double checkpoint( double h, double k, double x,
double y, double a, double b)
{
double p = (( double )Math.pow((x - h), 2 )
/ ( double )Math.pow(a, 2 ))
+ (( double )Math.pow((y - k), 2 )
/ ( double )Math.pow(b, 2 ));
return p;
}
public static void main(String arr[])
{
double 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" );
}
}
|
Python 3
import math
def checkpoint(h, k, x, y, a, b):
p = ((math. pow ((x - h), 2 ) / math. pow (a, 2 )) +
(math. pow ((y - k), 2 ) / math. pow (b, 2 )))
return p
if __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" )
|
C#
using System;
class GFG {
static double checkpoint( double h, double k, double x,
double y, double a, double b)
{
double p = (( double )Math.Pow((x - h), 2)
/ ( double )Math.Pow(a, 2))
+ (( double )Math.Pow((y - k), 2)
/ ( double )Math.Pow(b, 2));
return p;
}
public static void Main()
{
double 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" );
}
}
|
Javascript
<script>
function checkpoint(h , k , x , y , a , b)
{
var p = Math.pow((x - h), 2) / Math.pow(a, 2)
+ Math.pow((y - k), 2) / Math.pow(b, 2);
return p;
}
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" );
</script>
|
PHP
<?php
function checkpoint( $h , $k , $x ,
$y , $a , $b )
{
$p = (pow(( $x - $h ), 2) / pow( $a , 2)) +
(pow(( $y - $k ), 2) / pow( $b , 2));
return $p ;
}
$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" ) ;
?>
|
Time complexity: O(1)
Auxiliary Space: O(1)
Distance Formula Approach
To determine if a point is inside, outside or on an ellipse, we can use the following formula based on the distance between the point and the center of the ellipse:
((x – h) * cos(theta) + (y – k) * sin(theta))^2 / a^2 + ((x – h) * sin(theta) – (y – k) * cos(theta))^2 / b^2 <= 1
where (h, k) is the center of the ellipse, a is the semi-major axis, b is the semi-minor axis, and theta is the angle between the x-axis and the major axis of the ellipse.
If the result of this formula is less than or equal to 1, then the point is inside the ellipse. If it is greater than 1, then the point is outside the ellipse. If it is equal to 1, then the point is on the ellipse.
Below are the steps for the above approach:
- Calculate the value of theta using the formula: theta = atan2(b * (y – k), a * (x – h))
- Calculate the value of the distance from the formula mentioned above.
- Compare the result with 1 to determine the position of the point.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string check_point_position( int h, int k, int x, int y,
int a, int b)
{
double theta = atan2 (b * (y - k), a * (x - h));
double distance
= pow ((x - h) * cos (theta) + (y - k) * sin (theta),
2)
/ pow (a, 2)
+ pow ((x - h) * sin (theta) - (y - k) * cos (theta),
2)
/ pow (b, 2);
if (distance < 1)
return "Inside" ;
else if (distance > 1)
return "Outside" ;
else
return "On" ;
}
int main()
{
cout << check_point_position(0, 0, 2, 1, 4, 5)
<< endl;
cout << check_point_position(1, 2, 200, 100, 6, 5)
<< endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static String checkPointPosition( int h, int k, int x, int y, int a, int b) {
double theta = Math.atan2(b * (y - k), a * (x - h));
double distance = Math.pow((x - h) * Math.cos(theta) +
(y - k) * Math.sin(theta), 2 ) / Math.pow(a, 2 )
+ Math.pow((x - h) * Math.sin(theta) - (y - k) *
Math.cos(theta), 2 ) / Math.pow(b, 2 );
if (distance < 1 ) {
return "Inside" ;
} else if (distance > 1 ) {
return "Outside" ;
} else {
return "On" ;
}
}
public static void main(String[] args) {
System.out.println(checkPointPosition( 0 , 0 , 2 , 1 , 4 , 5 ));
System.out.println(checkPointPosition( 1 , 2 , 200 , 100 , 6 , 5 ));
}
}
|
Python3
import math
def check_point_position(h, k, x, y, a, b):
theta = math.atan2(b * (y - k), a * (x - h))
distance = ((x - h) * math.cos(theta) + (y - k)
* math.sin(theta)) * * 2 / a * * 2
+ ((x - h) * math.sin(theta) - (y - k) * math.cos(theta)) * * 2 / b * * 2
if distance < 1 :
return "Inside"
elif distance > 1 :
return "Outside"
else :
return "On"
print (check_point_position( 0 , 0 , 2 , 1 , 4 , 5 ))
print (check_point_position( 1 , 2 , 200 , 100 , 6 , 5 ))
|
C#
using System;
public class Program {
public static string
CheckPointPosition( double h, double k, double x,
double y, double a, double b)
{
double theta = Math.Atan2(b * (y - k), a * (x - h));
double distance
= Math.Pow((x - h) * Math.Cos(theta)
+ (y - k) * Math.Sin(theta),
2)
/ Math.Pow(a, 2)
+ Math.Pow((x - h) * Math.Sin(theta)
- (y - k) * Math.Cos(theta),
2)
/ Math.Pow(b, 2);
if (distance < 1) {
return "Inside" ;
}
else if (distance > 1) {
return "Outside" ;
}
else {
return "On" ;
}
}
public static void Main()
{
Console.WriteLine(CheckPointPosition(
0, 0, 2, 1, 4, 5));
Console.WriteLine(CheckPointPosition(
1, 2, 200, 100, 6, 5));
}
}
|
Javascript
function checkPointPosition(h, k, x, y, a, b) {
let theta = Math.atan2(b * (y - k), a * (x - h));
let distance = (Math.pow((x - h) * Math.cos(theta) + (y - k) * Math.sin(theta), 2) / Math.pow(a, 2)) +
(Math.pow((x - h) * Math.sin(theta) - (y - k) * Math.cos(theta), 2) / Math.pow(b, 2));
if (distance < 1) {
return "Inside" ;
} else if (distance > 1) {
return "Outside" ;
} else {
return "On" ;
}
}
console.log(checkPointPosition(0, 0, 2, 1, 4, 5));
console.log(checkPointPosition(1, 2, 200, 100, 6, 5));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
04 Nov, 2023
Like Article
Save Article