I have two points X = (x1,y1) and Y=(x2,y2) in the Cartesian plane. I need to find the third point Z = (x,y) such that these three points make an equilateral triangle.
I'm calculating the Euclidean distance between two points using the following code sample:
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)
In theory, I need to equate the distances of XZ and YZ to XY.This gives us two possible answers and I need them both too. But I'm having a difficulty in initiating the point Z in my code. Can someone please help me with this?
The following is a sample of what I tried.
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)))
But this returns the following error:
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
In order to get the third vertex, you could just rotate the point (x2, y2) by 60 degrees around point (x1, y1). The other admissible solution would be obtained with a rotation by -60 degrees, i.e., in the opposite direction.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With