Python 测试点是否在矩形中
Python Test If Point is in Rectangle
我是 python 的新手,仍在学习中,但我希望有更多经验的人可以帮助我。
我正在尝试编写一个 python 脚本:
- 创造四点
- 创建四个矩形
- 检查每个点是否在任何矩形中,然后将结果写入输出文件。
该题涉及Point和Rectangle两个数据结构class。我已经开始创建点 class 和矩形 classes。矩形 class 应该包含从随机模块的随机方法创建的相关数据集。从我的尝试中你可以看出,我有点到处都是,但我已经使用#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 < x2
和 y1 < y2
(如果不是,你可以交换它们),一个点 (x,y) 在其中如果 x1 < x < x2 and y1 < y < y2
,则为矩形。由于可以链接 Python 比较运算符,因此这甚至是有效的 Python 代码,它应该会产生正确的结果(在其他语言中,您必须编写类似 x1 < x and x < x2
等的代码)。
如果需要,可以使用 <=
而不是 <
。使用 <=
表示矩形边界上的点(例如,点 (x1,y1))算作在矩形内部,而使用 <
表示这些点在矩形外部。
最好单独编写一个函数来完成这项工作。这是我的功能。想复制就复制
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
我是 python 的新手,仍在学习中,但我希望有更多经验的人可以帮助我。
我正在尝试编写一个 python 脚本:
- 创造四点
- 创建四个矩形
- 检查每个点是否在任何矩形中,然后将结果写入输出文件。
该题涉及Point和Rectangle两个数据结构class。我已经开始创建点 class 和矩形 classes。矩形 class 应该包含从随机模块的随机方法创建的相关数据集。从我的尝试中你可以看出,我有点到处都是,但我已经使用#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 < x2
和 y1 < y2
(如果不是,你可以交换它们),一个点 (x,y) 在其中如果 x1 < x < x2 and y1 < y < y2
,则为矩形。由于可以链接 Python 比较运算符,因此这甚至是有效的 Python 代码,它应该会产生正确的结果(在其他语言中,您必须编写类似 x1 < x and x < x2
等的代码)。
如果需要,可以使用 <=
而不是 <
。使用 <=
表示矩形边界上的点(例如,点 (x1,y1))算作在矩形内部,而使用 <
表示这些点在矩形外部。
最好单独编写一个函数来完成这项工作。这是我的功能。想复制就复制
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