找到坐标外所有可能的三角形(python)

发布于 2025-01-14 04:30:30 字数 1510 浏览 2 评论 0原文

大家好,我是新来的。到目前为止,我已经从 StackOverflow 获得了很多信息,谢谢大家。

我有 6 个坐标:

A:(4,2) 乙:(6,1) C: (9,4) d: (8,9) E: (5,2) F: (2,2)

我有一个测量位置,假设 P=(3,7)

我想找出可以形成覆盖位置 P 的三角形的所有点组合。我已经有一个代码计算一个位置是否在三角形内:

# A utility function to calculate area
# of triangle formed by (x1, y1),
# (x2, y2) and (x3, y3)

def area(x1, y1, x2, y2, x3, y3):

    return abs((x1 * (y2 - y3) + x2 * (y3 - y1)
                + x3 * (y1 - y2)) / 2.0)


# A function to check whether point P(x, y)
# lies inside the triangle formed by
# A(x1, y1), B(x2, y2) and C(x3, y3)
def isInside(x1, y1, x2, y2, x3, y3, x, y):

    # Calculate area of triangle ABC
    A = area (x1, y1, x2, y2, x3, y3)

    # Calculate area of triangle PBC
    A1 = area (x, y, x2, y2, x3, y3)
    
    # Calculate area of triangle PAC
    A2 = area (x1, y1, x, y, x3, y3)
    
    # Calculate area of triangle PAB
    A3 = area (x1, y1, x2, y2, x, y)
    
    # Check if sum of A1, A2 and A3
    # is same as A
    if(A == A1 + A2 + A3):
        return True
    else:
        return False

# Driver program to test above function
# Let us check whether the point P(10, 15)
# lies inside the triangle formed by
# A(0, 0), B(20, 0) and C(10, 30)
if (isInside(0, 0, 20, 0, 10, 30, 10, 15)):
    print('Inside')
else:
    print('Not Inside')

但是这样我必须将所有三角形一一制作。我正在寻找一种方法来计算覆盖点 P 的所有三角形。

作为额外信息,脚本的目标是进行三角形插值以确定点 P 的风速。

有人可以帮助我吗?我真的不知道如何使用所有可能的组合。

亲切的问候,

西蒙

一些注释: *使用蟒蛇 *使用Spyder *我有能力使用模块

Hello everyone i am new here. So far i have had a lot of information from StackOverflow so thanks everyone.

I got 6 coördinates:

A: (4,2)
B: (6,1)
C: (9,4)
D: (8,9)
E: (5,2)
F: (2,2)

And i have a measured position, lets say P=(3,7)

I want to find out all the combinations of points that can make a triangle that will cover the position P. I already have a code to calculate if a position is within a triangle:

# A utility function to calculate area
# of triangle formed by (x1, y1),
# (x2, y2) and (x3, y3)

def area(x1, y1, x2, y2, x3, y3):

    return abs((x1 * (y2 - y3) + x2 * (y3 - y1)
                + x3 * (y1 - y2)) / 2.0)


# A function to check whether point P(x, y)
# lies inside the triangle formed by
# A(x1, y1), B(x2, y2) and C(x3, y3)
def isInside(x1, y1, x2, y2, x3, y3, x, y):

    # Calculate area of triangle ABC
    A = area (x1, y1, x2, y2, x3, y3)

    # Calculate area of triangle PBC
    A1 = area (x, y, x2, y2, x3, y3)
    
    # Calculate area of triangle PAC
    A2 = area (x1, y1, x, y, x3, y3)
    
    # Calculate area of triangle PAB
    A3 = area (x1, y1, x2, y2, x, y)
    
    # Check if sum of A1, A2 and A3
    # is same as A
    if(A == A1 + A2 + A3):
        return True
    else:
        return False

# Driver program to test above function
# Let us check whether the point P(10, 15)
# lies inside the triangle formed by
# A(0, 0), B(20, 0) and C(10, 30)
if (isInside(0, 0, 20, 0, 10, 30, 10, 15)):
    print('Inside')
