Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.02 KB | None | 0 0
  1. public class Point
  2. {
  3. private final double DEFAULT_COORDINATE=90;
  4. private final double DEFAULT_VAL=0;
  5. private double _radius;
  6. private double _alpha;
  7. /**
  8. * Constructor for objects of class Point. Construct a new point with the specified x y coordinates. If the x coordinate is negative it is set to zero. If the y coordinate is negative it is set to zero.
  9. *@param
  10. *x - The x coordinate
  11. *@param
  12. *y - The y coordinate*/
  13. public Point (double x, double y)
  14. {
  15. if(x>=DEFAULT_VAL)
  16. x=x;
  17. else
  18. x=DEFAULT_VAL;
  19. if(y>=DEFAULT_VAL)
  20. y=y;
  21. else
  22. y=DEFAULT_VAL;
  23. if (x==DEFAULT_VAL)
  24. _alpha=DEFAULT_COORDINATE;
  25. else
  26. _alpha=getAlpha(x,y);
  27. _radius=getRadius(x,y);
  28. }
  29.  
  30. private double getRadius (double x,double y)
  31. {
  32. _radius=Math.sqrt((Math.pow(x,2)+Math.pow(y,2)));
  33. return _radius;
  34. }
  35.  
  36. private double getAlpha (double x, double y)
  37. {
  38. _alpha=Math.atan(y/x);
  39. _alpha =(180/Math.PI)*_alpha;
  40. return _alpha;
  41. }
  42.  
  43. /**
  44. *Constructor for objects of class Point. Copy constructor, construct a point using another point.
  45. *@param
  46. *other - The point from which to construct the new object
  47. */
  48. public Point (Point other)
  49. {
  50. _radius=other._radius;
  51. _alpha=other._alpha;
  52. }
  53.  
  54. /**This method returns the x coordinate of the point.
  55. *@return
  56. *The x coordinate of the point*/
  57. public double getX()
  58. {
  59. double returnredian=(_alpha)*(Math.PI/180);
  60. double returnX= Math.cos(returnredian)*_radius;
  61. double x=Math.round(returnX*10000)/(double)10000;
  62. return x;
  63. }
  64.  
  65. /**This method returns the y coordinate of the point.
  66. *@return
  67. *The y coordinate of the point*/
  68. public double getY()
  69. {
  70. double returnredian=_alpha/(180/Math.PI);
  71. double returnY= Math.sin(returnredian)*_radius;
  72. double y=Math.round(returnY*10000)/(double)10000;
  73. return y;
  74. }
  75.  
  76. /**This method sets the x coordinate of the point. If the new x coordinate is negative the old x coordinate will remain unchanged.
  77. *@param
  78. *x - The new x coordinate
  79. */
  80. public void setX (double x)
  81. {
  82. double currentY = getY();
  83. if(x>=DEFAULT_VAL)
  84. x=x;
  85. else
  86. x=DEFAULT_VAL;
  87. _radius=getRadius (x,currentY);
  88. _alpha=getAlpha (x,currentY);
  89. _alpha=Math.round(_alpha*10000)/(double)10000;
  90. }
  91.  
  92. /**This method sets the y coordinate of the point. If the new y coordinate is negative the old y coordinate will remain unchanged.
  93. *@param
  94. *y - The new y
  95. */
  96. public void setY (double y)
  97. {
  98. double currentX = getX();
  99. if(y>=DEFAULT_VAL)
  100. y=y;
  101. else
  102. y=DEFAULT_VAL;
  103. _radius=getRadius (currentX,y);
  104. _alpha=getAlpha (currentX,y);
  105. _alpha=Math.round(_alpha*10000)/(double)10000;
  106. }
  107.  
  108. /**Returns a string representation of Point in the format (x,y).
  109. *@Overrides
  110. *toString in class java.lang.Object
  111. *@return
  112. *A String representation of the Point*/
  113.  
  114. public String toString()
  115. {
  116. return "("+getX()+","+getY()+")";
  117.  
  118. }
  119.  
  120. /**Check if the given point is equal to this point.
  121. *@param
  122. *other - The point to check equality with
  123. *@return
  124. *True if the given point is equal to this point
  125. */
  126. public boolean equals (Point other)
  127. {
  128. return getX() == other.getX() && getY() == other.getY();
  129. }
  130.  
  131. /**Check if this point is above a received point.
  132. *@param
  133. *other- The point to check if this point is above
  134. *@return
  135. *True if this point is above the other point*/
  136. public boolean isAbove (Point other)
  137. {
  138. return other.getY()<getY();
  139. }
  140.  
  141. /**Check if this point is below a received point.
  142. *@param
  143. *other - The point to check if this point is below
  144. *@return
  145. *True if this point is below the other point*/
  146. public boolean isUnder (Point other)
  147. {
  148. return other.getY()>getY();
  149. }
  150.  
  151. /**Check if this point is left or a received point.
  152. *@param
  153. *other- The point to check if this point is left of.
  154. *@return
  155. *True if this point is left of the other point.*/
  156. public boolean isLeft (Point other)
  157. {
  158. return other.getX()>getX();
  159. }
  160.  
  161. /**Check if this point is right of a received point.
  162. *@param
  163. *other - The point to check if this point is right of
  164. *@return
  165. *True if this point is right of the other point*/
  166. public boolean isRight (Point other)
  167. {
  168. return other.getX()<getX();
  169. }
  170.  
  171. /**Check the distance between this point and a given point.
  172. *@param
  173. *other - The point to check the distance from
  174. *@return
  175. *the distance*/
  176. public double distance (Point other)
  177. {
  178. return Math.sqrt(Math.pow((other.getY()-getY()),2)+Math.pow((other.getX()-getX()),2));
  179. }
  180.  
  181. /**Moves a point. If either coordinate becomes negative the point remains unchanged.
  182. *@param
  183. *dx - The difference to add to x
  184. *@param
  185. *dy - The difference to add to y*/
  186. public void move (double dX, double dY)
  187. {
  188. if((getX()+dX)>=DEFAULT_VAL&&(getY()+dY)>=DEFAULT_VAL)
  189. {
  190. setX(getX()+dX);
  191. setY(getY()+dY);}
  192.  
  193. }
  194. }
  195.  
  196. public class Segment1
  197. {
  198. private Point _poLeft;
  199. private Point _poRight;
  200. final double DEFAULT_VAL=0;
  201. /**Constructs a new segment using two Points. If the y coordinates are different, change the y of the right point to be equal to the y of the left point.
  202. *@param
  203. *@param left - the left point of the segment
  204. *@param right - the right point of the segment*/
  205. public Segment1 (Point left, Point right)
  206. {
  207. _poLeft=new Point(left);
  208. _poRight=new Point(right);
  209. double leftY=_poLeft.getY();
  210. double rightY = _poRight.getY();
  211. if (leftY!=rightY)
  212. _poRight.setY(leftY);
  213. }
  214.  
  215. /**Constructs a new segment using 4 specified x y coordinates: Two coordinates for the left point and two coordinates for the right point. If the y coordinates are different, change the y of the right point to be equal to the y of the left point.
  216. *@param
  217. *@param leftx-X value of left point.
  218. *@param lefty-Y value of left point.
  219. *@param rightx-X value of right point.
  220. *@param righty-Y value of right point.
  221. */
  222. public Segment1 (double leftx, double lefty ,double rightx, double righty)
  223. {
  224. if (lefty!=leftx)
  225. righty=lefty;
  226. _poLeft = new Point(leftx, lefty);
  227. _poRight = new Point(leftx, lefty);
  228. }
  229.  
  230. /**Copy Constructor. Construct a segment using a reference segment
  231. @param
  232. *other - the reference segment*/
  233. public Segment1 (Segment1 other)
  234. {
  235. _poLeft=new Point (other._poLeft);
  236. _poRight=new Point (other._poRight);
  237. }
  238.  
  239. /**Returns the left point of the segment.
  240. *@return
  241. *The left point of the segment*/
  242.  
  243. public Point getPoLeft()
  244. {
  245. return new Point(_poLeft);
  246. }
  247.  
  248. /**Returns the right point of the segment.
  249.  
  250. *@return
  251. *The right point of the segment*/
  252. public Point getPoRight() {
  253. return new Point (_poRight);
  254. }
  255.  
  256. /**Returns the segment length.
  257. *@return
  258. *The segment length*/
  259. public double getLength(){
  260. return _poLeft.distance(_poRight);
  261. }
  262.  
  263. /**Return a string representation of this segment in the format (3.0,4.0)---(3.0,6.0).
  264. *@override
  265. *toString in class java.lang.Object
  266. *@return
  267. *String representation of this segment*/
  268.  
  269. public String toString()
  270. {
  271. return _poLeft.toString()+"---"+_poRight.toString();
  272.  
  273. }
  274.  
  275. /**Check if the reference segment is equal to this segment.
  276. *@param
  277. *other - the reference segment
  278. *@return
  279. *True if the reference segment is equal to this segment*/
  280. public boolean equals (Segment1 other)
  281. {
  282. return _poLeft==other._poLeft&&_poRight==other._poRight;
  283. }
  284.  
  285. /**Check if this segment is above a reference segment.
  286. *@param
  287. *other - the reference segment
  288. *@return
  289. *True if this segment is above the reference segment*/
  290. public boolean isAbove (Segment1 other)
  291. {
  292. return _poLeft.isAbove(other._poLeft);
  293. }
  294.  
  295. /**Check if this segment is under a reference segment.
  296. *@param
  297. *other - the reference segment
  298. *@return
  299. *True if this segment is under the reference segment*/
  300. public boolean isUnder (Segment1 other)
  301. {
  302. return other._poLeft.isAbove(_poLeft);
  303. }
  304.  
  305. /**Check if this segment is left of a received segment.
  306. *@param
  307. *other - the reference segment
  308. *@return
  309. *True if this segment is left to the reference segment*/
  310. public boolean isLeft (Segment1 other)
  311. {
  312. return _poLeft.isLeft(other._poLeft)&&_poRight.isLeft(other._poRight);
  313. }
  314.  
  315. /**Check if this segment is right of a received segment.
  316. *@param
  317. *other - the reference segment
  318. *@return
  319. *True if this segment is right to the reference segment*/
  320. boolean isRight (Segment1 other)
  321. {
  322. return _poLeft.isRight(other._poLeft)&&_poRight.isRight(other._poRight);
  323. }
  324.  
  325. /**Move the segment horizontally by delta.
  326. *@param
  327. *delta - the displacement size*/
  328. public void moveHorizontal (double delta)
  329. {
  330.  
  331. _poLeft.move(delta, 0);
  332. _poRight.move(delta, 0);
  333.  
  334. }
  335.  
  336. /**Move the segment vertically by delta.
  337. *@param
  338. *delta - the displacement size*/
  339. public void moveVertical (double delta)
  340. {
  341.  
  342. _poLeft.move(0,delta);
  343. _poRight.move(0,delta);
  344.  
  345. }
  346.  
  347. /**Change the segment size by moving the right point by delta. Will be implemented only for a valid delta: only if the new right point remains the right point.
  348. *@param
  349. *delta - the length change*/
  350. public void changeSize (double delta)
  351. {
  352. if (_poLeft.isLeft(_poRight)){
  353. _poRight.move(_poRight.getX()+delta,_poRight.getY());
  354. }
  355. }
  356.  
  357. /**Check if a point is located on the segment.
  358. *@param
  359. *p - a point to be checked
  360. *@return
  361. *True if p is on this segment*/
  362. public boolean pointOnSegment (Point p)
  363. {
  364. return p.getY()==_poLeft.getY() &&
  365. _poLeft.getX()<=p.getX() &&p.getX()>=_poRight.getX();
  366. }
  367.  
  368. /**Check if this segment is bigger than a reference segment.
  369. *@param
  370. *other - the reference segment
  371. *@return
  372. *True if this segment is bigger than the reference segment*/
  373. public boolean isBigger (Segment1 other)
  374. {
  375. return getLength()>other.getLength();
  376. }
  377.  
  378. /**Returns the overlap size of this segment and a reference segment.
  379. *@param
  380. *other - the reference segment
  381. *@return
  382. *The overlap size*/
  383. public double overlap (Segment1 other)
  384. {
  385. double Ls1=_poLeft.distance(_poRight);
  386. double Ls2=other._poLeft.distance(other._poRight);
  387. //option #1
  388. if (_poLeft.getX()>=other._poLeft.getX()&&_poRight.getX()>other._poLeft.getX()&&_poLeft.getX()<other._poRight.getX()&&_poRight.getX()<=other._poRight.getX())
  389. return Ls1;
  390. //option #2
  391. else if (other._poRight.getX()<_poLeft.getX()&&other._poRight.getX()<_poRight.getX()&&other._poRight.isRight(_poLeft))
  392. return DEFAULT_VAL;
  393.  
  394. //option #3
  395. else if (other._poLeft.getX()>=_poLeft.getX()&&other._poRight.getX()<=_poRight.getX()&&other._poLeft.getX()<_poRight.getX()&&other._poRight.getX()>_poLeft.getX())
  396. return Ls2;
  397. //option #4
  398. else if (_poLeft.getX()<other._poLeft.getX()&&_poRight.getX()>other._poLeft.getX()&&_poRight.getX()<other._poRight.getX())
  399. return _poRight.getX()-other._poLeft.getX();
  400. //option #5
  401. else if (_poRight.getX()<other._poLeft.getX()&&_poRight.getX()<other._poRight.getX()&&_poRight.isRight(other._poLeft))
  402. return DEFAULT_VAL;
  403. //option #6
  404. else if (other._poLeft.getX()<_poLeft.getX()&&_poLeft.getX()<other._poRight.getX()&&_poRight.getX()>other._poRight.getX())
  405. return other._poRight.getX()-_poLeft.getX();
  406. else return DEFAULT_VAL;
  407. }
  408.  
  409. /**Compute the trapeze perimeter, which is constructed by this segment and a reference segment.
  410. *@param
  411. *other - the reference segment
  412. *@return
  413. *The trapeze perimeter*/
  414. public double trapezePerimeter (Segment1 other)
  415. {
  416. double L1=other._poLeft.distance(other._poRight);
  417. double L2=_poLeft.distance(_poRight);
  418. double L3=_poLeft.distance(other._poLeft);
  419. double L4=_poRight.distance(other._poRight);
  420. return L1+L2+L3+L4 ;
  421. }
  422. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement