Jump to content
• Advertisement

# Vector Division

This topic is 2538 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

So recently I came across a problem where I needed to solve for "t" from the line equation

P = P0 + t(P1 - P0)

where P, P0, and P1 are vectors like [1,2,3] and t is a real number.

So I came up with the following equation for t

t = (P - P0) / (P1 - P0)

then it dawned on me that I don't know how to divide 2 vectors and get a scalar. In MatLab, if I divde 2 vectors I get a scalar
but I have no idea what it is doing behind the scenes. Could you please enlighten me?

#### Share this post

##### Share on other sites
Advertisement
You can't divide vectors in general. If you happen to know that (P-P0) and (P1-P0) are proportional, you can compute their dot product and then divide by the square of the length of (P1-P0).
P = P0 + t * (P1 - P0) P - P0 = t * (P1 - P0) dot_product(P - P0, P1 - P0) = dot_product(t * (P1 - P0), P1 - P0) dot_product(P - P0, P1 - P0) = t * dot_product(P1 - P0, P1 - P0) t = dot_product(P - P0, P1 - P0) / dot_product(P1 - P0, P1 - P0) t = dot_product(P - P0, P1 - P0) / length_squared(P1 - P0)

#### Share this post

##### Share on other sites
Is that how you think MatLab does it?

#### Share this post

##### Share on other sites
According to the help (it seems to be in the help page for mrdivide), MatLab is solving a least squares problem, which in this case can be done the way I described. So, yes.

#### Share this post

##### Share on other sites
From your first equation P = P0 + t(P1 - P0), those are all in one line so you could just use the magnitude of the vectors perhaps (imagine they are all just on the x-axis, then they're just plain old numbers). It should work for that particular equation.

#### Share this post

##### Share on other sites

So recently I came across a problem where I needed to solve for "t" from the line equation

P = P0 + t(P1 - P0)

that is actually an equation system. You look at each component separately:
P.x = P0.x + t*(P1.x-P0.x)
P.y = P0.y + t*(P1.y-P0.y)
...

you can now solve all of these equations separately (using the formula you already derived, just applied to each component). In the general case this system doesn't have a solution since you will get different solutions for t. A solutions only exist in the case where P lies on the line defined by P0 and P1. In that case you get the same t from both equations.

Can you explain what underlying problem you are trying to solve?

Edit: Somehow I assumed the 2d case...

#### Share this post

##### Share on other sites

[quote name='akaitora' timestamp='1312766167' post='4845990']
So recently I came across a problem where I needed to solve for "t" from the line equation

P = P0 + t(P1 - P0)

that is actually an equation system. You look at each component separately:
P.x = P0.x + t*(P1.x-P0.x)
P.y = P0.y + t*(P1.y-P0.y)

you can now solve both of these equations separately (using the formula you already derived, just applied to each component). Now in the general case this system doesn't have a solution since you will get two different solutions for t. A solutions only exist in the case where P lies on the line defined by P0 and P1. In that case you get the same t from both equations.[/quote]

Let me finish that thought. When you have a system of equations with more equations than variables, you generally cannot solve it exactly, but you could still find a value of t that works well as a compromise for all the individual coordinates. You do this by minimizing the distance between P and P0+t*(P1-P0), which is a least squares problem. If you work out the problem (you can do this by orthogonal projection or by differentiating the square of the distance with respect to t), you'll arrive at the solution I posted earlier.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
25
3. 3
4. 4
JoeJ
18
5. 5
• Advertisement

• 14
• 22
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631764
• Total Posts
3002210
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!