Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1.  
  2. public class Rectangle {
  3. private int ix1, iy1, ix2, iy2;
  4.  
  5. public Rectangle() {}
  6.  
  7. public Rectangle(int x1, int y1, int x2, int y2) {
  8. ix1 = x1; iy1 = y1; ix2 = x2; iy2 = y2;
  9. }
  10.  
  11. public int getX1() {
  12. return ix1;
  13. }
  14. public int getY1() {
  15. return iy1;
  16. }
  17. public int getX2() {
  18. return ix2;
  19. }
  20. public int getY2() {
  21. return iy2;
  22. }
  23.  
  24. public int calcArea() {
  25. return Math.abs((ix2 - ix1) * (iy2 - iy1));
  26. }
  27.  
  28. public int compareTo(Object r) {
  29. if(this.calcArea() < ((Rectangle)r).calcArea())
  30. return -1;
  31. if(this.calcArea() > ((Rectangle)r).calcArea())
  32. return 1;
  33. return 0;
  34. }
  35.  
  36. public String toString() {
  37. return "ix1: " + ix1 + ", iy1: " + iy1 + ", ix2: " + ix2 + ", iy2: " + iy2;
  38. }
  39.  
  40. public boolean equals(Rectangle r) {
  41. return this.calcArea() == r.calcArea();
  42. }
  43.  
  44. public void translateX(int iPoints) {
  45. ix1 += iPoints;
  46. ix2 += iPoints;
  47. }
  48.  
  49. public void translateY(int iPoints) {
  50. iy1 += iPoints;
  51. iy2 += iPoints;
  52. }
  53.  
  54. public void translateXY(int iPoints) {
  55. translateX(iPoints);
  56. translateY(iPoints);
  57. }
  58.  
  59. public boolean isInside(int ptx, int pty) {
  60. return (ix1 < ptx && ix2 > ptx && iy1 < pty && iy2 > pty);
  61. }
  62.  
  63. public Rectangle unionRect(Rectangle r) {
  64. int x1 = Math.min(ix1, r.ix1);
  65. int y1 = Math.min(iy1, r.iy1);
  66. int x2 = Math.max(ix2, r.ix2);
  67. int y2 = Math.max(iy2, r.iy2);
  68. return new Rectangle(x1, y1, x2, y2);
  69. }
  70.  
  71. public Rectangle intersectionRect(Rectangle r) {
  72. int x1 = Math.max(ix1, r.ix1);
  73. int y1 = Math.max(iy1, r.iy1);
  74. int x2 = Math.min(ix2, r.ix2);
  75. int y2 = Math.min(iy2, r.iy2);
  76. return new Rectangle(x1, y1, x2, y2);
  77. }
  78.  
  79. public static void main(String[] args) {
  80. Rectangle rect = new Rectangle(2,2,10,10);
  81. System.out.println(rect.toString());
  82.  
  83. Rectangle rect2 = new Rectangle(5,5,8,8);
  84. System.out.println(rect2.toString());
  85.  
  86. if(rect.equals(rect2))
  87. System.out.println("rect == rect2");
  88. else System.out.println("rect != rect2");
  89.  
  90. if(rect.compareTo(rect2) == -1)
  91. System.out.println("rect < rect2");
  92. else if(rect.compareTo(rect2) == 1) System.out.println("rect > rect2");
  93. else System.out.println("rect == rect2");
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement