簡體   English   中英

Python函數來找到等邊三角形的一個點

[英]Python function to find a point of an equilateral triangle

我正在嘗試編寫一個函數np.ndarrays of shape (N,) equilateral(x, y):它需要兩個np.ndarrays of shape (N,) ,其中 x 和 y 是自然數並返回一個點 z an np.ndarray of shape (N,)例如( x , y , z ) 是等邊三角形的頂點。

任何一位請建議。

為了獲得第三個頂點,您只需將點 (x2, y2,...) 圍繞點 (x1, y1,...) 旋轉 60 度。 另一種可接受的解決方案將通過旋轉 -60 度(即,在相反方向上)獲得。

因此,只需將 y 圍繞 x 旋轉 60/-60 度,您就擁有了第三個坐標。

您可以使用以下程序實現此目的。 您還可以擴展代碼以測試點之間的距離是否相等。

    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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM