Link to high-resolution version

# IronMortality's Blog

## Sunday, 18 February 2018

## Friday, 16 February 2018

### Shortest distance between a point and a line

Problem

There is a point P with co-ordinates [xp,yp] and a line with known parameters. Find the distance between P and the nearest point on the line, or equivalently, the perpendicular distance between P and the line.

Approach

Find the co-ordinates of this point O and then use Pythagoras' Theorem to determine distance.

--------------------------------------

Point of intersection: O = [xo,yo]

Point outside line: P = [xp,yp]

y-axis intercept: Q = [xq,yq] = [0,C]

P_ = OP, Q_ = OQ

Perpendicular vectors <=> P_.Q_ = 0

Line equation (slope-intercept form): y = M*x + C

P_ = (xp - xo)*ex_ + (yp - yo)*ey_

Q_ = (xq - xo)*ex_ + (yq - yo)*ey_

= -xo*ex_ + (C - yo)*ey_

P_.Q_ = -xo*(xp - xo) + (C - yo)*(yp - yo) = 0

(C - yo)*(yp - yo) = xo*(xp - xo)

C - yo = xo*(xp - xo)/(yp - yo)

C = xo*(xp - xo)/(yp - yo) + yo

yo = M*xo + C

M*xo = yo - C

M = -(C - yo)/xo

= -(xp - xo)/(yp - yo)

= (xo - xp)/(yp - yo)

xo - xp = M*(yp - yo)

= M*yp - M*(M*xo + C)

= M*yp - M^2*xo - M*C

xo + M^2*xo = M*(yp - C) + xp

xo*(1 + M^2) =

xo = (xp + M*(yp - C))/(1 + M^2)

yo = M*(xp + M*(yp - C))/(1 + M^2)) + C

= (M*(xp + M*(yp - C)) + C*(1 + M^2))/(1 + M^2)

= (M*(xp + M*yp) - M^2*C + C + M^2*C)/(1 + M^2)

= (M*(xp + M*yp) + C)/(1 + M^2)

L² = (xo - xp)² + (yo - yp)²

xo - xp = (xp + M*(yp - C))/(1 + M^2) - xp

= (xp + M*(yp - C) - xp*(1 + M^2))/(1 + M^2)

= (M*(yp - C) - M²*xp)/(1 + M²)

= M*(yp - C - M*xp)/(1 + M²)

yo - yp = (M*(xp + M*yp) + C)/(1 + M^2) - yp

= (M*(xp + M*yp) + C - yp*(1 + M²))/(1 + M^2)

= (M*xp + C - yp)/(1 + M²)

L² = (M*(yp - C - M*xp)/(1 + M²))² + ((M*xp + C - yp)/(1 + M²))²

= M²*(yp - C - M*xp)²/(1 + M²)² + (M*xp + C - yp)²/(1 + M²)²

= (M²*(yp - C - M*xp)² + (M*xp + C - yp)²)/(1 + M²)²

= (M²*(yp - C - M*xp)² + (-1)²(yp - C - M*xp))²/(1 + M²)²

= (M²*(yp - C - M*xp)² + (yp - C - M*xp)²)/(1 + M²)²

= (M² + 1)*(yp - C - M*xp)²/(1 + M²)²

= (yp - C - M*xp)²/(1 + M²)

=> L = |yp - M*xp - C|/√(1 + M²)

---------------------------------------------

Distance equation using line equation in standard form

A*x + B*y + C' = 0

B*y = -A*x - C'

y = -(A/B)*x - C'/B

Equate coefficients -> M = -A/B, C = -C'/B

L = |yp - M*xp - C|/√(1 + M²)

