日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python判断点在矩形内_Python测试点是否在矩形中

發(fā)布時間:2025/3/21 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python判断点在矩形内_Python测试点是否在矩形中 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

我正在嘗試編寫以下Python腳本:

>創(chuàng)造四個點

>創(chuàng)建四個矩形

>檢查每個點是否在任何矩形中,然后將結果寫到輸出文件中.

問題涉及兩個數據結構Point和Rectangle類.我已經開始創(chuàng)建Point類和Rectangle類.矩形類應包含通過隨機模塊的random方法創(chuàng)建的相關數據集.從我的嘗試中可以看出,我到處都是,但是我已經使用#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

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

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+'

')

f.close()

總結

以上是生活随笔為你收集整理的python判断点在矩形内_Python测试点是否在矩形中的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。