给定一个以(h, k)为中心的椭圆,长半轴a ,短半轴b ,都与笛卡尔平面对齐。任务是确定点(x, y)是否在椭圆所包围的区域内。
例子:
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
方法:我们必须求解给定点(x, y)的椭圆方程,
(x-h)^2/a^2 + (y-k)^2/b^2 <= 1
如果在不等式中,结果小于 1,则该点位于内,否则,如果恰好为1,则该点位于椭圆上,如果不满足不等式,则该点位于椭圆外。
下面是上述方法的实现:
C++
// C++ Program to check if the point
// lies within the ellipse or not
#include
using namespace std;
// Function to check the point
int checkpoint(int h, int k, int x, int y, int a, int b)
{
// checking the equation of
// ellipse with the given point
int p = (pow((x - h), 2) / pow(a, 2))
+ (pow((y - k), 2) / pow(b, 2));
return p;
}
// Driver code
int main()
{
int 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 int checkpoint(int h, int k, int x, int y, int a, int b)
{
// checking the equation of
// ellipse with the given point
int p = ((int)Math.pow((x - h), 2) / (int)Math.pow(a, 2))
+ ((int)Math.pow((y - k), 2) / (int)Math.pow(b, 2));
return p;
}
//Driver code
public static void main(String arr[])
{
int 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 int checkpoint(int h, int k, int x,
int y, int a, int b)
{
// checking the equation of
// ellipse with the given point
int p = ((int)Math.Pow((x - h), 2) /
(int)Math.Pow(a, 2)) +
((int)Math.Pow((y - k), 2) /
(int)Math.Pow(b, 2));
return p;
}
// Driver code
public static void Main()
{
int 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
PHP
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
?>
Javascript
输出:
Inside
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。