簡體   English   中英

Python測試點是否在矩形中

[英]Python Test If Point is in Rectangle

我是 python 新手,仍在學習繩索,但我希望有更多經驗的人可以幫助我。

我正在嘗試編寫一個 python 腳本:

  1. 創建四個點
  2. 創建四個矩形
  3. 檢查每個點是否在任何矩形中,然后將結果寫入輸出文件。

問題涉及到兩個數據結構 Point 和 Rectangle 類。 我已經開始創建 Point 類和 Rectangle 類。 Rectangle 類應該保存從 random 模塊的 random 方法創建的相關數據集。 正如您從我的嘗試中可以看出的那樣,我到處都是,但我已經使用 #comments 來嘗試實現我想要做的事情。

我的具體問題是:
1)我怎樣才能讓這個腳本工作?
2)我缺少哪些變量或函數來生成隨機矩形並查看特定點是否在這些矩形中?

## 1. Declare the Point class
class Point:
    def __init__(self,x = 0.0, y = 0.0): 
        self.x = x 
        self.y = y
    pass
## 2. Declare the Rectangle class 
class Rectangle: 
    def __int__(self): ## A rectangle can be determined aby (minX, maxX) (minY, maxY) 
        self.minX = self.minY = 0.0 
        self.maxX = self.maxY = 1.0 
    def contains(self, point): ## add code to check if a point is within a rectangle 
        """Return true if a point is inside the rectangle."""
        # Determine if a point is inside a given polygon or not
        # Polygon is a list of (x,y) pairs. This function
        # returns True or False. 
    def point_in_poly(x,y,poly):
        n = len(poly)
        inside = False
        p1x,p1y = poly[0]
        for i in range(n+1):
            p2x,p2y = poly[i % n]
            if y > min(p1y,p2y):
                if y <= max(p1y,p2y):
                    if x <= max(p1x,p2x):
                        if p1y != p2y:
                            xints = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
                    if p1x == p2x or x <= xints:
                        inside = not inside
            p1x,p1y = p2x,p2y
        return inside
## 3. Generate four points 
##define a Point list to keep four points 
points = []
##add codes to generate four points and append to the points list
polygon = [(0,10),(10,10),(10,0),(0,0)]
point_x = 5
point_y = 5

## 4. Generate four rectangles 
##define a Rectangle list 
rects = [] 
for i in range(4):
    rectangle = Rectangle() 
    ## Generate x 
    x1 = random.random() 
    x2 = random.random() 
    ## make sure minX != maxX 
    while(x1 == x2): 
        x1 = random.random() 
    if x1<x2: 
        rectangle.minX=x1 
        rectangle.maxX=x2 
    elif x1>x2:
        rectangle.minX=x2
        rectangle.maxX=x1
    rects.append(rectangle)
    ## Develop codes to generate y values below 
    ## make sure minY != maxY 
    while(y1 == y2):
        y1 = random.random()
    if y1<y2:
        rectangle.minY=y1
        rectangle.maxY=y2
    elif y1>y2:
        recetangle.minY=y2
        racetangle.maxY=y1
    ## add to the list 
    rects.append(rectangle)

## 5. Add code to check which point is in which rectangle 
resultList = [] ## And use a list to keep the results 
for i in range(4):
    for j in range(4):
        if points[i] in rectangle[j]:
            print i

# write the results to file
f=open('Code5_4_1_Results.txt','w') 
for result in resultList:
    f.write(result+'\n') 
f.close()

這是非常簡單的數學。 給定一個包含點 (x1,y1) 和 (x2,y2) 的矩形,並假設x1 < x2y1 < y2 (如果沒有,您可以交換它們),如果x1 < x < x2 and y1 < y < y2 ,則點 (x,y) 位於該矩形內x1 < x < x2 and y1 < y < y2 由於可以鏈接 Python 比較運算符,這甚至是應該產生正確結果的有效 Python 代碼(在其他語言中,您必須編寫類似x1 < x and x < x2等的內容)。

如果需要,您可以使用<=而不是< 使用<=表示矩形邊界上的點(例如,點 (x1,y1))算作在矩形內部,而使用< so 表示這些點在矩形外部。

最好編寫一個單獨的函數來完成這項工作。 這是我的功能。 如果你願意,你可以復制它

def pointInRect(point,rect):
    x1, y1, w, h = rect
    x2, y2 = x1+w, y1+h
    x, y = point
    if (x1 < x and x < x2):
        if (y1 < y and y < y2):
            return True
    return False

暫無
暫無

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM