Page 1 of 1

Help! Finding the intersection of a vector and a line.

Posted: Thu Mar 13, 2014 11:12 am
by RyanPridgeon
I'm trying to write a function that determines where a direction vector intersects with a line.

I drew this quick sketch;

Image

Obviously if I was doing this normally I'd equate them and solve using a simulatenous equation

like

A + tB = sC

Ax + t(Bx) = s(Cx)
Ay + t(By) = s(Cy)

And solve these simultaneous equations for s and sub back into sC

Is this the best way? Is there a quicker way via some vector manipulation?

Re: Help! Finding the intersection of a vector and a line.

Posted: Thu Mar 13, 2014 11:37 am
by Falco Girgis
There is definitely no way from vector manipulation, because the equation of a line encodes more information than a simple vector does.

A line gives you a slope and a y intercept.
A vector gives you just a slope.

The best you can do is consider a vector a line with a y intercept of 0, and solve for the intersection of two lines.

Re: Help! Finding the intersection of a vector and a line.

Posted: Thu Mar 13, 2014 11:46 am
by RyanPridgeon
Thanks, I actually just realised a simple way to do it using trig (because I realised I'm only ever going to be using perfectly horizontal or perfectly vertical lines)

Image

Does anyone know if this would be faster or slower than solving the intersection of 2 lines?

Re: Help! Finding the intersection of a vector and a line.

Posted: Thu Mar 13, 2014 4:50 pm
by dandymcgee
Slower due to the use of the tan() function. The simple algebra involved in solving for the intersection of two lines will be faster, but the better question is do you care? If not, implement whatever is most readable.

Always optimize bottlenecks first.

Re: Help! Finding the intersection of a vector and a line.

Posted: Thu Mar 13, 2014 7:50 pm
by qpHalcy0n
Depending on the domain, either the matrix determinant method (optimized) or conversion to homogenous space would be optimal. If in 2D you can move to a 3D homogenous space where W=1. You can take the cross product of the 2 lines with each other. Divide the result through by W. Ensure W != 0.

It's impossible to intersect a vector with a line without context. The vector must have a point accompanying it otherwise there are infinite intersection points.