📜  检查点是否在抛物线上、外或内

📅  最后修改于: 2021-10-23 08:22:15             🧑  作者: 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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程