[英]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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.