Created
March 13, 2017 23:31
-
-
Save gchumillas/3e8d253cb9788bcdc0b8563702c7c59d to your computer and use it in GitHub Desktop.
Intersection between the line AB and and the perpendicular line which contains the point C.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Point { | |
readonly x: number; | |
readonly y: number; | |
} | |
/** | |
* This function returns the intersection between the line AB | |
* and the perpendicular line that contains the point C. | |
*/ | |
function intersection(a: Point, b: Point, c: Point): Point { | |
let d0 = b.x - a.x; | |
let d1 = b.y - a.y; | |
let d2 = c.x - a.x; | |
let d3 = c.y - a.y; | |
let divisor = d0 * d0 + d1 * d1; | |
let x = divisor > 0 ? (d0 * d2 + d1 * d3) / divisor : 0; | |
return {x: a.x + d0 * x, y: a.y + d1 * x}; | |
} | |
let d = intersection({ x: 0, y: 0 }, { x: 10, y: 10 }, { x: 5, y: 0 }); | |
alert(`(${d.x}, ${d.y})`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment