Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1.  
  2. /**
  3. * This code converts the cartesian system points to the polar system.
  4. *
  5. * @author Daniel Beilin
  6. * @version Maman12 April 17
  7. */
  8. public class Point
  9. {
  10. // instance variables - the class gets two points
  11. /**
  12. * @param _radius - vector length from 0,0 to x,y
  13. * @param _alpha - represents the angle of the vector on the X line.
  14. * @param
  15. */
  16. private double _radius;
  17. private double _alpha;
  18. private final int DEFAULT_VAL=0;
  19. private final int DEFAULT_X=0;
  20. private final int DEFAULT_Y=0;
  21.  
  22. public Point(double x, double y) //checks if the paramters are equal or bigger than 0. if not, initializes them as 0.
  23. {
  24. if (x<=DEFAULT_VAL)
  25. {
  26. x=DEFAULT_X;
  27. }
  28. if (y<=DEFAULT_VAL)
  29. {
  30. y=DEFAULT_Y;
  31. }
  32. _radius = Math.sqrt(Math.pow((x - DEFAULT_X),2)+Math.pow((y - DEFAULT_Y),2));
  33. _alpha = Math.acos( x / _radius );
  34. }
  35. /**
  36. * Copy constructor for Point
  37. * constructs a point with same info as other point
  38. * @param other gets the original points to construct a new copied point
  39. */
  40. public Point (Point other)
  41. {
  42. _radius = other._radius;
  43. _alpha = other._alpha;
  44. }
  45.  
  46. private double Round(double d)
  47. {
  48. return Math.round(d*10000)/(double)10000);
  49. }
  50. /**
  51. * returns the X variable
  52. * @return the X coordinate of the point
  53. */
  54. public double getX() //לזכור להמיר מרדיאנים למעלות
  55. {
  56. if (_alpha == (Math.PI/2));{
  57. return 0;
  58. }
  59. return Math.cos (_alpha * _radius);
  60.  
  61. }
  62. /**
  63. * returns the Y variable
  64. * @return the Y coordinate of the point
  65. */
  66. public double getY()
  67. {
  68. if (_alpha == (Math.PI/2));{
  69. return 0;
  70. }
  71. return Math.sin( _alpha * _radius);
  72. }
  73. /**
  74. * Sets the X coordinate of the point
  75. * if it receives a number smaller than 0, then X doesn't change
  76. * @param x the new X coordinate
  77. */
  78. public void setX(double x)
  79. {
  80. if (x<=DEFAULT_VAL)
  81. x = DEFAULT_X;
  82. _alpha = Math.tan (getY() * x);
  83. _radius = Math.sqrt(Math.pow((getX() - DEFAULT_X),2)+Math.pow((getY() - DEFAULT_Y),2));
  84. }
  85. /**
  86. * Sets the Y coordinate of the point
  87. * if it receives a number smaller than 0, then Y doesn't change
  88. * @param x the new Y coordinate
  89. */
  90. public void setY(double y)
  91. {
  92. if (y<=DEFAULT_VAL)
  93. y = DEFAULT_Y;
  94. _alpha = Math.sin (getX() * y);
  95. _radius = Math.sqrt(Math.pow((getX() - DEFAULT_X),2)+Math.pow((getY() - DEFAULT_Y),2));
  96. }
  97. /**
  98. * returns a string with the point values
  99. * @return String values
  100. */
  101. public String toString()
  102. {
  103. return "(" + getX() + "," + getY() + ")";
  104. }
  105. /**
  106. * Checks if the point is equal to the other point
  107. * @other the point to compare to
  108. * @return true if the points are equal
  109. */
  110. public boolean equals(Point other)
  111. {
  112. return (other._radius == _radius && other._alpha == _alpha);
  113. }
  114.  
  115. /**
  116. * Checks if the point is above other point
  117. * @param Other the other point to compare to
  118. * @return true if point is above other point.
  119. */
  120. //public boolean isAbove(Point other)
  121.  
  122. /*
  123. * Checks if the point is under other point
  124. * @param Other the other point to compare to
  125. * @return true if point is under other point
  126.  
  127. public boolean isUnder(Point other)
  128. {
  129. return (other.isAbove(this));
  130. }
  131. /**
  132. * Checks if point is left to other
  133. * @param the point to compare to
  134. * @return true if the point is left to other point
  135.  
  136. public boolean isLeft(Point other)
  137. {
  138. return (other._x > _x);
  139. }
  140. /**
  141. * Checks if point is right to other
  142. * @param the point to compare to
  143. * @return true if the point is right to other point
  144. *
  145. public boolean isRight(Point other)
  146. {
  147. return (other.isLeft(this));
  148. }
  149.  
  150. public double distance(Point p)
  151. {
  152. return Math.sqrt(Math.pow((_x - p._x),2)+Math.pow((_y - p._y),2));
  153. }
  154. /*
  155. * Moves the x coordinate by dx and the y by dy
  156. * if it moves the points outside the first quarter, then the point will stay the same
  157. * @param dX, the amount to move X
  158. * @param dY, the amount to move Y
  159. *
  160. public void move (double dX, double dY)
  161. {
  162. if ((_x + dX) >= 0 && (_y + dY) >= 0)
  163. {
  164. setX(_x + dX);
  165. setY(_y + dY);
  166. }
  167. }
  168.  
  169. public double getRadius()
  170. {
  171. return _radius;
  172. }
  173.  
  174. public double setRadius(double x, double y)
  175. {
  176. _radius = Math.sqrt(Math.pow((_x - x),2)+Math.pow((_y - y),2));
  177. return _radius;
  178. }
  179.  
  180. public double getAlpha()
  181. {
  182. return _alpha;
  183. }
  184. */
  185.  
  186. } //class Point
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement