Math Algorithms – Transforming Math Formulas into Code for Line-Line Intersection

algorithmsmath

I'm having hard times transforming a math formula to code. I can solve it on the paper easily, but it's hard for me to bring it into code form. Is it generally possible to bring a math formula directly into a program, or, if not, how can it be transferred?

Taking, for example, the Pythagorean Theorem: a² + b² = c². Having a and b defined in the code, it is straight forward to apply this formula.

c = sqrt(pow(a, 2) + pow(b, 2))

Currently, I'm having hard times bringing the line-line intersection, that I've solved on paper, into my program. Assuming a line being defined as L: O + a * D, one can find the intersection of two lines by setting them as equal.

L1: O1 + a * D1
L2: O2 + b * D2

=> O1 + a * D1 = O2 + b * D2

Using pen an paper, I would have already defined values and can solve the equation by splitting the equation into three sub-equations and applying simple substituion (I hope you can read my hand writing):

enter image description here

My actual problem

… is that substitution can not be applied algorithmically, or I don't know how to do. The equation L1 = L2 must be transformed to a generalized formula that can be directly translated into code (like a² + b² = c²), but I don't know how.

I was hoping for a step-by-step explanation (either as answer or linked) on how to come to the formula/algorithm I am searching for. I know there are quite many websites that describe the intersection of two lines, but I would like to understand how to find the algorithm from a mathematical starting point.

Best Answer

As you already suggest in your question, there is a big difference between implementing a formula, and implementing a calculation routine. Your paper solution is a routine, not a formula.

  • You needed paper to write down numbers as intermediate results. Your algorithm will need something similar. Algorithms for solving systems of equations typically manipulate a matrix (2D array) of numbers. Put your equation factors into an 2D array, and try to describe the substitions as an operation on that matrix.

  • When solving this on paper, you take decisions along the way. Eg. You choose to combine the first and third equation and solve that towards R. Then you used the second equation to solve p. This will not always work if there are some 0 factors in your equation. You might need to solve towards another variable, or combine the equations in a different order. The algorithm needs to take into account all these scenarios.

  • Your problem is 'overdetermined': In general, 2 lines in 3D will not intersect. It will require a 'lucky shot' (special case) for them to intersect. Your paper example happens to have a solution. And it happens to be round numbers: easy to check. What if your first solution for P would be 1.0 and your second solution would be 0.9999999? Would that be a rounding error of the algorithm, or would the problem have no solution?

Related Topic