ccc*_*ccc 3 euclidean-distance python-3.x
我有两个点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)
Run Code Online (Sandbox Code Playgroud)
理论上,我需要使XZ和YZ的距离相等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)))
Run Code Online (Sandbox Code Playgroud)
但这会返回以下错误:
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
Run Code Online (Sandbox Code Playgroud)
为了获得第三个顶点,您只需围绕点 旋转点 度数(x2, y2)即可。另一个可接受的解决方案将通过旋转一定度数(即沿相反方向)来获得。60(x1, y1)-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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2888 次 |
| 最近记录: |