aboutsummaryrefslogtreecommitdiffstats
path: root/docs/examples/introduction/Point.java
blob: 4de01359bf355bc317491d94249ea8bc70e5a322 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
  Copyright (c) Xerox Corporation 1998-2002.  All rights reserved.

  Use and copying of this software and preparation of derivative works based
  upon this software are permitted.  Any distribution of this software or
  derivative works must comply with all applicable United States export control
  laws.

  This software is made available AS IS, and Xerox Corporation makes no warranty
  about the software, its performance or its conformity to any specification.
*/

package introduction;

public class Point {

   protected double x = 0;
   protected double y = 0;
   protected double theta = 0;
   protected double rho = 0;

   protected boolean polar = true;
   protected boolean rectangular = true;

   public double getX(){
      makeRectangular();
      return x;
   }

   public double getY(){
      makeRectangular();
      return y;
   }

   public double getTheta(){
      makePolar();
      return theta;
   }

   public double getRho(){
      makePolar();
      return rho;
   }

   public void setRectangular(double newX, double newY){
      x = newX;
      y = newY;
      rectangular = true;
      polar = false;
   }

   public void setPolar(double newTheta, double newRho){
      theta = newTheta;
      rho = newRho;
      rectangular = false;
      polar = true;
   }

   public void rotate(double angle){
      setPolar(theta + angle, rho);
   }

   public void offset(double deltaX, double deltaY){
      setRectangular(x + deltaX, y + deltaY);
   }

   protected void makePolar(){
      if (!polar){
	 theta = Math.atan2(y,x);
	 rho = y / Math.sin(theta);
	 polar = true;
      }
   }

   protected void makeRectangular(){
      if (!rectangular) {
		 y = rho * Math.sin(theta);
		 x = rho * Math.cos(theta);
		 rectangular = true;
      }
   }

   public String toString(){
      return "(" + getX() + ", " + getY() + ")[" 
	 + getTheta() + " : " + getRho() + "]";
   }

   public static void main(String[] args){
      Point p1 = new Point();
      System.out.println("p1 =" + p1);
      p1.setRectangular(5,2);
      System.out.println("p1 =" + p1);
      p1.setPolar( Math.PI / 4.0 , 1.0);
      System.out.println("p1 =" + p1);
      p1.setPolar( 0.3805 , 5.385);
      System.out.println("p1 =" + p1);
   }
}