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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check whether the given point // lies inside, outside, or on the ellipse string check_point_position( int h, int k, int x, int y, int a, int b) { // Find the value of angle theta double theta = atan2 (b * (y - k), a * (x - h)); // Find the value of distance 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" ; } // Driver Code int main() { cout << check_point_position(0, 0, 2, 1, 4, 5) << endl; // Output: Inside cout << check_point_position(1, 2, 200, 100, 6, 5) << endl; // Output: Outside return 0; } |
Java
import java.util.*; public class Main { // Function to check whether the given point // lies inside, outside, or on the ellipse public static String checkPointPosition( int h, int k, int x, int y, int a, int b) { // Find the value of angle theta double theta = Math.atan2(b * (y - k), a * (x - h)); // Find the value of distance 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" ; } } // Driver Code public static void main(String[] args) { System.out.println(checkPointPosition( 0 , 0 , 2 , 1 , 4 , 5 )); // Output: Inside System.out.println(checkPointPosition( 1 , 2 , 200 , 100 , 6 , 5 )); // Output: Outside } } |
Python3
# Python program for the above approach import math # Function to check whether the given point # lies inside, outside, or on the ellipse def check_point_position(h, k, x, y, a, b): # Find the value of angle theta theta = math.atan2(b * (y - k), a * (x - h)) # Find the value of distance 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" # Driver Code print (check_point_position( 0 , 0 , 2 , 1 , 4 , 5 )) # Output: Inside print (check_point_position( 1 , 2 , 200 , 100 , 6 , 5 )) # Output: Outside |
C#
using System; public class Program { // Function to check whether the given point // lies inside, outside, or on the ellipse public static string CheckPointPosition( double h, double k, double x, double y, double a, double b) { // Find the value of angle theta double theta = Math.Atan2(b * (y - k), a * (x - h)); // Find the value of distance 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)); // Output: Inside Console.WriteLine(CheckPointPosition( 1, 2, 200, 100, 6, 5)); // Output: Outside } } |
Javascript
function checkPointPosition(h, k, x, y, a, b) { // Find the value of angle theta let theta = Math.atan2(b * (y - k), a * (x - h)); // Find the value of distance 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" ; } } // Driver Code console.log(checkPointPosition(0, 0, 2, 1, 4, 5)); // Output: Inside console.log(checkPointPosition(1, 2, 200, 100, 6, 5)); // Output: Outside |
Inside Outside
Time Complexity: O(1)
Auxiliary Space: O(1)
Check if a point is inside, outside or on the ellipse
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++
// C++ Program to check if the point // lies within the ellipse or not #include <bits/stdc++.h> using namespace std; // Function to check the point double checkpoint( double h, double k, double x, double y, double a, double b) { // checking the equation of // ellipse with the given point double p = ( pow ((x - h), 2) / pow (a, 2)) + ( pow ((y - k), 2) / pow (b, 2)); return p; } // Driver code 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
// Java Program to check if the point // lies within the ellipse or not import java.util.*; class solution { // Function to check the point static double checkpoint( double h, double k, double x, double y, double a, double b) { // checking the equation of // ellipse with the given point 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; } // Driver code 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" ); } } // This code is contributed by Surendra_Gangwar |
Python 3
# Python 3 Program to check if # the point lies within the # ellipse or not import math # Function to check the point def 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 code 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" ) # This code is contributed # by ChitraNayal |
C#
// C# Program to check if the point // lies within the ellipse or not using System; class GFG { // Function to check the point static double checkpoint( double h, double k, double x, double y, double a, double b) { // checking the equation of // ellipse with the given point 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; } // Driver code 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" ); } } // This code is contributed by inder_verma |
Javascript
<script> // javascript Program to check if the point // lies within the ellipse or not // Function to check the point function checkpoint(h , k , x , y , a , b) { // checking the equation of // ellipse with the given point var p = Math.pow((x - h), 2) / Math.pow(a, 2) + Math.pow((y - k), 2) / 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> |
PHP
<?php // PHP Program to check if the point // lies within the ellipse or not // Function to check the point function 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 ?> |
Inside
Time complexity: O(1)
Auxiliary Space: O(1)