Skip to content
Advertisement

How to create a python script that checks if a given point is inside a certain area?

I want to make a python script which checks this, but the point i will be using are (for example) x = 1290 to 1340 and y = 1460 to 1510 (this means that A = 1290 B = 1340 C = 1460 and D = 1510), i have already made a simple script but it needs me to write every single possible coordinate mannualy… here it is:

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!")

Anyone has tips to make this either easier or make a total different script?

Advertisement

Answer

Assuming that the sides of the square are parallel to the coordinate axis, you just have to check if the x input is between the limits and that the y input is between the limits. Also, assuming the limits are inclusive (i.e. x == 1290 is inside the square). Thus:

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")
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement