【发布时间】:2012-11-25 18:10:36
【问题描述】:
首先,我对 Python(编程领域)还很陌生,但我想学习和转换 jwpat7 开发的函数。给定一组从凸包导出的点
hull= [(560023.44957588764,6362057.3904932579),
(560023.44957588764,6362060.3904932579),
(560024.44957588764,6362063.3904932579),
(560026.94957588764,6362068.3904932579),
(560028.44957588764,6362069.8904932579),
(560034.94957588764,6362071.8904932579),
(560036.44957588764,6362071.8904932579),
(560037.44957588764,6362070.3904932579),
(560037.44957588764,6362064.8904932579),
(560036.44957588764,6362063.3904932579),
(560034.94957588764,6362061.3904932579),
(560026.94957588764,6362057.8904932579),
(560025.44957588764,6362057.3904932579),
(560023.44957588764,6362057.3904932579)]
此脚本返回此post problem 之后所有可能区域的打印。代码由 jwpat7 是:
import math
def mostfar(j, n, s, c, mx, my): # advance j to extreme point
xn, yn = hull[j][0], hull[j][1]
rx, ry = xn*c - yn*s, xn*s + yn*c
best = mx*rx + my*ry
while True:
x, y = rx, ry
xn, yn = hull[(j+1)%n][0], hull[(j+1)%n][1]
rx, ry = xn*c - yn*s, xn*s + yn*c
if mx*rx + my*ry >= best:
j = (j+1)%n
best = mx*rx + my*ry
else:
return (x, y, j)
n = len(hull)
iL = iR = iP = 1 # indexes left, right, opposite
pi = 4*math.atan(1)
for i in range(n-1):
dx = hull[i+1][0] - hull[i][0]
dy = hull[i+1][1] - hull[i][1]
theta = pi-math.atan2(dy, dx)
s, c = math.sin(theta), math.cos(theta)
yC = hull[i][0]*s + hull[i][1]*c
xP, yP, iP = mostfar(iP, n, s, c, 0, 1)
if i==0: iR = iP
xR, yR, iR = mostfar(iR, n, s, c, 1, 0)
xL, yL, iL = mostfar(iL, n, s, c, -1, 0)
area = (yP-yC)*(xR-xL)
print ' {:2d} {:2d} {:2d} {:2d} {:9.3f}'.format(i, iL, iP, iR, area)
结果是:
i iL iP iR Area
0 6 8 0 203.000
1 6 8 0 211.875
2 6 8 0 205.800
3 6 10 0 206.250
4 7 12 0 190.362
5 8 0 1 203.000
6 10 0 4 201.385
7 0 1 6 203.000
8 0 3 6 205.827
9 0 3 6 205.640
10 0 4 7 187.451
11 0 4 7 189.750
12 1 6 8 203.000
我希望创建一个函数,返回最小矩形的长度、宽度和面积。例如:
Length, Width, Area = get_minimum_area_rectangle(hull)
print Length, Width, Area
18.036, 10.392, 187.451
我的问题是:
- 我需要创建一个函数还是两个函数。例如:定义 mostfar 和 get_minimum_area_rectangle
- hull 是一个值列表。它是最好的格式吗?
- 遵循单一功能的方法,我在最远的内部集成时遇到问题
提前致谢
1) 解决方案:一个函数 按照 Scott Hunter 建议的第一个解决方案,我在将 mostfar() 集成到 get_minimum_area_rectangle() 中时遇到了问题。任何建议或帮助都非常感谢,因为我可以学习。
#!/usr/bin/python
import math
def get_minimum_area_rectangle(hull):
# get pi greek
pi = 4*math.atan(1)
# number of points
n = len(hull)
# indexes left, right, opposite
iL = iR = iP = 1
# work clockwise direction
for i in range(n-1):
# distance on x axis
dx = hull[i+1][0] - hull[i][0]
# distance on y axis
dy = hull[i+1][1] - hull[i][1]
# get orientation angle of the edge
theta = pi-math.atan2(dy, dx)
s, c = math.sin(theta), math.cos(theta)
yC = hull[i][0]*s + hull[i][1]*c
在上面的 jwpat7 示例之后,我需要使用 mostfar()。在这一点上,我很难理解如何整合(对不起,这个术语不正确)
【问题讨论】:
-
回答关于您选择的解决方案的问题:只需缩进
def mostfar:...代码并将其放在开头的def get_minimum_area_rectangle:内。 -
@martineau,谢谢。但是你认为把 def mostfar 放在 def get_minimum_area_rectangle 里面是优雅的吗?你知道一些优雅的方法来解决这个问题吗?再次感谢
-
它只是有点不优雅,特别是因为它确实是一个本地函数。在每次调用外部函数时都会执行函数定义的意义上,这有点低效。这样做可以避免这种情况,如果您在其名称中添加下划线前缀,它将被视为私有模块函数。实现此目的的另一种方法是将其作为
_mostfar()方法放入一个类中。然后get_minimum_area_rectangle可以是一个单独的方法来调用它或重命名类的__call__()方法(使其成为“函子”或函数对象类)。 -
@martineau。如果你有时间(我不希望滥用你的时间)。为了学习和提高,我可以问你一个写的例子吗?提前致谢
标签: python performance algorithm coding-style styles