Advertisement
badunius

line math

Jul 29th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function lineEquation(ax, ay, bx, by) {
  2.   const dx = bx - ax
  3.   const dy = by - ay
  4.   const k = dx === 0 ? 0 : dy / dx
  5.   const n = -ax * k + ay
  6.   return n === 0
  7.     ? `y = ${k}x`
  8.     : `y = ${k}x ${n < 0 ? '-' : '+'} ${Math.abs(n)}`
  9. }
  10.  
  11. function lineIntersection(L1, L2) {
  12.   const den = (L1[0].x - L1[1].x) * (L2[0].y - L2[1].y) - (L1[0].y - L1[1].y) * (L2[0].x - L2[1].x)
  13.   if (den === 0) return 'No intersection'
  14.   const X = ((L1[0].x * L1[1].y - L1[0].y * L1[1].x) * (L2[0].x - L2[1].x) - (L1[0].x - L1[1].x) * (L2[0].x * L2[1].y - L2[0].y * L2[1].x)) / den
  15.   const Y = ((L1[0].x * L1[1].y - L1[0].y * L1[1].x) * (L2[0].y - L2[1].y) - (L1[0].y - L1[1].y) * (L2[0].x * L2[1].y - L2[0].y * L2[1].x)) / den
  16.   return `x: ${X}, y: ${Y}`
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement