gpt4 book ai didi

python-3.x - 为平面中给定的两个点创建等边三角形 - Python

转载 作者:行者123 更新时间:2023-12-05 01:00:23 25 4
gpt4 key购买 nike

我在笛卡尔平面上有两个点 X = (x1,y1)Y=(x2,y2)。我需要找到第三个点 Z = (x,y) 使得这三个点构成一个等边三角形。

我正在使用以下代码示例计算两点之间的欧几里得距离:

def distance(points, i, j):
dx = points[i][0] - points[j][0]
dy = points[i][1] - points[j][1]
return math.sqrt(dx*dx + dy*dy)

理论上,我需要将 XZYZ 的距离等同于 XY。这给了我们两个可能的答案,我需要它们两者也是。但是我很难在我的代码中启动 Z 点。有人可以帮我吗?以下是我尝试过的示例。

L = [0, 6]  #known two points
d= distance(points, L[0], L[1])
x = Symbol('x')
y = Symbol('y')
newpoint = x,y #coordintes of the third point of the triangle
f1 = distance(points, L[0], newpoint)
f2 = distance(points, L[1], newpoint)
print(nsolve((f1, f2), (x, y), (d,d)))

但这会返回以下错误:

 File "/Users/*.py", line 99, in <module>
f1 = distance(points, L[0], newpoint)

File "/Users/*.py", line 36, in distance
dx = points[i][0] - points[j][0]

TypeError: list indices must be integers or slices, not tuple

最佳答案

为了获得第三个顶点,您可以将点 (x2, y2) 围绕点 (x1, y1)< 旋转 60。另一个可接受的解决方案是通过 -60 度的旋转获得,即在相反的方向上。

import math


def get_point(x1, y1, x2, y2):
#express coordinates of the point (x2, y2) with respect to point (x1, y1)
dx = x2 - x1
dy = y2 - y1

alpha = 60./180*math.pi
#rotate the displacement vector and add the result back to the original point
xp = x1 + math.cos( alpha)*dx + math.sin(alpha)*dy
yp = y1 + math.sin(-alpha)*dx + math.cos(alpha)*dy

return (xp, yp)


print(get_point(1, 1, 2, 1))
# (1.5, 0.1339745962155614)

关于python-3.x - 为平面中给定的两个点创建等边三角形 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50547068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com