Vector Perpendicular to a Point
我需要计算两点之间的垂直向量。
我可以这样获得原始向量:
1 2 3 4 5 6 7 8 9 | dir.x = point2.x - point1.x; dir.y = point2.y - point1.y; float hyp = (float) Math.sqrt(dir.x*dir.x + dir.y*dir.y); if(hyp != 0){ dir.x /= hyp; dir.y /= hyp; } |
但是我不确定如何获得垂直向量。
翻转x和y,使其中一个为负,然后除以长度以进行归一化:
1 2 3 4 5 | double x1 = y; double y1 = -x; double length1 = Math.sqrt(x1*x1 + y1*y1); x1 /= length1; y1 /= length1; |
并获得相反方向的垂直点:
1 2 3 4 5 | double x2 = -y; double y2 = x; double length2 = Math.sqrt(x2*x2 + y2*y2); x2 /= length2; y2 /= length2; |
还应该检查长度是否为非零。
在二维中工作时,只需切换另一个矢量的x和y坐标并取一个,即可获得垂直矢量。这可以通过将2D旋转90度并注意到正弦为1且余弦为0来证明,因此值只是切换位置。