查找点是否在圆内
给定一个圆(中心和半径的坐标) 和一个点(坐标),请确定该点是否位于圆内或圆上。
Input: x = 4, y = 4 // Given Point
circle_x = 1, circle_y = 1, rad = 6; // Circle
Output: Inside
Input: x = 3, y = 3 // Given Point
circle_x = 0, circle_y = 1, rad = 2; // Circle
Output: Outside
强烈建议您最小化浏览器,然后自己尝试。
这个想法是计算点到中心的距离。如果距离小于或等于半径。要点在里面,在外面。
以下是上述想法的实现。
C++
// C++ program to check if a point
// lies inside a circle or not
#include
using namespace std;
bool isInside(int circle_x, int circle_y,
int rad, int x, int y)
{
// Compare radius of circle with distance
// of its center from given point
if ((x - circle_x) * (x - circle_x) +
(y - circle_y) * (y - circle_y) <= rad * rad)
return true;
else
return false;
}
// Driver function
int main()
{
int x = 1, y = 1;
int circle_x = 0, circle_y = 1, rad = 2;
isInside(circle_x, circle_y, rad, x, y) ?
cout << "Inside" : cout << "Outside";
}
Java
// Java program to check if a point lies
// inside a circle or not
class GFG {
static boolean isInside(int circle_x, int circle_y,
int rad, int x, int y)
{
// Compare radius of circle with
// distance of its center from
// given point
if ((x - circle_x) * (x - circle_x) +
(y - circle_y) * (y - circle_y) <= rad * rad)
return true;
else
return false;
}
// Driver Program to test above function
public static void main(String arg[])
{
int x = 1, y = 1;
int circle_x = 0, circle_y = 1, rad = 2;
if (isInside(circle_x, circle_y, rad, x, y))
System.out.print("Inside");
else
System.out.print("Outside");
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python3 program to check if
# a point lies inside a circle
# or not
def isInside(circle_x, circle_y, rad, x, y):
# Compare radius of circle
# with distance of its center
# from given point
if ((x - circle_x) * (x - circle_x) +
(y - circle_y) * (y - circle_y) <= rad * rad):
return True;
else:
return False;
# Driver Code
x = 1;
y = 1;
circle_x = 0;
circle_y = 1;
rad = 2;
if(isInside(circle_x, circle_y, rad, x, y)):
print("Inside");
else:
print("Outside");
# This code is contributed
# by mits.
C#
// C# program to check if a point lies
// inside a circle or not
using System;
class GFG {
static bool isInside(int circle_x, int circle_y,
int rad, int x, int y)
{
// Compare radius of circle with
// distance of its center from
// given point
if ((x - circle_x) * (x - circle_x) +
(y - circle_y) * (y - circle_y) <= rad * rad)
return true;
else
return false;
}
// Driver Program to test above function
public static void Main()
{
int x = 1, y = 1;
int circle_x = 0, circle_y = 1, rad = 2;
if (isInside(circle_x, circle_y, rad, x, y))
Console.Write("Inside");
else
Console.Write("Outside");
}
}
// This code is contributed by nitin mittal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论