1 using Microsoft.Xna.Framework;
5 public static class RectangleExtensions
7 public static Rectangle Multiply(
this Rectangle rect,
float f)
9 Vector2 location =
new Vector2(rect.X, rect.Y) * f;
10 return new Rectangle(
new Point((
int)location.X, (
int)location.Y), rect.MultiplySize(f));
13 public static Rectangle Divide(
this Rectangle rect,
float f)
15 Vector2 location =
new Vector2(rect.X, rect.Y) / f;
16 return new Rectangle(
new Point((
int)location.X, (
int)location.Y), rect.DivideSize(f));
19 public static Point DivideSize(
this Rectangle rect,
float f)
21 return new Point((
int)(rect.Width / f), (
int)(rect.Height / f));
24 public static Point DivideSize(
this Rectangle rect, Vector2 f)
26 return new Point((
int)(rect.Width / f.X), (
int)(rect.Height / f.Y));
29 public static Point MultiplySize(
this Rectangle rect,
float f)
31 return new Point((
int)(rect.Width * f), (
int)(rect.Height * f));
34 public static Point MultiplySize(
this Rectangle rect, Vector2 f)
36 return new Point((
int)(rect.Width * f.X), (
int)(rect.Height * f.Y));
39 public static Vector2 CalculateRelativeSize(
this Rectangle rect, Rectangle relativeRect)
41 return new Vector2(rect.Width, rect.Height) /
new Vector2(relativeRect.Width, relativeRect.Height);
44 public static Rectangle ScaleSize(
this Rectangle rect, Rectangle relativeTo)
46 return rect.ScaleSize(rect.CalculateRelativeSize(relativeTo));
49 public static Rectangle ScaleSize(
this Rectangle rect, Vector2 scale)
51 var size = rect.MultiplySize(scale);
52 return new Rectangle(rect.X, rect.Y, size.X, size.Y);
55 public static Rectangle ScaleSize(
this Rectangle rect,
float scale)
57 var size = rect.MultiplySize(scale);
58 return new Rectangle(rect.X, rect.Y, size.X, size.Y);
61 public static bool IntersectsWorld(
this Rectangle rect, Rectangle value)
63 int bottom = rect.Y - rect.Height;
64 int otherBottom = value.Y - value.Height;
65 return value.Left < rect.Right && rect.Left < value.Right &&
66 value.Top > bottom && rect.Top > otherBottom;
72 public static bool ContainsWorld(
this Rectangle rect, Rectangle other)
75 (rect.X <= other.X) && ((other.X + other.Width) <= (rect.X + rect.Width)) &&
76 (rect.Y >= other.Y) && ((other.Y - other.Height) >= (rect.Y - rect.Height));
82 public static bool ContainsWorld(
this Rectangle rect, Vector2 point)
85 (rect.X <= point.X) && (point.X < (rect.X + rect.Width)) &&
86 (rect.Y >= point.Y) && (point.Y > (rect.Y - rect.Height));
92 public static bool ContainsWorld(
this Rectangle rect, Point point)
95 (rect.X <= point.X) && (point.X < (rect.X + rect.Width)) &&
96 (rect.Y >= point.Y) && (point.Y > (rect.Y - rect.Height));