找到与方向无关的两个向量的交点 [英] find intersection point of two vectors independent from direction

查看:29
本文介绍了找到与方向无关的两个向量的交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

所以我想知道粉红色 X 的坐标.但我只能找到计算两条线的交点的公式,我没有:( 所以我正在寻找一些帮助正确处理这个问题.

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

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

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

所有这些都是二维的

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

解决方案

如果您有一条由点 P 和归一化方向 R 定义的无限线和第二条无限线,由点Q和方向S定义,则无限线X的交点为:

alpha ... Q-P 和 R 之间的角度beta ... R 和 S 之间的角度伽玛 = 180° - α - βh = |Q-P |*罪(阿尔法)u = h/sin(beta)t = |Q-P |* sin(gamma)/sin(beta)t = 点(Q-P,(S.y,-S.x))/点(R,(S.y,-S.x))=行列式(mat2(Q-P,S))/行列式(mat2(R,S))u = 点(Q-P,(R.y,-R.x))/点(R,(S.y,-S.x))=行列式(mat2(Q-P,R))/行列式(mat2(R,S))X = P + R * t = Q + S * u

这可以通过使用

void setup() {大小(500,500);}无效画(){背景(0、0、0);行程(255);填充(255, 0, 0);PVector l1p1 = 新 PVector(250, 150);PVector l1p2 = 新 PVector(300, 300);PVector l2p1 = 新 PVector(200, 180);PVector l2p2 = 新 PVector(300, 220);PVector l3p1 = 新的 PVector(200, 300);PVector l3p2 = 新的 PVector(250, 280);线(l1p1.x,l1p1.y,l1p2.x,l1p2.y);线(l2p1.x,l2p1.y,l2p2.x,l2p2.y);线(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);圆(x1.x,x1.y,10);PVector x2 = Intersect(l1p1, dir1, l3p1, dir3);圆(x2.x,x2.y,10);PVector x3 = Intersect(l2p1, dir2, l3p1, dir3);圆(x3.x,x3.y,10);}

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

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

I have two vectors and i want to know where these vectors will intersect independent from direction or length. So lets just say i would draw an infinite line in either direction and i want to know where those two lines will intersect and get the coordinates. See image below for clarification:

So i want to know the coordinates of the pink X. But i can only find formulas for calculating the intersection point of two lines with an stard and end point which i dont have :( So i am looking for some help on how to approach this properly.

I have calculated the normalized direction of the blue lines: like so:

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

Some context on why i want to do this: I am trying to find the center point of a circle created from 3 points.

All this is in 2D

If extra information is needed i am happy to provide.

解决方案

If you've a endless line which is defined by a point P and a normalized direction R and a second endless line, which is defined by a point Q and a direction S, then the intersection point of the endless lines X is:

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

This can be calculated by the use of PVector, as follows:

// 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;
}

See the example:

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);
}

Note, if the lines are parallel then the scalars of the returned point (PVector object) are infinit. This can be evaluated by Float.isInfinite. e.g:

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

这篇关于找到与方向无关的两个向量的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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