function raysIntersect(pos1, dir1, pos2, dir2, deg_allowance) {
  if(!deg_allowance) deg_allowance = 0; // degrees divergence still considered 'parallel'
  var p = pos1;
  var q = pos2;
  var r = vectorize_2d(dir1);
  var s = vectorize_2d(dir2);
  var t = abs(vcp(vsub(q,p),s) / vcp(r,s));
  var t_norm = abs(vcp(vsub(vnorm(q),vnorm(p)),s) / vcp(r,s));
  var u_norm = abs(vcp(vsub(vnorm(q),vnorm(p)),r) / vcp(r,s));
  if(abs(vcp(r,s)) < abs(vcp([0,1],vectorize_2d(radians(deg_allowance))))) { // parallel (within allowance)
    if(vcp(vsub(vnorm(q),vnorm(p)),r) == 0) return true; // collinear
    else return false;  // parallel, non-intersecting
  }
  else if((0 <= t_norm && t_norm <= 1) && (0 <= u_norm && u_norm <= 1))
    return vadd(p,vscale(r,t)); // rays intersect here
  else return false;  // diverging, non-intersecting
}
/**
 ** Determines if and where two rays will intersect. All angles in radians.
 ** Variation based on http://stackoverflow.com/a/565282/5774767
 */
Determines if and where two rays will intersect. All angles in radians.
Determines if and where two rays will intersect . All angles in radians .
function raysIntersect(pos1, dir1, pos2, dir2, deg_allowance) {
  if(!deg_allowance) deg_allowance = 0; 
  var p = pos1;
  var q = pos2;
  var r = vectorize_2d(dir1);
  var s = vectorize_2d(dir2);
  var t = abs(vcp(vsub(q,p),s) / vcp(r,s));
  var t_norm = abs(vcp(vsub(vnorm(q),vnorm(p)),s) / vcp(r,s));
  var u_norm = abs(vcp(vsub(vnorm(q),vnorm(p)),r) / vcp(r,s));
  if(abs(vcp(r,s)) < abs(vcp([0,1],vectorize_2d(radians(deg_allowance))))) { 
    if(vcp(vsub(vnorm(q),vnorm(p)),r) == 0) return true; 
    else return false;  
  }
  else if((0 <= t_norm && t_norm <= 1) && (0 <= u_norm && u_norm <= 1))
    return vadd(p,vscale(r,t)); 
  else return false;  
}
function raysIntersect ( pos1 , dir1 , pos2 , dir2 , deg_allowance ) { if ( ! deg_allowance ) deg_allowance = 0 ; var p = pos1 ; var q = pos2 ; var r = vectorize_2d ( dir1 ) ; var s = vectorize_2d ( dir2 ) ; var t = abs ( vcp ( vsub ( q , p ) , s ) / vcp ( r , s ) ) ; var t_norm = abs ( vcp ( vsub ( vnorm ( q ) , vnorm ( p ) ) , s ) / vcp ( r , s ) ) ; var u_norm = abs ( vcp ( vsub ( vnorm ( q ) , vnorm ( p ) ) , r ) / vcp ( r , s ) ) ; if ( abs ( vcp ( r , s ) ) < abs ( vcp ( [ 0 , 1 ] , vectorize_2d ( radians ( deg_allowance ) ) ) ) ) { if ( vcp ( vsub ( vnorm ( q ) , vnorm ( p ) ) , r ) == 0 ) return true ; else return false ; } else if ( ( 0 <= t_norm && t_norm <= 1 ) && ( 0 <= u_norm && u_norm <= 1 ) ) return vadd ( p , vscale ( r , t ) ) ; else return false ; }
Determines if and where two rays will intersect.
Determines if and where two rays will intersect .
// degrees divergence still considered 'parallel' // parallel (within allowance) // collinear // parallel, non-intersecting // rays intersect here // diverging, non-intersecting