Barotrauma Client Doc
RectangleExtensions.cs
1 using Microsoft.Xna.Framework;
2 
3 namespace Barotrauma.Extensions
4 {
5  public static class RectangleExtensions
6  {
7  public static Rectangle Multiply(this Rectangle rect, float f)
8  {
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));
11  }
12 
13  public static Rectangle Divide(this Rectangle rect, float f)
14  {
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));
17  }
18 
19  public static Point DivideSize(this Rectangle rect, float f)
20  {
21  return new Point((int)(rect.Width / f), (int)(rect.Height / f));
22  }
23 
24  public static Point DivideSize(this Rectangle rect, Vector2 f)
25  {
26  return new Point((int)(rect.Width / f.X), (int)(rect.Height / f.Y));
27  }
28 
29  public static Point MultiplySize(this Rectangle rect, float f)
30  {
31  return new Point((int)(rect.Width * f), (int)(rect.Height * f));
32  }
33 
34  public static Point MultiplySize(this Rectangle rect, Vector2 f)
35  {
36  return new Point((int)(rect.Width * f.X), (int)(rect.Height * f.Y));
37  }
38 
39  public static Vector2 CalculateRelativeSize(this Rectangle rect, Rectangle relativeRect)
40  {
41  return new Vector2(rect.Width, rect.Height) / new Vector2(relativeRect.Width, relativeRect.Height);
42  }
43 
44  public static Rectangle ScaleSize(this Rectangle rect, Rectangle relativeTo)
45  {
46  return rect.ScaleSize(rect.CalculateRelativeSize(relativeTo));
47  }
48 
49  public static Rectangle ScaleSize(this Rectangle rect, Vector2 scale)
50  {
51  var size = rect.MultiplySize(scale);
52  return new Rectangle(rect.X, rect.Y, size.X, size.Y);
53  }
54 
55  public static Rectangle ScaleSize(this Rectangle rect, float scale)
56  {
57  var size = rect.MultiplySize(scale);
58  return new Rectangle(rect.X, rect.Y, size.X, size.Y);
59  }
60 
61  public static bool IntersectsWorld(this Rectangle rect, Rectangle value)
62  {
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;
67  }
68 
72  public static bool ContainsWorld(this Rectangle rect, Rectangle other)
73  {
74  return
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));
77  }
78 
82  public static bool ContainsWorld(this Rectangle rect, Vector2 point)
83  {
84  return
85  (rect.X <= point.X) && (point.X < (rect.X + rect.Width)) &&
86  (rect.Y >= point.Y) && (point.Y > (rect.Y - rect.Height));
87  }
88 
92  public static bool ContainsWorld(this Rectangle rect, Point point)
93  {
94  return
95  (rect.X <= point.X) && (point.X < (rect.X + rect.Width)) &&
96  (rect.Y >= point.Y) && (point.Y > (rect.Y - rect.Height));
97  }
98  }
99 }