Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. package mypkg;
  2.  
  3. public class Rectangle
  4. {
  5. private int iX1, iY1, iX2, iY2;
  6.  
  7. public Rectangle()
  8. {
  9. iX1 = 0;
  10. iY1 = 0;
  11. iX2 = 0;
  12. iY2 = 0;
  13. }
  14.  
  15. public Rectangle(int x1, int y1, int x2, int y2)
  16. {
  17. iX1 = x1;
  18. iY1 = y1;
  19. iX2 = x2;
  20. iY2 = y2;
  21. }
  22.  
  23.  
  24.  
  25. public int getiX1()
  26. {
  27. return iX1;
  28. }
  29.  
  30. public int getiY1()
  31. {
  32. return iY1;
  33. }
  34.  
  35. public int getiX2()
  36. {
  37. return iX2;
  38. }
  39.  
  40. public int getiY2()
  41. {
  42. return iY2;
  43. }
  44.  
  45. public int calcArea()
  46. {
  47. int height = getiY1() - getiY2();
  48. int width = getiX2() - getiX1();
  49. return height*width;
  50. }
  51.  
  52. public int compareTo(Object r)
  53. {
  54. if ( (getiY1() - getiY2()) * (getiX2() - getiX1()) > ((Rectangle) r).calcArea())
  55. return 1;
  56. else if ( (getiY1() - getiY2()) * (getiX2() - getiX1()) < ((Rectangle) r).calcArea())
  57. return -1;
  58. else
  59. return 0;
  60. }
  61.  
  62. public String toString()
  63. {
  64. return iX1+" "+iY1+" "+iX2+" "+iY2;
  65. }
  66.  
  67. public boolean equals(Rectangle r)
  68. {
  69. return this.calcArea() == r.calcArea();
  70. }
  71.  
  72. public void translateX(int iPoints)
  73. {
  74. iX1 = iX1 + iPoints;
  75. iX2 = iX2 + iPoints;
  76. }
  77.  
  78. public void translateY(int iPoints)
  79. {
  80. iX1 = iX1 + iPoints;
  81. iX2 = iX2 + iPoints;
  82. }
  83.  
  84. public void translateXY(int iPoints)
  85. {
  86. iX1 = iX1 + iPoints;
  87. iX2 = iX2 + iPoints;
  88. iX1 = iX1 + iPoints;
  89. iX2 = iX2 + iPoints;
  90. }
  91.  
  92. public boolean isInside(int ptX, int ptY)
  93. {
  94. if(ptX > iX1 && ptX < iX2 && ptY > iY1 && ptY < iY2)
  95. return true;
  96. return false;
  97. }
  98.  
  99. public Rectangle intersectionRect(Rectangle r)
  100. {
  101. int leftX = Math.max(this.iX1, r.iX1);
  102. int rightX = Math.min(this.iX2, r.iX2);
  103. int topY = Math.max(this.iY1, r.iY1);
  104. int bottomY = Math.min(this.iY2, r.iY2);
  105.  
  106. if (leftX < rightX && topY < bottomY)
  107. return new Rectangle(leftX, topY, rightX, bottomY);
  108. else
  109. return null;
  110. }
  111. public Rectangle unionRect(Rectangle r)
  112. {
  113. int leftX = Math.min(this.iX1, r.iX1);
  114. int rightX = Math.max(this.iX2, r.iX2);
  115. int topY = Math.min(this.iY1, r.iY1);
  116. int bottomY = Math.max(this.iY2, r.iY2);
  117.  
  118. if (leftX < rightX && topY < bottomY)
  119. return new Rectangle(leftX, topY, rightX, bottomY);
  120. else
  121. return null;
  122. }
  123. /*public static void main(String[] args)
  124. {
  125. Rectangle oR1 = new Rectangle(0,0,10,8);
  126. Rectangle oR2 = new Rectangle(2,3,7,9);
  127.  
  128. System.out.println(oR1);
  129. System.out.println(oR2);
  130.  
  131. oR1.calcArea();
  132.  
  133. }*/
  134. }
  135.  
  136. public class Color implements Comparable
  137. {
  138. private long RValue, GValue, BValue;
  139. private long ColorValue = 256*256*RValue + 256*GValue + 1*BValue;
  140.  
  141. public Color()
  142. {}
  143.  
  144. public Color(long c)
  145. {
  146. ColorValue = c;
  147. }
  148.  
  149. public long getRValue()
  150. {
  151. return RValue;
  152. }
  153.  
  154. public long getGValue()
  155. {
  156. return GValue;
  157. }
  158.  
  159. public long getBValue()
  160. {
  161. return BValue;
  162. }
  163.  
  164. public void setRValue(long R)
  165. {
  166. RValue = R;
  167. }
  168.  
  169. public void setGValue(long G)
  170. {
  171. GValue = G;
  172. }
  173.  
  174. public void setBValue(long B)
  175. {
  176. BValue = B;
  177. }
  178.  
  179. public String toString()
  180. {
  181. return RValue+" "+GValue+" "+BValue+" "+Color;
  182. }
  183.  
  184. public boolean equals(Object c)
  185. {
  186. if(this.RValue == ((Color) c).getRValue() && this.GValue == ((Color) c).getGValue() && this.BValue == ((Color) c).getBValue())
  187. return true;
  188. }
  189.  
  190. public int compareTo(Object c)
  191. {
  192. if (ColorValue < ((Color) c).ColorValue)
  193. return -1;
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement