# Help solving linear equations

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

## Recommended Posts

Hi I'm trying to implement a fast triangle-triangle intersection algorithm (Tropp06,[url="http://news.xinhuanet.com/english2010/china/2011-07/30/c_131018764.htm"]PDF[/url])

In Stage 4 of the paper I have found the line of intersection between the plane of triangle B and triangle A, this a vector t emenating from a point T.
The edge of B that I'm looking for intersection with is defined as a point in B, P plus an edge vector p.

So the equation as given in the paper is of the from
P + d p = T + g t
where d and g are scalar multiples of the vectors p and t.

P,p,T and t are all known. d and g are the unknown scalars I need to solve for.

The goal here is to save operations and avoid using divides. The paper also hints at using determinants to save calculations.
The result is a point where the line T+t and P+p cross.

I re-write the equation to look like this -d p + g t = (P-T) in an attempt to make it look more like what I find in linear algebra texts.
I look at this as a series of 3 equations of 2 variables. Something like:
[code]-d * p.x + g * t.x = (P-T).x
-d * p.y + g * t.y = (P-T).y
-d * p.z + g * t.z = (P-T).z[/code]

Now, all the examples I can find show how to solve for 3 equations and 3 variables, or 2 equations and 2 variables, not 3 equations and 2 variables.
Also, the paper says that since the vectors lie in the same plane "2 x 2 equations sets are solved".

Thanks to anyone who can help with this.

##### Share on other sites
Knowing that the vectors lie on the same plane allows you to ignore one of the equations and solve a 2x2 linear system instead. The rule of thumb is usually to take the cross product of 'p' and 't', determine the component with the largest magnitude, and throw away the equation whose coefficients are that component (i.e. if (p x t).y has the largest magnitude, throw out the middle equation in your system). The purpose of that approach is to leave you with the most stable 2x2 system that has a solution (if there is one to begin with).

##### Share on other sites
[quote name='Zipster' timestamp='1312284574' post='4843508']
Knowing that the vectors lie on the same plane allows you to ignore one of the equations and solve a 2x2 linear system instead. The rule of thumb is usually to take the cross product of 'p' and 't', determine the component with the largest magnitude, and throw away the equation whose coefficients are that component (i.e. if (p x t).y has the largest magnitude, throw out the middle equation in your system). The purpose of that approach is to leave you with the most stable 2x2 system that has a solution (if there is one to begin with).
[/quote]

Thank you this is helpful. I'm not sure what you mean by 'stable'? Perhaps I will have problems in floating point accuracy if the plane is in a certain orientation? I intend to solve using determinants according to the example here: [url="http://www.jimloy.com/algebra/determin.htm"]http://www.jimloy.com/algebra/determin.htm[/url]

It looks like if there is no solution the denominator of one of the determinants is 0.

##### Share on other sites
[quote name='PrestoChung' timestamp='1312369801' post='4844001']
[quote name='Zipster' timestamp='1312284574' post='4843508']
Knowing that the vectors lie on the same plane allows you to ignore one of the equations and solve a 2x2 linear system instead. The rule of thumb is usually to take the cross product of 'p' and 't', determine the component with the largest magnitude, and throw away the equation whose coefficients are that component (i.e. if (p x t).y has the largest magnitude, throw out the middle equation in your system). The purpose of that approach is to leave you with the most stable 2x2 system that has a solution (if there is one to begin with).
[/quote]

Thank you this is helpful. I'm not sure what you mean by 'stable'? Perhaps I will have problems in floating point accuracy if the plane is in a certain orientation? I intend to solve using determinants according to the example here: [url="http://www.jimloy.com/algebra/determin.htm"]http://www.jimloy.co...ra/determin.htm[/url]

It looks like if there is no solution the denominator of one of the determinants is 0.
[/quote]
Yes, you can't just throw out any equation at random because it's possible you'll get a denominator of 0 when you attempt to solve the remaining equations, however at the same time a denominator that's extremely [i]close[/i] to 0 might not behave very well either, depending on your available range and precision. So as long as you have to check for a solvable system anyway, might as well choose the [i]best[/i] solvable system (if there is one).

##### Share on other sites
You have a linear system

A x = b

where A is 3x2, x is 2x1, and b is 3x1. Instead you can solve,

A[sup]T[/sup] A x = A[sup]T[/sup] b .

The 2x2 matrix G = A[sup]T[/sup] A is called the [i]Grammian [/i]of the columns of A. It's a matrix of dot products, and it will be invertible so long as the columns of A are linearly independent. This will give you a least-squares solution. I.e., if there is no x such that A x = b, it'll give you an x that minimizes ||A x - b||. But of course, if there [i]is [/i]a solution, that's what you get.

This approach has the advantage of not requiring branches to choose which set of equations to solve. It has the disadvantage of involving a bunch of multiplies to compute G.

What's fastest? I don't know.

##### Share on other sites
Yes, I intentionally omitted the projection approach because it felt a little too advanced if you don't already understand linear systems However, it will work, as long as you know beforehand that your vectors are coplanar, or else |Ax - b| will be greater than 0 and the solution won't actually be an intersection.

##### Share on other sites
well you could argue that solving the least squares problem is actually more accurate; since with numerical precision 'coplanar' lines will probably not 'actually' be coplanar, so the least squares solution should be closer to the ideal solution than dropping a dimension and solving in 2D assuming they are 100% coplanar

##### Share on other sites
Thanks, guys.

I must have fudged my algebra somewhere.

[color=#1C2837][size=2]P + d p = T + g t[/size][/color]
[color=#1C2837][size=2]d p + g(-t) = T - P[/size][/color]

Does that look right?
I"m solving for scalars d and g. The x,y(or z) component of vectors p and t, and the point (T-P) are the 'constant' factors in Cramers rule.
And this part comes out correct in my calculations.
But for some reason i'm getting g = 1+|Ag|/|A|

I thought it might have been because I attached the 'negative' sign to the -t vector? But if that was the case I would think d would come out wrong because it shares the same denominator with g, namely
A=
| p.x -t.x |
| p.y -t.y |
while the matrix in the numerator of g doesn't involve -t at all. That is
Ag=
| p.x (T-P).x |
| p.y (T-P).y |

At least when I plug in 1+|Ag|/|A| for g I get the same point as when I plug in the proper d, but it does confuse me that the form doesn't match what I've seen for Cramer's rule.

Thanks to all for the help on this one