javascript 确定一个二维向量是在另一个向量的右边还是左边
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13221873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determining if one 2D vector is to the right or left of another
提问by Eric
Given two 2D vectors, how can you tell whether the second is to the right (clockwise) of the first, or to the left (counter-clockwise)?
给定两个二维向量,如何判断第二个是在第一个的右侧(顺时针)还是左侧(逆时针)?
For instance, in these diagram B is to the right (counter-clockwise) of A
例如,在这些图中,B 位于 A 的右侧(逆时针方向)
A B . .----> A
^ ? |\ |
| / | \ |
|/ V \ V
. B A B
回答by Eric
You can achieve this using a dot product. dot(a, b) == a.x*b.x + a.y*b.y
can be used to find whether vectors are perpendicular:
您可以使用点积来实现这一点。dot(a, b) == a.x*b.x + a.y*b.y
可用于确定向量是否垂直:
var dot = a.x*b.x + a.y*b.y
if(dot > 0)
console.log("<90 degrees")
else if(dot < 0)
console.log(">90 degrees")
else
console.log("90 degrees")
Put another way. dot > 0
tells you if a
is "in front of" b
.
换一种方式。dot > 0
告诉你是否a
“在前面” b
。
Assume b
is on the right of a
. Rotating b
90 degrees counterclockwise puts it in front of a
.
Now assume b
is on the left of a
. Rotating b
90 degrees counterclockwise puts it behind a
.
假设b
在 的右侧a
。b
逆时针旋转90 度会将其置于a
.
现在假设b
在 的左侧a
。b
逆时针旋转90 度会将其放在后面a
。
Therefore, the sign of dot(a, rot90CCW(b))
tells you whether b is on the right or left of a, where rot90CCW(b) == {x: -b.y, y: b.x}
.
因此,符号dot(a, rot90CCW(b))
告诉您 b 是在 a 的右侧还是左侧,其中rot90CCW(b) == {x: -b.y, y: b.x}
。
Simplyifying:
简单化:
var dot = a.x*-b.y + a.y*b.x;
if(dot > 0)
console.log("b on the right of a")
else if(dot < 0)
console.log("b on the left of a")
else
console.log("b parallel/antiparallel to a")
回答by eh9
In the clarification in a comment from @Eric, "if A points forward, which side of it is B on?"
在@Eric 评论中的澄清中,“如果 A 指向前方,那么 B 在哪一边?”
In this formulation the answer is dead-simple. "A" points forward, as in the example, when its x-coordinate is zero. With this assumption, "B" is on the right when its x-coordinate is positive, is on the left when negative, and is neither when zero.
在这个公式中,答案非常简单。“A”指向前方,如示例中,当其 x 坐标为零时。在这个假设下,“B”在其 x 坐标为正时在右侧,在负时在左侧,在零时都不是。
Extending this clarification to "A" in general position means introducing a new coordinate system, as follows: "In a coordinate system where A points forward, ...". The simplest new coordinate system is the one where the basis vectors are A
and (1,0)
. (If A is a multiple of (1,0)
, then it's just a 90 degree rotation of the basic situation.) The coordinate transform is L : P = (P_x, P_y) --> P' = (P'_x, P'_y) = (A_y * P_x - A_x * P_y, P_y)
. This kind of linear transformation is called a skew transformation. The test is the sign of the coordinate P'_x
. Check that L takes A to the vector (0,1) in the new coordinate system. This method uses the same arithmetic as the other answer.
将此澄清扩展到一般位置的“A”意味着引入一个新的坐标系,如下所示:“在 A 指向前方的坐标系中,......”。最简单的新坐标系是基向量为A
和 的坐标系(1,0)
。(如果 A 是 的倍数(1,0)
,那么它只是基本情况的 90 度旋转。)坐标变换是L : P = (P_x, P_y) --> P' = (P'_x, P'_y) = (A_y * P_x - A_x * P_y, P_y)
。这种线性变换称为偏斜变换。测试是坐标的符号P'_x
。检查 L 是否将 A 带到新坐标系中的向量 (0,1)。此方法使用与其他答案相同的算术。
I wrote this up so that the deeper geometric content may be illuminating.
我写了这个,以便更深层次的几何内容可能具有启发性。
回答by Gab
@Eric there is a fundamental problem however with your dot product when the vector sizes vary greatly.
@Eric 但是当向量大小变化很大时,您的点积存在一个基本问题。
var dot = a.x*-b.y + a.y*b.x;
var dot = ax*-by + ay*bx;
If a(2,-2) and b(-500,-500) clearly B is on the left of a, but doing the dot product it comes to greater than 0.
如果 a(2,-2) 和 b(-500,-500) 显然 B 在 a 的左边,但是做点积它大于 0。