gpt4 book ai didi

java - 找到与方向无关的两个 vector 的交点

转载 作者:行者123 更新时间:2023-11-30 05:26:15 25 4
gpt4 key购买 nike

我有两个 vector ,我想知道这些 vector 将在哪里相交,与方向或长度无关。所以我们可以说我会在任一方向上画一条无限的线,我想知道这两条线将在哪里相交并获得坐标。请参阅下图以进行说明:

enter image description here

所以我想知道粉红色 X 的坐标。但是我只能找到计算两条线与起点和终点的交点的公式,而我没有:(所以我正在寻找一些关于如何计算的帮助正确地解决这个问题。

我已经计算了蓝线的标准化方向:像这样:

PVector norm12 = new PVector(-dir12.y, dir12.x);
PVector norm23 = new PVector(dir23.y, -dir23.x);

关于我为什么要这样做的一些背景:我试图找到由 3 点创建的圆的中心点。

所有这些都是二维的

如果需要额外信息,我很乐意提供。

最佳答案

如果你有一条由点 P 定义的无尽线和归一化方向 R和第二条无限线,由点 Q 定义和一个方向S ,那么无尽线的交点X是:

alpha ... angle between Q-P and R
beta ... angle between R and S

gamma = 180° - alpha - beta

h = | Q - P | * sin(alpha)
u = h / sin(beta)

t = | Q - P | * sin(gamma) / sin(beta)

t = dot(Q-P, (S.y, -S.x)) / dot(R, (S.y, -S.x)) = determinant(mat2(Q-P, S)) / determinant(mat2(R, S))
u = dot(Q-P, (R.y, -R.x)) / dot(R, (S.y, -S.x)) = determinant(mat2(Q-P, R)) / determinant(mat2(R, S))

X = P + R * t = Q + S * u

这可以通过使用 PVector 来计算,如下:

// Intersect 2 endless lines
// line 1: "P" is on endless line, the direction is "dir1" ("R")
// line 2: "Q" is on endless line, the direction is "dir2" ("S")
PVector Intersect( PVector P, PVector dir1, PVector Q, PVector dir2) {

PVector R = dir1.copy();
PVector S = dir2.copy();
R.normalize();
S.normalize();

PVector QP = PVector.sub(Q, P);
PVector SNV = new PVector(S.y, -S.x);

float t = QP.dot(SNV) / R.dot(SNV);

PVector X = PVector.add(P, PVector.mult(R, t));
return X;
}

参见示例:

void setup() {
size(500,500);
}

void draw() {

background(0, 0, 0);

stroke(255);
fill(255, 0, 0);

PVector l1p1 = new PVector(250, 150);
PVector l1p2 = new PVector(300, 300);
PVector l2p1 = new PVector(200, 180);
PVector l2p2 = new PVector(300, 220);
PVector l3p1 = new PVector(200, 300);
PVector l3p2 = new PVector(250, 280);

line(l1p1.x, l1p1.y, l1p2.x, l1p2.y);
line(l2p1.x, l2p1.y, l2p2.x, l2p2.y);
line(l3p1.x, l3p1.y, l3p2.x, l3p2.y);

PVector dir1 = PVector.sub(l1p2, l1p1);
PVector dir2 = PVector.sub(l2p2, l2p1);
PVector dir3 = PVector.sub(l3p2, l3p1);

PVector x1 = Intersect(l1p1, dir1, l2p1, dir2);
circle(x1.x, x1.y, 10);
PVector x2 = Intersect(l1p1, dir1, l3p1, dir3);
circle(x2.x, x2.y, 10);
PVector x3 = Intersect(l2p1, dir2, l3p1, dir3);
circle(x3.x, x3.y, 10);
}

请注意,如果直线平行,则返回点( PVector 对象)的标量为无穷大。这可以通过 Float.isInfinite 来评估。例如:

if (!Float.isInfinite(x1.x) || !Float.isInfinite(x1.y))
circle(x1.x, x1.y, 10);

关于java - 找到与方向无关的两个 vector 的交点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58541430/

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