计算两点之间的角度(顺时针)

编程入门 行业动态 更新时间:2024-10-08 22:54:00
本文介绍了计算两点之间的角度(顺时针)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我很久没有使用数学了,这应该是一个简单的问题.

I have been not using math for a long time and this should be a simple problem to solve.

假设我有两个点A:(1,0)和B:(1,-1).

Suppose I have two points A: (1, 0) and B: (1, -1).

我想使用一个程序(Python或任何一种编程语言)来计算A,原点(0,0)和B之间的顺时针角度.它将是这样的:

I want to use a program (Python or whatever programming language) to calculate the clockwise angle between A, origin (0, 0) and B. It will be something like this:

angle_clockwise(point1, point2)

请注意,参数的顺序很重要.由于角度计算将是顺时针方向:

Note that the order of the parameters matters. Since the angle calculation will be clockwise:

  • 如果我调用angle_clockwise(A,B),它将返回45.
  • 如果我调用angle_clockwise(B,A),它将返回315.

换句话说,算法是这样的:

In other words, the algorithm is like this:

  • 在第一个参数点与(0,0)之间绘制一条线(线1).
  • 在第二个点参数与(0,0)之间绘制一条线(第2条线).
  • 将第1行绕着(0,0)顺时针旋转,直到与第2行重叠.
  • 行进的角距离线1将是返回的角度.
  • 有什么办法可以编码此问题?

    Is there any way to code this problem?

    推荐答案

    使用两个向量的内积和行列式.如果您想了解其工作原理,那么这实际上就是您应该了解的内容.您需要了解/阅读矢量数学才能理解.

    Use the inner product and the determinant of the two vectors. This is really what you should understand if you want to understand how this works. You'll need to know/read about vector math to understand.

    请参阅: en.wikipedia/wiki/Dot_product 和 en.wikipedia/wiki/Determinant

    from math import acos from math import sqrt from math import pi def length(v): return sqrt(v[0]**2+v[1]**2) def dot_product(v,w): return v[0]*w[0]+v[1]*w[1] def determinant(v,w): return v[0]*w[1]-v[1]*w[0] def inner_angle(v,w): cosx=dot_product(v,w)/(length(v)*length(w)) rad=acos(cosx) # in radians return rad*180/pi # returns degrees def angle_clockwise(A, B): inner=inner_angle(A,B) det = determinant(A,B) if det<0: #this is a property of the det. If the det < 0 then B is clockwise of A return inner else: # if the det > 0 then A is immediately clockwise of B return 360-inner

    在行列式计算中,您将两个向量连接起来以形成2 x 2矩阵,并为此计算行列式.

    In the determinant computation, you're concatenating the two vectors to form a 2 x 2 matrix, for which you're computing the determinant.

    更多推荐

    计算两点之间的角度(顺时针)

    本文发布于:2023-11-30 13:27:34,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1650147.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:两点   顺时针   角度

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!