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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

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

Post 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?
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post 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.
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

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

Post 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?
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

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

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

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

Post 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.
Post Reply