summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/Point2.java
blob: 3b471b76908d36ca0a651d6f596fbdd1746a88fc (plain) (blame)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package NET.worlds.scape;

import java.io.IOException;

public class Point2 implements Persister {
   public float x;
   public float y;
   private static Object classCookie = new Object();

   public Point2() {
   }

   public Point2(float _x, float _y) {
      this.set(_x, _y);
   }

   public Point2(Point2 p) {
      this.x = p.x;
      this.y = p.y;
   }

   public Point2 copy(Point2 b) {
      this.x = b.x;
      this.y = b.y;
      return this;
   }

   public void set(float _x, float _y) {
      this.x = _x;
      this.y = _y;
   }

   public float length() {
      return (float)Math.sqrt(this.x * this.x + this.y * this.y);
   }

   public Point2 normalize() {
      float len = this.length();
      if (len > 0.0F) {
         this.dividedBy(len);
      }

      return this;
   }

   public Point2 negate() {
      this.x = -this.x;
      this.y = -this.y;
      return this;
   }

   public Point2 plus(Point2 p) {
      this.x = this.x + p.x;
      this.y = this.y + p.y;
      return this;
   }

   public Point2 plus(float v) {
      this.x += v;
      this.y += v;
      return this;
   }

   public Point2 minus(float v) {
      return this.plus(-v);
   }

   public Point2 minus(Point2 p) {
      this.x = this.x - p.x;
      this.y = this.y - p.y;
      return this;
   }

   public Point2 times(float v) {
      this.x *= v;
      this.y *= v;
      return this;
   }

   public Point2 times(Point2 p) {
      this.x = this.x * p.x;
      this.y = this.y * p.y;
      return this;
   }

   public Point2 dividedBy(float v) {
      this.x /= v;
      this.y /= v;
      return this;
   }

   public Point2 dividedBy(Point2 p) {
      this.x = this.x / p.x;
      this.y = this.y / p.y;
      return this;
   }

   public float dot(Point2 p) {
      return p.x * this.x + p.y * this.y;
   }

   @Override
   public String toString() {
      return this.x + "," + this.y;
   }

   @Override
   public void saveState(Saver s) throws IOException {
      s.saveVersion(0, classCookie);
      s.saveFloat(this.x);
      s.saveFloat(this.y);
   }

   @Override
   public void restoreState(Restorer r) throws IOException, TooNewException {
      switch (r.restoreVersion(classCookie)) {
         case 0:
            this.x = r.restoreFloat();
            this.y = r.restoreFloat();
            return;
         default:
            throw new TooNewException();
      }
   }

   @Override
   public void postRestore(int version) {
   }
}