📜  检查点在抛物线的内部,外部还是上方

📅  最后修改于: 2021-05-05 01:38:49             🧑  作者: Mango

给定一个顶点为(h,k)的抛物线,并且a   ,即焦点与顶点之间的距离。任务是确定点(x,y)是在抛物线的内部,外部还是上方。
例子

Input: h = 100, k = 500, x = 20, y = 10, a = 4 
Output: Outside

Input: h = 0, k = 0, x = 2, y = 1, a = 4
Output: Inside

方法:非常简单,我们只需要求解点(x,y)的方程式

求解后,如果结果小于0,则该点位于抛物线;否则,如果结果恰好为0,则该点位于抛物线上;如果结果大于0,则未满足要求的点位于抛物线之外
在这里,我们采用对称轴为y = k的抛物线,尽管该方法适用于任何抛物线。
下面是上述方法的实现:

C++
// C++ Program to check if the point
// lies within the parabola or not
#include 
using namespace std;
 
// Function to check the point
int checkpoint(int h, int k, int x, int y, int a)
{
 
    // checking the equation of
    // parabola with the given point
    int p = pow((y - k), 2) - 4 * a * (x - h);
 
    return p;
}
 
// Driver code
int main()
{
    int h = 0, k = 0, x = 2, y = 1, a = 4;
 
    if (checkpoint(h, k, x, y, a) > 0)
        cout << "Outside" << endl;
 
    else if (checkpoint(h, k, x, y, a) == 0)
        cout << "On the parabola" << endl;
 
    else
        cout << "Inside" << endl;
 
    return 0;
}


Java
// Java Program to check if the point
// lies within the parabola or not
 
class solution
{
 
// Function to check the point
static int checkpoint(int h, int k, int x, int y, int a)
{
    // checking the equation of
    // parabola with the given point
    int p =(int) Math.pow((y - k), 2) - 4 * a * (x - h);
 
    return p;
}
 
//driver code
public static void main(String arr[])
{
 
    int h = 0, k = 0, x = 2, y = 1, a = 4;
 
    if (checkpoint(h, k, x, y, a) > 0)
    System.out.println("Outside");
 
    else if (checkpoint(h, k, x, y, a) == 0)
    System.out.println("On the parabola");
 
    else
    System.out.println("Inside");
 
}
}


Python3
# Python3 Program to check if the point
# lies within the parabola or not
 
#  Function to check the point
def checkpoint(h, k, x, y, a):
 
    # checking the equation of
    # parabola with the given point
    p = pow((y - k), 2) - 4 * a * (x - h)
 
    return p
 
# Driver code
if __name__ == "__main__" :
     
    h = 0
    k = 0
    x = 2
    y = 1
    a = 4
 
    if checkpoint(h, k, x, y, a) > 0:
        print ("Outside\n")
 
    elif checkpoint(h, k, x, y, a) == 0:
        print ("On the parabola\n")
 
    else:
        print ("Inside\n");
         
# This code is contributed by
# Surendra_Gangwar


C#
// C# Program to check if the point
// lies within the parabola or not
using System;
 
class GFG
{
 
// Function to check the point
public static int checkpoint(int h, int k,
                             int x, int y,
                             int a)
{
    // checking the equation of
    // parabola with the given point
    int p = (int) Math.Pow((y - k), 2) -
                            4 * a * (x - h);
 
    return p;
}
 
// Driver code
public static void Main(string[] arr)
{
    int h = 0, k = 0,
        x = 2, y = 1, a = 4;
 
    if (checkpoint(h, k, x, y, a) > 0)
    {
        Console.WriteLine("Outside");
    }
 
    else if (checkpoint(h, k, x, y, a) == 0)
    {
        Console.WriteLine("On the parabola");
    }
 
    else
    {
        Console.WriteLine("Inside");
    }
}
}
 
// This code is contributed
// by Shrikant13


PHP
 0)
    echo "Outside";
else if (checkpoint($h, $k, $x,
                    $y, $a) == 0)
    echo "On the parabola";
else
    echo "Inside";
 
// This code is contributed
// by inder_verma
?>


Javascript


输出:
Inside