ZenScript main repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DIRectangle.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package org.openzen.drawablegui;
  7. /**
  8. *
  9. * @author Hoofdgebruiker
  10. */
  11. public class DIRectangle {
  12. public static final DIRectangle EMPTY = new DIRectangle(0, 0, 0, 0);
  13. public final int x;
  14. public final int y;
  15. public final int width;
  16. public final int height;
  17. public DIRectangle(int x, int y, int width, int height) {
  18. if (width < 0)
  19. throw new IllegalArgumentException("Width must be >= 0");
  20. if (height < 0)
  21. throw new IllegalArgumentException("Height must be >= 0");
  22. this.x = x;
  23. this.y = y;
  24. this.width = width;
  25. this.height = height;
  26. }
  27. public int getCenterX() {
  28. return (x + width) / 2;
  29. }
  30. public int getCenterY() {
  31. return (y + height) / 2;
  32. }
  33. public boolean contains(int x, int y) {
  34. return x >= this.x && x < (this.x + this.width)
  35. && y >= this.y && y < (this.y + this.height);
  36. }
  37. public boolean overlaps(DIRectangle other) {
  38. if (x + width < other.x)
  39. return false;
  40. if (y + height < other.y)
  41. return false;
  42. if (other.x + other.width < x)
  43. return false;
  44. if (other.y + other.height < y)
  45. return false;
  46. return true;
  47. }
  48. public DIRectangle offset(int x, int y) {
  49. return new DIRectangle(this.x + x, this.y + y, width, height);
  50. }
  51. public DIRectangle expand(int edge) {
  52. return new DIRectangle(x - edge, y - edge, width + 2 * edge, height + 2 * edge);
  53. }
  54. public DIRectangle union(DIRectangle other) {
  55. int left = Math.min(x, other.x);
  56. int top = Math.min(y, other.y);
  57. int right = Math.max(x + width, other.x + other.width);
  58. int bottom = Math.max(y + height, other.y + other.height);
  59. return new DIRectangle(left, top, right - left, bottom - top);
  60. }
  61. public DIRectangle intersect(DIRectangle other) {
  62. int left = Math.max(x, other.x);
  63. int top = Math.max(y, other.y);
  64. int right = Math.min(x + width, other.x + other.width);
  65. int bottom = Math.min(y + height, other.y + other.height);
  66. if (right < left || bottom < top)
  67. return EMPTY;
  68. return new DIRectangle(left, top, right - left, bottom - top);
  69. }
  70. @Override
  71. public String toString() {
  72. return "(x = " + x + ", y = " + y + ", width = " + width + ", height = " + height + ")";
  73. }
  74. }