如何知道点是在直线的右侧还是左侧 [英] How to know if point is on the right side or on the left side of line

查看:56
本文介绍了如何知道点是在直线的右侧还是左侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我说的是 3D我在平面上有 2 个点(A 和 B),我从它们制作了一个向量 AB(B-A).我用点 A 和向量 AB 做了一条线.在同一平面上,我还有一点.我想知道该点是在我所做的线的右侧还是左侧.感谢您的帮助..

I talking about 3D I have 2 points on a plane(A and B) and I made a Vector AB (B-A) from them. I have made a line with point A and Vector AB. On the same plane I have another point. I want to know if the point is on the right side or on the left side of the line I have made. Thanks for help..

推荐答案

你已经得到了两个点 A、B 和第三个点 C.

You have got your two points A, B, and a third point C.

您可能已经知道可以使用点积求角的余弦:

You may already know that you can find the cosinus of an angle, using the dot product:

cos(AB, AC) = AB . AC / (||AB|| ||AC||)

其中 . 表示点积,|||| 欧几里得范数.

Where . denotes the dot product and || || the Euclidean norm.

然而,你要的不是角的余弦,而是角的正弦.如果 CAB 的左侧,则正弦 sin(AB, AC) 为正,如果 C 为负code> 位于 AB 的右侧.

However, what you want is not the cosinus of the angle, but the sinus of the angle. The sinus sin(AB, AC) will be positive if C is on the left of AB, and negative if C is on the right of AB.

让我们称D通过旋转中心A和角度+Pi/取B的图像得到的点2(逆时针旋转四分之一圈).

Let's call D the point obtained by taking the image of B by the rotation of centre A and angle +Pi/2 (rotation by a quarter-turn counterclockwise).

事实证明:

sin(AB, AC) = cos(AD, AC)

此外,向量AD的坐标很容易计算:如果向量AB有坐标(x,y),则向量AD 有坐标 (-y, x).

Furthermore, the coordinates of vector AD are easy to compute: if vector AB has coordinates (x,y), then vector AD has coordinates (-y, x).

将所有这些放在一起,我们得到以下公式:

Putting all this together, we get the following formula:

sin(AB,AC) = ((yA - yB)*(xC-xA) + (xB-xA)*(yC-yA)) / sqrt(((xB-xA)**2+(yB-yA)**2)*((xC-xA)**2 + (yC-yA)**2))

这是一个介于 -1 和 +1 之间的数字.如果你只关心一个左"或正确"回答,那么你只想要sin(AB,AC)的符号;并且这个表达式的分母总是正的.因此你只需要计算分子:

This is a number between -1 and +1. If all you care about is a "left" or "right" answer, then you only want the sign of sin(AB,AC); and the denominator of this expression is always positive. Hence you only need to compute the numerator:

n = ((yA - yB)*(xC-xA) + (xB-xA)*(yC-yA))
if n > 0:
  print("C is on the LEFT of AB")
else if n < 0:
  print("C is on the RIGHT of AB")
else if n == 0:
  print("C is on AB")

这篇关于如何知道点是在直线的右侧还是左侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