Guest User

Untitled

a guest
Mar 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. /**
  2. * 2D Equivalent of the box
  3. *
  4. * @author Mark van der Wal
  5. * @since 22/02/18
  6. */
  7. public class Area {
  8.  
  9. private Vector2f min;
  10. private Vector2f max;
  11.  
  12. public Area(float x, float y) {
  13. this();
  14. grow(x, y);
  15. }
  16.  
  17. public Area() {
  18. min = new Vector2f();
  19. max = new Vector2f();
  20. reset();
  21. }
  22.  
  23. public void reset() {
  24. min.x = Float.MAX_VALUE;
  25. min.y = Float.MAX_VALUE;
  26.  
  27. max.x = Float.MIN_VALUE;
  28. max.y = Float.MIN_VALUE;
  29. }
  30.  
  31. public void grow(Vector2f point) {
  32. grow(point.x, point.y);
  33. }
  34.  
  35. public void grow(float x, float y) {
  36. min.x = Math.min(x, min.x);
  37. min.y = Math.min(y, min.y);
  38.  
  39. max.x = Math.max(x, max.x);
  40. max.y = Math.max(y, max.y);
  41. }
  42.  
  43. /**
  44. * reset the area and grow from all gives values
  45. *
  46. * @param values all values the area is going to grow from
  47. */
  48. public void regenerate(float... values) {
  49. if (values.length > 0 && values.length % 2 == 0) {
  50. reset();
  51.  
  52. for (int i = 0; i < values.length; i += 2) {
  53. grow(values[i], values[i + 1]);
  54. }
  55. }
  56. }
  57.  
  58. public void regenerate(Vector2f... points) {
  59. if (points.length > 0) {
  60. reset();
  61.  
  62. for (Vector2f point : points) {
  63. grow(point);
  64. }
  65. }
  66. }
  67.  
  68. public boolean intersects(int x, int y) {
  69. return x >= min.x && x <= max.x && y >= min.y && y <= max.y;
  70. }
  71.  
  72. public boolean hasVolume() {
  73. float absX = Math.abs(min.x - max.x);
  74. float absY = Math.abs(min.y - max.y);
  75.  
  76. return absX > 0 && absY > 0;
  77. }
  78.  
  79. public boolean hasArea() {
  80. float absX = Math.abs(min.x - max.x);
  81. float absY = Math.abs(min.y - max.y);
  82.  
  83. return absX > 0 || absY > 0;
  84. }
  85.  
  86. public float getX() {
  87. return min.x;
  88. }
  89.  
  90. public float getY() {
  91. return min.y;
  92. }
  93.  
  94. public float getWidth() {
  95. return max.x - min.x;
  96. }
  97.  
  98. public float getHeight() {
  99. return max.y - min.y;
  100. }
  101.  
  102. public Vector2f getMin() {
  103. return min;
  104. }
  105.  
  106. public Vector2f getMax() {
  107. return max;
  108. }
  109. }
Add Comment
Please, Sign In to add comment