使用列表元素的定义来决定哪个给出最小的答案
在下面的代码中,我计算了哪些三角形覆盖了某个点。现在我试图让函数perimeter
计算剩下的三角形的周长,可以在triangle_fit
中找到它。当我知道这些三角形的周长时,我想选择最小的三角形。我做了一些尝试,但该函数只能识别 x1
、y1
、x2
、...
。
import itertools
import math
def area(x1, y1, x2, y2, x3, y3):
return abs((x1 * (y2 - y3) + x2 * (y3 - y1)
+ x3 * (y1 - y2)) / 2.0)
def isInside(x1, y1, x2, y2, x3, y3, x, y):
A = area (x1, y1, x2, y2, x3, y3)
A1 = area (x, y, x2, y2, x3, y3)
A2 = area (x1, y1, x, y, x3, y3)
A3 = area (x1, y1, x2, y2, x, y)
if(A == A1 + A2 + A3):
return True
else:
return False
def perimeter (x1, y1, x2, y2, x3, y3):
return abs(math.sqrt((x1-x2)**2+(y1-y2)**2)+math.sqrt((x2-x3)^2
+(y2-y3)**2)+math.sqrt((x3-x1)**2+(y3-y1)**2))
points = [(0,10), (0,0), (10,0), (10,10), (5,2), (2,2)]
P = (5,1)
triangle_fit = []
for triangle in itertools.combinations(points, 3):
p1, p2, p3 = triangle
if isInside(*p1, *p2, *p3, *P):
triangle_fit.append(triangle)
print(triangle_fit)
In the code below I have calculated which triangles cover a certain point. Now I am trying to let the function perimeter
calculate the perimeter of the left over triangles which can be found in triangle_fit
. And when I know the perimeters of these triangles I want to chose the smallest triangle. I did some attempts but the function will only recognize x1
, y1
, x2
, ...
.
import itertools
import math
def area(x1, y1, x2, y2, x3, y3):
return abs((x1 * (y2 - y3) + x2 * (y3 - y1)
+ x3 * (y1 - y2)) / 2.0)
def isInside(x1, y1, x2, y2, x3, y3, x, y):
A = area (x1, y1, x2, y2, x3, y3)
A1 = area (x, y, x2, y2, x3, y3)
A2 = area (x1, y1, x, y, x3, y3)
A3 = area (x1, y1, x2, y2, x, y)
if(A == A1 + A2 + A3):
return True
else:
return False
def perimeter (x1, y1, x2, y2, x3, y3):
return abs(math.sqrt((x1-x2)**2+(y1-y2)**2)+math.sqrt((x2-x3)^2
+(y2-y3)**2)+math.sqrt((x3-x1)**2+(y3-y1)**2))
points = [(0,10), (0,0), (10,0), (10,10), (5,2), (2,2)]
P = (5,1)
triangle_fit = []
for triangle in itertools.combinations(points, 3):
p1, p2, p3 = triangle
if isInside(*p1, *p2, *p3, *P):
triangle_fit.append(triangle)
print(triangle_fit)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过按周长对三角形列表进行排序来完成此操作,这会将较小的三角形移到开头(请注意,如果它们具有相同的周长,则可以有多个最小的三角形)。
为了使此操作变得更容易,我更改了
perimeter()
函数的调用顺序,以接受三个点的序列,以匹配triangle_fit
列表内容的格式。我还将其更改为使用math.hypot()< /code>
函数计算每对点之间的欧几里德距离。这样做不仅使它更快一点,还修复了代码中的一个错误,我注意到您使用
^
而不是**
进行求幂。You can do it by sorting the list of triangles by their perimeter which will move the smaller ones to the beginning (note there can be more than one that is smallest if they have the same perimeter).
To make doing this easier I changed the calling sequence of your
perimeter()
function to accept a sequence of three points to match the format of the contents of thetriangle_fit
list. I also changed it to use themath.hypot()
function to compute the Euclidean-distance between each pair points. Doing this not only makes it a little faster, it also fixed a bug in your code I noticed where you used^
instead of**
for exponentiation.