How to Get The Intersection Point of Two Vector

find intersection point of two vectors independent from direction

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:

Sample Image

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:

Sample Image

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

How to get the intersection point of two vector?

Using R's spatial facilities:

library(sp)     ## Provides basic spatial classes/methods, incl. SpatialLines
library(rgeos) ## Supports topological operations, including intersection

## Read in data and wrap them up as SpatialLines objects
a = c(1,5,2,6,3,6,3,5,7)
b = c(5,3,5,7,2,6,9,3,6)
SL1 <- SpatialLines(list(Lines(Line(cbind(seq_along(a),a)), "A")))
SL2 <- SpatialLines(list(Lines(Line(cbind(seq_along(b),b)), "B")))

## Find intersections
coords <- coordinates(gIntersection(SL1, SL2))

## Check that it worked
plot(a,type = "l")
lines(b)
points(coords, col="red", pch=16)

Sample Image

How to find the points along two vectors where the distance is equal to X

You can use the law of cosines.

Here is an illustration of applying this to your problem.

What you should then do is note that the ratio of the magnitude of vector A to vector B allows you to convert from a to b in the picture. From there, C is known, and A = 180 degrees - C - B.

You have six variables, a b c A B C, two constraints (A = 180 - C - B and a = ratio*b), and a constant in the form of C.

You can now select values for two of the three of either a or b, A or B, or c. Use the law of sines to equate this variable to its respective partner. Doing so will leave you with one unknown, which you can use the respective formula on the wolfram page to solve for (or just derive it yourself from one of the formulas on the page).

Edit: Also note that you will need to convert / map the angles you find from these formulas to your original orientation / coordinate system.

Find point of intersection between two vectors in MATLAB

One solution is to use the equations derived in this tutorial for finding the intersection point of two lines in 2-D (update: this is an internet archive link since the site no longer exists). You can first create two matrices: one to hold the x coordinates of the line endpoints and one to hold the y coordinates.

x = [0 0; 6 6];  %# Starting points in first row, ending points in second row
y = [0 6; 6 0];

The equations from the above source can then be coded up as follows:

dx = diff(x);  %# Take the differences down each column
dy = diff(y);
den = dx(1)*dy(2)-dy(1)*dx(2); %# Precompute the denominator
ua = (dx(2)*(y(1)-y(3))-dy(2)*(x(1)-x(3)))/den;
ub = (dx(1)*(y(1)-y(3))-dy(1)*(x(1)-x(3)))/den;

And you can now compute the intersection point of the two lines:

xi = x(1)+ua*dx(1);
yi = y(1)+ua*dy(1);

For the example in the question, the above code gives xi = 3 and yi = 3, as expected. If you want to check that the intersection point lies between the endpoints of the lines (i.e. they are finite line segments), you just have to check that the values ua and ub both lie between 0 and 1:

isInSegment = all(([ua ub] >= 0) & ([ua ub] <= 1));

A couple more points from the tutorial I linked to above:

  • If the denominator den is 0 then the two lines are parallel.
  • If the denominator and numerator for the equations for ua and ub are 0 then the two lines are coincident.

Point of intersection between two vectors when the direction of one vector is not known

Following on from the comments I'm going to take a leap here and answer your ultimate question directly.

Say the player is, at the initial time, at a point p and travelling with velocity v; your gun is at position q and shoots a bullet in any direction at speed s:

Sample Image

The length of OP is vΔt and that of Q sΔt. The angle a is given by the dot product:

Sample Image

We can then use the cosine rule to solve for Δt:

Sample Image

Written in this form, we can easily see that it is a quadratic equation, and thus directly solve for Δt using the Quadratic formula:

Sample Image

There are a few cases we need to consider here:

  • v < s: need to take the positive root only as otherwise we would get negative time.
  • v > s and dot(PQ, s) < 0: the bullet will never catch the player.
  • v > s and dot(PQ, s) > 0: take the negative root this time, as the positive root is for a backwards travelling player (longer time; this is also the case presented in the diagram above).

Having the correct value for Δt from above will then enable us to find the intersection point o, and therefore the intended direction d:

Sample Image

Note that d is not normalized. Also, this solution works for 3D too, unlike an approach with angles.

R: finding intersection between two vectors

This a floating point error in r. See the Floating Point Guide for more information.

This can be seen as the error because this returns what you're looking for:

v1 = c(2, 2.01, 2.02, 2.03, 2.04, 2.05, 2.06, 2.07, 2.08, 2.09, 2.1, 
2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18, 2.19, 2.2, 2.21,
2.22, 2.23, 2.24, 2.25, 2.26, 2.27, 2.28, 2.29, 2.3, 2.31, 2.32,
2.33, 2.34, 2.35, 2.36, 2.37, 2.38, 2.39, 2.4, 2.41, 2.42, 2.43,
2.44, 2.45, 2.46, 2.47, 2.48, 2.49, 2.5, 2.51, 2.52, 2.53, 2.54,
2.55, 2.56, 2.57, 2.58, 2.59, 2.6, 2.61, 2.62, 2.63, 2.64, 2.65,
2.66, 2.67, 2.68, 2.69, 2.7, 2.71, 2.72, 2.73, 2.74, 2.75, 2.76,
2.77, 2.78, 2.79, 2.8, 2.81, 2.82, 2.83, 2.84, 2.85, 2.86, 2.87,
2.88, 2.89, 2.9, 2.91, 2.92, 2.93, 2.94, 2.95, 2.96, 2.97, 2.98,
2.99)

v2 <- seq(2, 2.99, 0.01)

v1 <- round(v1,2) #rounds to 2 decimal places
v2 <- round(v2,2)

intersect(v1,v2) #returns v1


Related Topics



Leave a reply



Submit