Python 如何检查坐标是否位于某个区域?
我需要创建代码,允许您检查您输入的坐标是否位于某个区域内。到目前为止,我有:Python 如何检查坐标是否位于某个区域?,python,arrays,loops,coordinates,Python,Arrays,Loops,Coordinates,我需要创建代码,允许您检查您输入的坐标是否位于某个区域内。到目前为止,我有: import random import math import pylab import numpy pylab.close("all") #All import statements x = [(random.randint(-50,50)) for i in range(10)]
import random
import math
import pylab
import numpy
pylab.close("all") #All import statements
x = [(random.randint(-50,50)) for i in range(10)] #Creating list of x coordinates
y = [(random.randint(-50,50)) for j in range(10)] #Creating list of y coordinates
array=zip(x,y) #Creating an array by combining the x and y coordinates
print array
counter = 0 #Start of 1c, resetting counter
for i, j in array: #Telling what to inspect
if 35**2 <= (i**2+j**2) <= 65**2: #Conditions for the coordinates to fall within 15 pixels of a circle with radius 50
counter+= 1 #If conditions met then add 1 to counter
n=(1.0*counter/7000)*100 #Calculating percentage of coordinates in the region
print "on average", n, "% of the locations in the array fall in this region" #Print result, end of part 1c
name = raw_input('type a coordinate location: ') #Start of 1d, python input result
for i, j in name:
if i in name in array:
if 35**2 <= (i**2+j**2) <= 65**2:
print "warning, your chosen location falls near the edge of the circle"
else:
print "coordinate does not exist"
随机导入
输入数学
进口派拉布
进口numpy
pylab.close(“全部”)#所有导入语句
x=[(random.randint(-50,50))表示范围(10)内的i]#创建x坐标列表
y=[(random.randint(-50,50))表示范围(10)内的j]#创建y坐标列表
array=zip(x,y)#通过组合x和y坐标创建数组
打印阵列
计数器=0#1c开始,重置计数器
对于数组中的i,j:#告诉要检查的内容
如果35**2名称是字符串。你不能使用
名称中的i,j:
您需要首先将其分解为值,然后创建一个元组
也许是这样的:
n=(name.split(',')[0],name.split(',')[1])-假设坐标以“,”分隔,““原始输入”返回一个字符串。您需要拆分()它并将数字转换为整数请不要使用单字母变量名。首先,它们在代码中不容易识别。而且有意义的名称使代码更容易阅读。当然,我可能想到的唯一有意义的单字母变量名是x、y和zso,这行代码会去哪里?这行代码只是一个演示,它是否会取代“for i,j in name:”。您应该用n
替换name
,但要使用比n
更好的名称。它当然在for循环之前…好的,它的目的是将输入的内容拆分为实际值?使用此选项,用户将如何在提示时输入坐标。他们会像“x,y”一样进入吗?