[英]Finding whether all points are in a rectangle
我有兩個角點定義了一個矩形。 如果所有點都位於矩形的內部或周邊,我需要返回 true。 我編寫的代碼可以區分所有點何時在內部或外部,但我無法弄清楚當數字在矩形內時如何使其返回 True。
此代碼使用“False”正確返回錯誤情況,但對於真實情況返回“None”而不是“True”。
fc=第一個角 sc=第二個角
def Functn1(fc=(0,0), sc=(0,0), p=[]):
x = fc[0]
y = fc[1]
w = sc[0]
h = sc[1]
for i in range(len(p)):
p_x = p[i][0] #Current point x
p_y = p[i][1] #Current point y
if not (p_x >= x and p_x <= w and p_y >= y and p_y <= h):
return False
print(Functn1((0,0), (5,5), [(1,1), (0,0), (5,5)]))
函數的默認返回值為 None。 只需在最后添加 return True 。
def Functn1(fc=(0,0), sc=(0,0), p=[]):
x = fc[0]
y = fc[1]
w = sc[0]
h = sc[1]
for i in range(len(p)):
p_x = p[i][0] #Current point x
p_y = p[i][1] #Current point y
if not (p_x >= x and p_x <= w and p_y >= y and p_y <= h):
return False
return True
print(Functn1((0,0), (5,5), [(1,1), (0,0), (5,5)]))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.