= |yp - (-A/B)*xp - (-C'/B)|/√(1 + (-A/B)²)

√(1 + (-A/B)²) = √(1 + (-1)²*A²/B²)

= √((B² + A²)/B²)

= √(B² + A²)/B

=> L = |yp + (A/B)*xp + C'/B|/(√(A² + B²)/B)

= |(B*yp + A*xp + C')/B|/(√(A² + B²)/B)

= |A*xp + B*yp + C'|/√(A² + B²)

----------------------------------------------

Sanity Checks

xp = 0, yp = 1, C = 0, M = 0

L = |1 - 0 - (0)*(0)|/√(1 + (0)²)

= |1|/√(1)

= 1

xp = 1, yp = 1, C = 0, M = -1

L = |1 - 0 - (-1)(1)|/√(1 + (-1)²)

= |1 + 1|/√(1 + 1)

= 2/√(2)

= √(2)

There is a point P with co-ordinates [xp,yp] and a line with known parameters. Find the distance between P and the nearest point on the line, or equivalently, the perpendicular distance between P and the line.

Approach

Find the co-ordinates of this point O and then use Pythagoras' Theorem to determine distance.

--------------------------------------

Point of intersection: O = [xo,yo]

Point outside line: P = [xp,yp]

y-axis intercept: Q = [xq,yq] = [0,C]

P_ = OP, Q_ = OQ

Perpendicular vectors <=> P_.Q_ = 0

Line equation (slope-intercept form): y = M*x + C

P_ = (xp - xo)*ex_ + (yp - yo)*ey_

Q_ = (xq - xo)*ex_ + (yq - yo)*ey_

= -xo*ex_ + (C - yo)*ey_

P_.Q_ = -xo*(xp - xo) + (C - yo)*(yp - yo) = 0

(C - yo)*(yp - yo) = xo*(xp - xo)

C - yo = xo*(xp - xo)/(yp - yo)

C = xo*(xp - xo)/(yp - yo) + yo

yo = M*xo + C

M*xo = yo - C

M = -(C - yo)/xo

= -(xp - xo)/(yp - yo)

= (xo - xp)/(yp - yo)

xo - xp = M*(yp - yo)

= M*yp - M*(M*xo + C)

= M*yp - M^2*xo - M*C

xo + M^2*xo = M*(yp - C) + xp

xo*(1 + M^2) =

xo = (xp + M*(yp - C))/(1 + M^2)

yo = M*(xp + M*(yp - C))/(1 + M^2)) + C

= (M*(xp + M*(yp - C)) + C*(1 + M^2))/(1 + M^2)

= (M*(xp + M*yp) - M^2*C + C + M^2*C)/(1 + M^2)

= (M*(xp + M*yp) + C)/(1 + M^2)

L² = (xo - xp)² + (yo - yp)²

xo - xp = (xp + M*(yp - C))/(1 + M^2) - xp

= (xp + M*(yp - C) - xp*(1 + M^2))/(1 + M^2)

= (M*(yp - C) - M²*xp)/(1 + M²)

= M*(yp - C - M*xp)/(1 + M²)

yo - yp = (M*(xp + M*yp) + C)/(1 + M^2) - yp

= (M*(xp + M*yp) + C - yp*(1 + M²))/(1 + M^2)

= (M*xp + C - yp)/(1 + M²)

L² = (M*(yp - C - M*xp)/(1 + M²))² + ((M*xp + C - yp)/(1 + M²))²

= M²*(yp - C - M*xp)²/(1 + M²)² + (M*xp + C - yp)²/(1 + M²)²

= (M²*(yp - C - M*xp)² + (M*xp + C - yp)²)/(1 + M²)²

= (M²*(yp - C - M*xp)² + (-1)²(yp - C - M*xp))²/(1 + M²)²

= (M²*(yp - C - M*xp)² + (yp - C - M*xp)²)/(1 + M²)²

= (M² + 1)*(yp - C - M*xp)²/(1 + M²)²

= (yp - C - M*xp)²/(1 + M²)

=> L = |yp - M*xp - C|/√(1 + M²)

---------------------------------------------

Distance equation using line equation in standard form

A*x + B*y + C' = 0

B*y = -A*x - C'

y = -(A/B)*x - C'/B

Equate coefficients -> M = -A/B, C = -C'/B

L = |yp - M*xp - C|/√(1 + M²)

= |yp - (-A/B)*xp - (-C'/B)|/√(1 + (-A/B)²)

√(1 + (-A/B)²) = √(1 + (-1)²*A²/B²)

= √((B² + A²)/B²)

= √(B² + A²)/B

=> L = |yp + (A/B)*xp + C'/B|/(√(A² + B²)/B)

= |(B*yp + A*xp + C')/B|/(√(A² + B²)/B)

= |A*xp + B*yp + C'|/√(A² + B²)

----------------------------------------------

Sanity Checks

xp = 0, yp = 1, C = 0, M = 0

L = |1 - 0 - (0)*(0)|/√(1 + (0)²)

= |1|/√(1)

= 1

xp = 1, yp = 1, C = 0, M = -1

L = |1 - 0 - (-1)(1)|/√(1 + (-1)²)

= |1 + 1|/√(1 + 1)

= 2/√(2)

= √(2)

## Sunday, 24 January 2016

## Tuesday, 17 November 2015

## Saturday, 23 May 2015

## Friday, 6 February 2015

## Thursday, 19 June 2014

### Quadratic Formula

I was always kind of sad that I was never taught how to derive the quadratic formula in school. We were taught how to use it, but it felt wrong to use an equation when I didn't understand where it came from or why it worked. At any rate it seemed very mysterious to me. I could understand there being a square root in the equation, but what was with the '4ac' term doing there?

I tried to derive the quadratic formula for myself, but my formula ended up looking a bit different from the one that we all know, but as far as I could tell worked. I later learned that my formula only worked for a=1, which I then fixed. I now know that it was just another way of expressing the quadratic equation, albeit in a much less convenient form. My chemistry teacher, as it so happens, criticised it for having too many fractions in the equation after an associate of mine pointed it out to him.

But that was many years ago, and even if I still had the notes, they are buried under thousands of pages of school work. I'll do my best to recreate the approach I took here, but you could probably find a similar derivation somewhere. It was based on a trick I was taught in class for solving quadratic equations.

For example, what is the value of x if x² + 4x + 3 = 0? We can easily solve the equation x² + 4x + 4 = 0, which we can factorise into (x + 2)² = 0, at which point it becomes obvious that x = -2. We can usilise this result by noting that x² + 4x + 3 = (x² + 4x + 4) - 1 = (x + 2)² - 1 = 0. It becomes immediately obvious then that x = -2 ± 1. Note that the former equation only had one possible solution (or the positive and negative solutions were the same, if you prefer).

Basically, our approach is to relate our quadratic equation of our choosing to an equation that is immediately solvable, and using the results of that equation to solve the one that we want. This is essentially what the quadratic formula doing for us. Anyway, here's the equation that we have to unpack:

It was probably around [8] that I originally stopped and called it a day, but we can simplify this equation by taking the '4a' out of the square root, like so:

I tried to derive the quadratic formula for myself, but my formula ended up looking a bit different from the one that we all know, but as far as I could tell worked. I later learned that my formula only worked for a=1, which I then fixed. I now know that it was just another way of expressing the quadratic equation, albeit in a much less convenient form. My chemistry teacher, as it so happens, criticised it for having too many fractions in the equation after an associate of mine pointed it out to him.

But that was many years ago, and even if I still had the notes, they are buried under thousands of pages of school work. I'll do my best to recreate the approach I took here, but you could probably find a similar derivation somewhere. It was based on a trick I was taught in class for solving quadratic equations.

For example, what is the value of x if x² + 4x + 3 = 0? We can easily solve the equation x² + 4x + 4 = 0, which we can factorise into (x + 2)² = 0, at which point it becomes obvious that x = -2. We can usilise this result by noting that x² + 4x + 3 = (x² + 4x + 4) - 1 = (x + 2)² - 1 = 0. It becomes immediately obvious then that x = -2 ± 1. Note that the former equation only had one possible solution (or the positive and negative solutions were the same, if you prefer).

Basically, our approach is to relate our quadratic equation of our choosing to an equation that is immediately solvable, and using the results of that equation to solve the one that we want. This is essentially what the quadratic formula doing for us. Anyway, here's the equation that we have to unpack:

We need to generalise our trick if we wish to use it for any coefficients of [1] that might come our way. In each and every case, our quadratic equation will end up taking the form:

Being careful not to confuse A, B and C with the coefficients of [1], we can easily solve for 'x' as:

Already we see a striking similarity with the quadratic formula that we all know, but we want to express 'x' in terms of the coefficients presented in [1]. To do this, we need to establish the rules that relate coefficients to A, B and C, something that we didn't have to do when we dealt with individual equations. We do this by expanding [2] and then equating the coefficients with [1]. So firstly:

Then, comparing the RHS with equation [1], we can establish that:

Finally, we can substitute [5], [6] and [7] into equation [3] to get

You might have noticed that although I had taken care to include both the positive and negative square root in [3], I used only the positive square root for [5]. Sadly, there are no insights to be drawn from this, the negative square root just leads to the same quadratic formula.

Subscribe to:
Posts (Atom)