简体   繁体   中英

find intersection point of two vectors independent from direction

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 . eg:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM