如何创建一个 python 脚本来检查给定点是否在特定区域内?
How to create a python script that checks if a given point is inside a certain area?
我想制作一个 python 脚本来检查这个,但是我将使用的点是(例如)x = 1290 到 1340 和 y = 1460 到 1510(这意味着 A = 1290 B = 1340 C = 1460 和 D = 1510),我已经制作了一个简单的脚本,但它需要我手动编写每个可能的坐标...这里是:
print ("")
print ("1923 1682")
print ("")
print ("Click below here to type!")
NFGCOORDINATES = (//here every single possible coordinate will come)
guess = str(input())
if guess in NFGCOORDINATES:
print ("Coordinates are in NFG area")
print ("")
print ("Made by El_Capitano with love!")
else:
print ("Coordinates are NOT in NFG area")
print ("")
print ("Made by El_Capitano with love!")
任何人有任何提示可以使这更容易或制作完全不同的脚本吗?
假设正方形的边平行于坐标轴,您只需检查 x
输入是否在限制之间以及 y
输入是否在限制之间。此外,假设限制是包容性的(即 x == 1290
在正方形内)。因此:
print('Your input should be of the form: 1923 1682')
guess = input()
try:
x, y = [int(val) for val in guess.split()]
except: # This is a very crude check.
print('Wrong input format')
raise
# Limits of the rectangle
x_low = 1290
x_high = 1340
y_low = 1460
y_high = 1510
if (x_low <= x <= x_high) and (y_low <= y <= y_high):
print ("Coordinates are in NFG area")
else:
print ("Coordinates are NOT in NFG area")
我想制作一个 python 脚本来检查这个,但是我将使用的点是(例如)x = 1290 到 1340 和 y = 1460 到 1510(这意味着 A = 1290 B = 1340 C = 1460 和 D = 1510),我已经制作了一个简单的脚本,但它需要我手动编写每个可能的坐标...这里是:
print ("")
print ("1923 1682")
print ("")
print ("Click below here to type!")
NFGCOORDINATES = (//here every single possible coordinate will come)
guess = str(input())
if guess in NFGCOORDINATES:
print ("Coordinates are in NFG area")
print ("")
print ("Made by El_Capitano with love!")
else:
print ("Coordinates are NOT in NFG area")
print ("")
print ("Made by El_Capitano with love!")
任何人有任何提示可以使这更容易或制作完全不同的脚本吗?
假设正方形的边平行于坐标轴,您只需检查 x
输入是否在限制之间以及 y
输入是否在限制之间。此外,假设限制是包容性的(即 x == 1290
在正方形内)。因此:
print('Your input should be of the form: 1923 1682')
guess = input()
try:
x, y = [int(val) for val in guess.split()]
except: # This is a very crude check.
print('Wrong input format')
raise
# Limits of the rectangle
x_low = 1290
x_high = 1340
y_low = 1460
y_high = 1510
if (x_low <= x <= x_high) and (y_low <= y <= y_high):
print ("Coordinates are in NFG area")
else:
print ("Coordinates are NOT in NFG area")