Python function to find a point of an equilateral triangle

Question:

I am trying to write a function equilateral(x, y): that takes two np.ndarrays of shape (N,) , where x and y are natural numbers and returns a point z an np.ndarray of shape (N,) such that (x, y, z) are are the vertices of an equilateral triangle.

Any one please suggest.

Asked By: Richard

||

Answers:

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.

So just rotate y around x by 60/-60 degrees and you have your 3rd co-ordinate.

Answered By: inkarar

You can achieve this using the following program.
You may also expand the code to test if the distance between the points is equal.

    import numpy as np
    def equilateral(x, y):
                    
        v_x = (x[0]+y[0]+np.sqrt(3)*(x[1]-y[1]))/2 # This computes the `x coordinate` of the third vertex.
        v_y = (x[1]+y[1]+np.sqrt(3)*(x[0]-y[0]))/2 #This computes the 'y coordinate' of the third vertex. 
        z = np.array([v_x, v_y]) #This is point z, the third vertex. 
        return z
Answered By: kibromhft

Actually there will be two points that can make an equilateral triangle with the given two points

import math

# define the coordinates of the first two points
x1, y1 = 0, 0
x2, y2 = 3, 4

# calculate the distance between the two points
distance = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)

# calculate the angle between the two points
angle = math.atan2(y2 - y1, x2 - x1)

# calculate the coordinates of the third point (the first one)
x3_1 = x1 + distance * math.cos(angle - (1 * math.pi / 3))
y3_1 = y1 + distance * math.sin(angle - (1 * math.pi / 3))

# calculate the coordinates of the third point (the second one)
x3_2 = x1 + distance * math.cos(angle + (1 * math.pi / 3))
y3_2 = y1 + distance * math.sin(angle + (1 * math.pi / 3))
Answered By: tusonggao
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.