duck

duck

Nov 19th, 2010
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.44 KB | None | 0 0
  1.     public static float SignedDistanceToLine2D( Vector2 point, Vector2 lineStart, Vector2 lineDirection )
  2.     {
  3.         Vector2 v = lineDirection;
  4.         Vector2 w = point - lineStart;     
  5.         float c1 = Vector2.Dot(w,v);
  6.         float c2 = Vector2.Dot(v,v);
  7.         float b = c1 / c2;     
  8.         Vector2 pointOnLine = lineStart + b * v;
  9.        
  10.         Vector2 diff = (point-pointOnLine);
  11.         int side = Vector2.Dot(diff,lineDirection) > 0 ? 1 : -1;
  12.        
  13.        
  14.         return diff.magnitude * side;
  15.     }
Advertisement
Add Comment
Please, Sign In to add comment