Advertisement
fursty

OOP2 Color Rectangle

Mar 2nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. public class ColorRectangle extends rgb implements
  2. Comparable<Object>{
  3. private int ix1,iy1,ix2,iy2;
  4. public ColorRectangle(){}
  5. public ColorRectangle(int x1,int y1,int x2,int y2)
  6. {
  7. this.ix1 = x1;
  8. this.iy1 = x2;
  9. this.ix2 = y1;
  10. this.iy2 = y1;
  11. }
  12. public int getIX1() { return ix1; }
  13. public int getIY1() { return iy1; }
  14. public int getIX2() { return ix2; }
  15. public int getIY2() { return iy2; }
  16. public int calcArea()
  17. {
  18. return Math.abs((ix2-ix1)*(iy2-iy1));
  19. }
  20. public int compareTo(Object r){
  21. if(this.calcArea()<((ColorRectangle)r).calcArea())
  22. {
  23. return -1;
  24. }
  25. if(this.calcArea()>((ColorRectangle)r).calcArea())
  26. {
  27. return 1;
  28. }
  29. return 0;
  30. }
  31. public String toString()
  32. {
  33. return "x1:" + ix1 + " y1:" + iy1 + " x2:" + ix2 + " y2:" + iy2;
  34. }
  35. public boolean equals(ColorRectangle R)
  36. {
  37. return (this.calcArea()==R.calcArea());
  38. }
  39. public void translateX(int iPoints)
  40. {
  41. ix1+=iPoints;
  42. ix2+=iPoints;
  43. }
  44. public void translateY(int iPoints)
  45. {
  46. iy1+=iPoints;
  47. iy2+=iPoints;
  48.  
  49. }
  50. public void translateXY(int iPoints)
  51. {
  52. translateX(iPoints);
  53. translateY(iPoints);
  54. }
  55. public boolean isInside(int ptX,int ptY)
  56. {
  57. return(ix1 < ptX && ix2>ptY && iy1<ptY && iy2 > ptY);
  58. }
  59. public ColorRectangle unionRect(ColorRectangle R)
  60. {
  61. int x = ix1<R.ix1? ix1 : R.ix1;
  62. int y = iy1<R.iy1? iy1 : R.iy1;
  63. int x2 = ix2>R.ix2? ix2 : R.ix2;
  64. int y2 = iy2>R.iy2? iy2 : R.iy2;
  65. return new ColorRectangle(x,y,x2,y2);
  66. }
  67. public ColorRectangle intersectionRect(ColorRectangle R)
  68. {
  69. int x = ix1>R.ix1? ix1 : R.ix1;
  70. int y = iy1>R.iy1? iy1 : R.iy1;
  71. int x2 = ix2<R.ix2? ix2 : R.ix2;
  72. int y2 = iy2<R.iy2? iy2 : R.iy2;
  73. return new ColorRectangle(x,y,x2,y2);
  74. }
  75. public static void main(String[] args)
  76. {
  77. ColorRectangle r1 = new ColorRectangle(5,5,12,12);
  78. ColorRectangle r2 = new ColorRectangle(4,4,2,2);
  79. System.out.println("r1: " + r1.toString());
  80. System.out.println("r2: " + r2.toString());
  81. if(r1.equals(r2))
  82. {
  83. System.out.println("Areas are equal");
  84. }
  85. else
  86. System.out.println("Areas are not equal");
  87. ColorRectangle U = r1.unionRect(r2);
  88. System.out.println("r3: "+U.toString());
  89. }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement