Python 代码判断点是否在正方形内
可以使用以下 Python 代码来判断点是否在正方形内:
def check_point_in_square(x, y):
if x >= -1 and x <= 1 and y >= -1 and y <= 1:
return 'yes'
else:
return 'no'
# 从外部输入点的坐标
point_x = float(input('请输入点的横坐标:'))
point_y = float(input('请输入点的纵坐标:'))
result = check_point_in_square(point_x, point_y)
print(result)
这段代码首先定义了一个名为 check_point_in_square 的函数,该函数接受点的横坐标和纵坐标作为参数。在函数内部,通过判断点的横坐标和纵坐标是否介于正方形的边界范围内来确定点是否在正方形内。
接下来,代码从外部输入点的坐标,并调用 check_point_in_square 函数进行判断。最后,根据函数返回的结果输出 'yes' 或 'no'。
原文地址: https://www.cveoy.top/t/topic/t0M 著作权归作者所有。请勿转载和采集!