1 module polyplex.math.rectangle; 2 import polyplex.math; 3 4 /// Base template for all AABB-types. 5 /// Params: 6 /// type = all values get stored as this type 7 class Rectangle { 8 private int x = 0; 9 private int y = 0; 10 private int width = 0; 11 private int height = 0; 12 13 this() { } 14 15 this(int width, int height) { 16 this.x = 0; 17 this.y = 0; 18 this.width = width; 19 this.height = height; 20 } 21 22 this(int x, int y, int width, int height) { 23 this.x = x; 24 this.y = y; 25 this.width = width; 26 this.height = height; 27 } 28 29 public @property int X() { return this.x; } 30 public @property void X(int x) { this.x = x; } 31 32 public @property int Y() { return this.y; } 33 public @property void Y(int y) { this.y = y; } 34 35 public @property int Width() { return this.width; } 36 public @property void Width(int width) { this.width = width; } 37 38 public @property int Height() { return this.height; } 39 public @property void Height(int height) { this.height = height; } 40 41 public int Left() { return this.x; } 42 public int Right() { return this.x + this.width; } 43 public int Top() { return this.y; } 44 public int Bottom() { return this.y + this.height; } 45 public Vector2 Center() { return Vector2(this.x + (this.width/2), this.y + (this.height/2)); } 46 47 public bool Intersects(Rectangle other) { 48 if (other is null) return false; 49 bool v = (other.Left > this.Right || other.Right < this.Left || other.Top > this.Bottom || other.Bottom < this.Top); 50 return !v; 51 } 52 53 public bool Intersects(Vector2 other) { 54 bool v = (other.X > this.Right || other.X < this.Left || other.Y > this.Bottom || other.Y < this.Top); 55 return !v; 56 } 57 58 public Rectangle Displace(int x, int y) { 59 return new Rectangle(this.X+x, this.Y+y, this.width, this.height); 60 } 61 62 public Rectangle Expand(int x, int y) { 63 return new Rectangle(this.X-x, this.Y-y, this.width+x, this.height+y); 64 } 65 }