You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Point.java 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
  3. Use and copying of this software and preparation of derivative works based
  4. upon this software are permitted. Any distribution of this software or
  5. derivative works must comply with all applicable United States export control
  6. laws.
  7. This software is made available AS IS, and Xerox Corporation makes no warranty
  8. about the software, its performance or its conformity to any specification.
  9. */
  10. package introduction;
  11. public class Point {
  12. protected double x = 0;
  13. protected double y = 0;
  14. protected double theta = 0;
  15. protected double rho = 0;
  16. protected boolean polar = true;
  17. protected boolean rectangular = true;
  18. public double getX(){
  19. makeRectangular();
  20. return x;
  21. }
  22. public double getY(){
  23. makeRectangular();
  24. return y;
  25. }
  26. public double getTheta(){
  27. makePolar();
  28. return theta;
  29. }
  30. public double getRho(){
  31. makePolar();
  32. return rho;
  33. }
  34. public void setRectangular(double newX, double newY){
  35. x = newX;
  36. y = newY;
  37. rectangular = true;
  38. polar = false;
  39. }
  40. public void setPolar(double newTheta, double newRho){
  41. theta = newTheta;
  42. rho = newRho;
  43. rectangular = false;
  44. polar = true;
  45. }
  46. public void rotate(double angle){
  47. setPolar(theta + angle, rho);
  48. }
  49. public void offset(double deltaX, double deltaY){
  50. setRectangular(x + deltaX, y + deltaY);
  51. }
  52. protected void makePolar(){
  53. if (!polar){
  54. theta = Math.atan2(y,x);
  55. rho = y / Math.sin(theta);
  56. polar = true;
  57. }
  58. }
  59. protected void makeRectangular(){
  60. if (!rectangular) {
  61. y = rho * Math.sin(theta);
  62. x = rho * Math.cos(theta);
  63. rectangular = true;
  64. }
  65. }
  66. public String toString(){
  67. return "(" + getX() + ", " + getY() + ")["
  68. + getTheta() + " : " + getRho() + "]";
  69. }
  70. public static void main(String[] args){
  71. Point p1 = new Point();
  72. System.out.println("p1 =" + p1);
  73. p1.setRectangular(5,2);
  74. System.out.println("p1 =" + p1);
  75. p1.setPolar( Math.PI / 4.0 , 1.0);
  76. System.out.println("p1 =" + p1);
  77. p1.setPolar( 0.3805 , 5.385);
  78. System.out.println("p1 =" + p1);
  79. }
  80. }