gpt4 book ai didi

java - 垂直于一点的 vector

转载 作者:行者123 更新时间:2023-12-01 08:51:56 25 4
gpt4 key购买 nike

我需要计算两点之间垂直的 vector 。

enter image description here我可以得到这样的原始 vector :

    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;

}

但我不知道如何获得垂直 vector 。

最佳答案

翻转 x 和 y,将其中一个设为负数,然后除以长度进行标准化:

double x1 = y;
double y1 = -x;
double length1 = Math.sqrt(x1*x1 + y1*y1);
x1 /= length1;
y1 /= length1;

并获取相反方向的垂直点:

double x2 = -y;
double y2 = x;
double length2 = Math.sqrt(x2*x2 + y2*y2);
x2 /= length2;
y2 /= length2;

您还应该检查长度是否不为零。

关于java - 垂直于一点的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42333152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com