Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. float DistanceFromRectangle(Vector2 point, Vector2 center, Vector2 size, float angle) {
  2.  
  3. // First, transform the point into the rectangle's local coordinate space.
  4. Vector2 offset = Rotate(point - center, -1 * angle);
  5.  
  6. // Take the absolute value of this offset, on x and y, and subtract the size.
  7. // This gives zero on the edge, increasing positively as we move away.
  8. // Clamp out any values less than zero.
  9. Vector2 outside = Max(Abs(offset) - size/2, Vector2.zero);
  10.  
  11. // Return the length of this vector, which is our distance to the closest point
  12. // on the square's edges or corners, and zero inside the square.
  13. return Length(outside);
  14. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement