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):
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.
P1 + a * (P2 - P1)