【问题标题】:Find the angle of two connected line segments based upon a random point根据随机点找到两条连接线段的角度
【发布时间】:2021-11-13 08:30:10
【问题描述】:

我有 3 对 x、y 坐标(A、B、C),它们形成了两条相连的线段。

我想根据可以落在线段任一侧的随机点(x,y 坐标)计算线段的角度。

例如,如果随机点是D,我想计算绿色角度,或者如果随机点是E,我想计算红色角度。

这是我要完成的函数签名:

function angle(segment_1, segment_2, random_point) {

}

【问题讨论】:

  • 这是作业吗?我最近看到了几乎完全相同的问题。
  • 计算 BA 和 BC 之间的角度(将在 0 到 180 度之间)。然后计算 BA 和(BE 或 BD)的向量积。这将为您提供 angle 的符号,然后如果 angle
  • @mozway 不,这不是作业
  • @PtitXav 你能用代码示例回答吗?

标签: javascript python trigonometry angle cartesian-coordinates


【解决方案1】:

在您的函数中,您可以在随机点和基础之间创建一个中间段,然后添加这两个角度

angle(BA,BE) + angle(BE,BC);

【讨论】:

    【解决方案2】:

    你需要一些基本的函数,比如标量积和叉积来实现这个;

    import math
    from collections import namedtuple
    Point = namedtuple('Point', 'x y')
    
    # define nom of a vector
    def norm(v):
        n = math.sqrt(v.x*v.x + v.y*v.y)
        return n
    
    # normalise a vector
    def normedVec(v):
        n = norm(v)
        nv = Point(v.x/n, v.y/n)
        return nv
    
    # print angle in degrees
    def printAngle(name,rad):
        deg = rad * 180.0 / math.pi
        print("{}={}".format(name,deg))
    
    # scalar (aka dot)product of 2 vectors 
    def dotProduct(v1,v2):
        d = v1.x*v2.x +  v1.y*v2.y
        print("dot={}".format(d))
        return d
    
    # vectorial (aka cross) product
    def crossProd(v1,v2):
        c = v1.x*v2.y - v1.y*v2.x
        print("c={}".format(c))
        return c
    
    # compute angle between 2 vectoris
    def angle(s1,s2):
        n1 = normedVec(s1)
        n2 = normedVec(s2)
        cosAngle = dotProduct(n1,n2)
        d = crossProd(n1,n2)
        angle = math.acos(cosAngle)
        if d <= 0:
            angle = - angle
        printAngle("angle",angle)
        return angle
    
    # compute angle between 2 vectors using point orientation 
    def angleOriented(AB,AC,AX):
        # Compute angle between 2 vectors
        ABC = angle(AB, AC)
        # Compute angle to random point
        ABX = angle(AB, AX)
        # if angle is negative then change angle sign 
        if ABX < 0:
            ABC = - ABC
        # put angle between 0 and 360
        while ABC < 0:
            ABC += 2*math.pi
        printAngle("Oriented",ABC)
    
    # test
    AB = Point(1,0)
    AC = Point(-1,1)
    AD = Point(0,-1)
    AE = Point(0,1)
    
    ABE = angleOriented(AB,AC,AE)
    print("-----")
    ABD = angleOriented(AB,AC,AD)
    

    【讨论】:

      猜你喜欢
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多