Skip to content

Instantly share code, notes, and snippets.

@steveruizok
Last active April 17, 2021 20:58
Show Gist options
  • Save steveruizok/490f41cc002f4251c6f1400003b6621f to your computer and use it in GitHub Desktop.
Save steveruizok/490f41cc002f4251c6f1400003b6621f to your computer and use it in GitHub Desktop.
Find the intersection point between two rays.
/**
* Get the intersection of two rays, with origin points p0 and p1, and direction vectors n0 and n1.
* @param p0 The origin point of the first ray
* @param n0 The direction vector of the first ray
* @param p1 The origin point of the second ray
* @param n1 The direction vector of the second ray
* @returns
*/
export function getRayRayIntesection(
p0: number[],
n0: number[],
p1: number[],
n1: number[]
) {
const p0e = [p0[0] + n0[0], p0[1] + n0[1]],
p1e = [p1[0] + n1[0], p1[1] + n1[1]],
m0 = (p0e[1] - p0[1]) / (p0e[0] - p0[0]),
m1 = (p1e[1] - p1[1]) / (p1e[0] - p1[0]),
b0 = p0[1] - m0 * p0[0],
b1 = p1[1] - m1 * p1[0],
x = (b1 - b0) / (m0 - m1),
y = m0 * x + b0
return [x, y]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment