Open In App

Check if a point is inside, outside or on a Hyperbola

Improve
Improve
Like Article
Like
Save
Share
Report

Given a hyperbola 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) lies within the area bounded by the hyperbola or not.

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: The given problem can be solved by solving the equation of hyperbola mentioned below, for the given point (x, y):

  \frac{(x-h)^2}{a^2}  - \frac{(y-k)^2}{b^2}

Based on the evaluated value of the above equation, the output of the program is as follows:

  • Less than 1: The point lies inside the hyperbola.
  • Equal to 1: The point lies on the hyperbola
  • Greater than 1: The point lies outside the hyperbola.

Below is the implementation of the above approach: 

C++

// C++ program for the
// above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the point
// (x, y) lies inside, on or
// outside the given hyperbola
void checkpoint(int h, int k, int x,
                int y, int a, int b)
{
 
    // Stores the value of the equation
    int p = (pow((x - h), 2) / pow(a, 2))
            - (pow((y - k), 2) / pow(b, 2));
 
    // Generate output based on value of p
    if (p > 1) {
        cout << "Outside";
    }
    else if (p == 1) {
        cout << "On the Hyperbola";
    }
    else {
        cout << "Inside";
    }
}
 
// Driver Code
int main()
{
    int h = 0, k = 0, x = 2;
    int y = 1, a = 4, b = 5;
 
    checkpoint(h, k, x, y, a, b);
 
    return 0;
}

                    

Java

// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to check if the point
// (x, y) lies inside, on or
// outside the given hyperbola
static void checkpoint(int h, int k, int x,
                       int y, int a, int b)
{
     
    // Stores the value of the equation
    int p = (int)(Math.pow((x - h), 2) / Math.pow(a, 2)) -
            (int)(Math.pow((y - k), 2) / Math.pow(b, 2));
 
    // Generate output based on value of p
    if (p > 1)
    {
        System.out.println("Outside");
    }
    else if (p == 1)
    {
        System.out.println("On the Hyperbola");
    }
    else
    {
        System.out.println("Inside");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int h = 0, k = 0, x = 2;
    int y = 1, a = 4, b = 5;
 
    checkpoint(h, k, x, y, a, b);
}
}
 
// This code is contributed by Kingash

                    

Python3

# Python3 program for the above approach
from math import pow
 
# Function to check if the point
# (x, y) lies inside, on or
# outside the given hyperbola
def checkpoint(h, k, x, y, a, b):
 
    # Stores the value of the equation
    p = ((pow((x - h), 2) // pow(a, 2)) -
         (pow((y - k), 2) // pow(b, 2)))
 
    # Generate output based on value of p
    if (p > 1):
        print("Outside")
     
    elif (p == 1):
        print("On the Hyperbola");
     
    else:
        print("Inside")
 
# Driver Code
if __name__ == "__main__":
 
    h = 0
    k = 0
    x = 2
    y = 1
    a = 4
    b = 5
 
    checkpoint(h, k, x, y, a, b)
 
# This code is contributed by AnkThon

                    

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if the point
// (x, y) lies inside, on or
// outside the given hyperbola
static void checkpoint(int h, int k, int x,
                       int y, int a, int b)
{
     
    // Stores the value of the equation
    int p = (int)(Math.Pow((x - h), 2) / Math.Pow(a, 2)) -
            (int)(Math.Pow((y - k), 2) / Math.Pow(b, 2));
 
    // Generate output based on value of p
    if (p > 1)
    {
        Console.WriteLine("Outside");
    }
    else if (p == 1)
    {
        Console.WriteLine("On the Hyperbola");
    }
    else
    {
        Console.WriteLine("Inside");
    }
}
 
// Driver Code
public static void Main(string[] args)
{
    int h = 0, k = 0, x = 2;
    int y = 1, a = 4, b = 5;
 
    checkpoint(h, k, x, y, a, b);
}
}
 
// This code is contributed by ukasp

                    

Javascript

<script>
 
// JavaScript program for the above approach
 
// Function to check if the point
// (x, y) lies inside, on or
// outside the given hyperbola
function checkpoint(h, k, x, y, a, b)
{
     
    // Stores the value of the equation
    p = ((Math.pow((x - h), 2) / Math.pow(a, 2)) -
         (Math.pow((y - k), 2) / Math.pow(b, 2)))
 
    // Generate output based on value of p
    if (p > 1)
        console.log("Outside");
     
    else if (p == 1)
        console.log("On the Hyperbola");
     
    else
        console.log("Inside");
}
 
// Driver Code
function main()
{
    var h = 0;
    var k = 0;
    var x = 2;
    var y = 1;
    var a = 4;
    var b = 5;
     
    checkpoint(h, k, x, y, a, b);
}
 
// main function call
main()
 
// This code is contributed by AnkThon
 
</script>

                    

Output: 
Inside

 

Time Complexity: O(1)
Auxiliary Space: O(1)


 



Last Updated : 25 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads