I'm trying to write a function that determines where a direction vector intersects with a line.
I drew this quick sketch;
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?
Help! Finding the intersection of a vector and a line.
Moderator: Coders of Rage
- RyanPridgeon
- 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:
- Falco Girgis
- 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.
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.
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.
- RyanPridgeon
- 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.
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)
Does anyone know if this would be faster or slower than solving the intersection of 2 lines?
Does anyone know if this would be faster or slower than solving the intersection of 2 lines?
- dandymcgee
- 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.
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.
Always optimize bottlenecks first.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
-
- 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.
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.
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.