Python函数查找等边三角形的点

2024-10-03 23:17:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图写一个函数equilateral(x, y):,它取两个np.ndarrays of shape (N,) ,其中x和y是自然数,返回一个点zan np.ndarray of shape (N,),这样(xyz)就是等边三角形的顶点

任何一个请推荐


Tags: of函数npndarraysndarrayshape顶点三角形
2条回答

为了获得第三个顶点,可以围绕点(x1,y1,…)旋转点(x2,y2,…)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

相关问题 更多 >