else:
    print('Not Inside')

But in this way I have to make all the triangles one by one. I am looking for a way to calculate all the triangles that cover point P.

As extra information, the goal of the script is to do a triangle interpolation to determine the wind speed at point P.

Could anyone help me with this? I really cant find out how to use all the possible combinations.

Kind regards,

Simon

some notes:
*using Anaconda
*using Spyder
*I am capable of using modules

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

潜移默化 2025-01-21 04:30:30

很高兴看到您已经在代码中付出了一些努力。

您可以使用 itertools [1] 中的组合来创建三角形:

import itertools
data = ['p1', 'p2', 'p3', 'p4', 'p5']

triangles = itertools.combinations(data, 3) 
print(*triangles, sep='\n')

输出:

('p1', 'p2', 'p3')
('p1', 'p2', 'p4')
('p1', 'p2', 'p5')
('p1', 'p3', 'p4')
('p1', 'p3', 'p5')
('p1', 'p4', 'p5')
('p2', 'p3', 'p4')
('p2', 'p3', 'p5')
('p2', 'p4', 'p5')
('p3', 'p4', 'p5')

Triangles 是一个生成器。您可以使用代码检查每个三角形并检查现有代码以检查点 P 是否在三角形内。

在本例中,我使用字符串作为元素,但您也可以使用元组:

import itertools
data = [(0,0), (1,1), (2,2), (2,3)]

triangles = itertools.combinations(data, 3) 
print(*triangles, sep='\n')

((0, 0), (1, 1), (2, 2))
((0, 0), (1, 1), (2, 3))
((0, 0), (2, 2), (2, 3))
((1, 1), (2, 2), (2, 3))

例如,您可以使用这部分代码作为起点:

import itertools

points = [(4,2), (6,1), (9,4), (8,9), (5,2), (2,2)]

P = (3,7)

for triangle in itertools.combinations(points, 3):
    p1, p2, p3 = triangle
    if isInside(*p1, *p2, *p3, *P):
        print(f"P {P} is in triangle: {triangle}")
    else:
        print(f"P {P} is NOT in triangle: {triangle}")

[1] https://docs.python.org/3/library/itertools.html#itertools.combinations

Nice to see that you have put some effort in your code already.

You can use combinations from itertools [1] to create triangles:

import itertools
data = ['p1', 'p2', 'p3', 'p4', 'p5']

triangles = itertools.combinations(data, 3) 
print(*triangles, sep='\n')

Output:

('p1', 'p2', 'p3')
('p1', 'p2', 'p4')
('p1', 'p2', 'p5')
('p1', 'p3', 'p4')
('p1', 'p3', 'p5')
('p1', 'p4', 'p5')
('p2', 'p3', 'p4')
('p2', 'p3', 'p5')
('p2', 'p4', 'p5')
('p3', 'p4', 'p5')

Triangles is a generator. You can use your code to go over each triangle and check your existing code to check if point P is inside a triangle.

in this case I used strings as elements, but you can also use tuples:

import itertools
data = [(0,0), (1,1), (2,2), (2,3)]

triangles = itertools.combinations(data, 3) 
print(*triangles, sep='\n')

((0, 0), (1, 1), (2, 2))
((0, 0), (1, 1), (2, 3))
((0, 0), (2, 2), (2, 3))
((1, 1), (2, 2), (2, 3))

You can for exmample use this part of code as a starting point:

import itertools

points = [(4,2), (6,1), (9,4), (8,9), (5,2), (2,2)]

P = (3,7)

for triangle in itertools.combinations(points, 3):
    p1, p2, p3 = triangle
    if isInside(*p1, *p2, *p3, *P):
        print(f"P {P} is in triangle: {triangle}")
    else:
        print(f"P {P} is NOT in triangle: {triangle}")

[1] https://docs.python.org/3/library/itertools.html#itertools.combinations

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