使用列表元素的定义来决定哪个给出最小的答案

发布于 2025-01-14 05:39:34 字数 1094 浏览 2 评论 0原文

在下面的代码中,我计算了哪些三角形覆盖了某个点。现在我试图让函数perimeter计算剩下的三角形的周长,可以在triangle_fit中找到它。当我知道这些三角形的周长时,我想选择最小的三角形。我做了一些尝试,但该函数只能识别 x1y1x2...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹泡泡o 2025-01-21 05:39:34

您可以通过按周长对三角形列表进行排序来完成此操作,这会将较小的三角形移到开头(请注意,如果它们具有相同的周长,则可以有多个最小的三角形)。

为了使此操作变得更容易,我更改了 perimeter() 函数的调用顺序,以接受三个点的序列,以匹配 triangle_fit 列表内容的格式。我还将其更改为使用 math.hypot()< /code>函数计算每对点之间的欧几里德距离。这样做不仅使它更快一点,还修复了代码中的一个错误,我注意到您使用 ^ 而不是 ** 进行求幂。

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 is_inside(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)
    return A == A1 + A2 + A3


def perimeter(points):
    (x1, y1), (x2, y2), (x3, y3) = points
    return (math.hypot(x1-x2, y1-y2) +
            math.hypot(x2-x3, y2-y3) +
            math.hypot(x3-x1, y3-y1))


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 is_inside(*p1, *p2, *p3, *P):
       triangle_fit.append(triangle)

triangle_fit.sort(key=perimeter)  # Sort by perimeter.

for points in triangle_fit:
    print(f'{points}: area={area(*points[0], *points[1], *points[2]):.2f}')

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 the triangle_fit list. I also changed it to use the math.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.

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 is_inside(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)
    return A == A1 + A2 + A3


def perimeter(points):
    (x1, y1), (x2, y2), (x3, y3) = points
    return (math.hypot(x1-x2, y1-y2) +
            math.hypot(x2-x3, y2-y3) +
            math.hypot(x3-x1, y3-y1))


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 is_inside(*p1, *p2, *p3, *P):
       triangle_fit.append(triangle)

triangle_fit.sort(key=perimeter)  # Sort by perimeter.

for points in triangle_fit:
    print(f'{points}: area={area(*points[0], *points[1], *points[2]):.2f}')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文