Home > Software engineering >  Python function to find a point of an equilateral triangle
Python function to find a point of an equilateral triangle

Time:10-23

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.

CodePudding user response:

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.

CodePudding user response:

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
  • Related