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

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

问题描述

我有两个向量,我想知道这些向量在哪里相交而与方向或长度无关.因此,可以说我会在任一方向上画一条无限线,而我想知道这两条线将在何处相交并获得坐标.参见下面的图片进行澄清:

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

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

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

关于为什么要执行此操作的一些上下文:我试图找到由3个点创建的圆的中心点.

所有这些都是二维的

如果需要其他信息,我很乐意提供.

解决方案

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

  alpha ... Q-P和R之间的角度beta ... R与S之间的夹角伽马= 180°-alpha-betah = |Q-P |* sin(alpha)u = h/sin(β)t = |Q-P |* sin(γ)/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);}无效draw(){背景(0,0,0);笔画(255);fill(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 =相交(l1p1,dir1,l2p1,dir2);circle(x1.x,x1.y,10);PVector x2 =相交(l1p1,dir1,l3p1,dir3);circle(x2.x,x2.y,10);PVector x3 =相交(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); 

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天全站免登陆