1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.vectomatic.common.model.geometry;
19
20 import com.google.gwt.user.client.rpc.IsSerializable;
21
22
23
24
25 public class Point implements IsSerializable {
26 public static final Point UNIT = new Point(1f, 1f);
27 public static final Point ZERO = new Point();
28 public float x;
29 public float y;
30
31 public Point() {
32 }
33
34 public Point(Point p) {
35 this.x = p.x;
36 this.y = p.y;
37 }
38
39 public Point(float x, float y) {
40 this.x = x;
41 this.y = y;
42 }
43
44 @Override
45 public boolean equals(Object o) {
46 if (o instanceof Point) {
47 Point p = (Point)o;
48 return x == p.x && y == p.y;
49 }
50 return false;
51 }
52
53 @Override
54 public int hashCode() {
55
56 return (int)x + 37 * (int)y;
57 }
58
59 public Point copyTo(Point dest) {
60 dest.x = this.x;
61 dest.y = this.y;
62 return dest;
63 }
64
65 public Point add(Point p) {
66 return add(p, this);
67 }
68
69 public Point add(Point p, Point dest) {
70 dest.x = x + p.x;
71 dest.y = y + p.y;
72 return dest;
73 }
74
75 public Point subtract(Point p) {
76 return subtract(p, this);
77 }
78
79 public Point subtract(Point p, Point dest) {
80 dest.x = x - p.x;
81 dest.y = y - p.y;
82 return dest;
83 }
84
85 public Point divide(Point p) {
86 return divide(p, this);
87 }
88
89 public Point divide(Point p, Point dest) {
90 dest.x = x / p.x;
91 dest.y = y / p.y;
92 return dest;
93 }
94
95 public Point multiply(float f) {
96 return multiply(f, this);
97 }
98
99 public Point multiply(float f, Point dest) {
100 dest.x = f * x;
101 dest.y = f * y;
102 return dest;
103 }
104
105 public Point negate() {
106 return negate(this);
107 }
108
109 public Point negate(Point dest) {
110 dest.x = -x;
111 dest.y = -y;
112 return dest;
113 }
114
115 public float squaredLength() {
116 return x * x + y * y;
117 }
118
119 public float length() {
120 return (float)Math.sqrt(squaredLength());
121 }
122
123 public Point transform(TransformMatrix t) {
124 return transform(t, this);
125 }
126
127 public Point transform(TransformMatrix t, Point dest) {
128 float px = t.m11 * x + t.m12 * y + t.m13;
129 float py = t.m21 * x + t.m22 * y + t.m23;
130 dest.x = px;
131 dest.y = py;
132 return dest;
133 }
134
135 public Point swap(Point p) {
136 float px = p.x;
137 float py = p.y;
138 p.x = x;
139 p.y = y;
140 x = px;
141 y = py;
142 return this;
143 }
144
145 @Override
146 public String toString() {
147 return "(" + x + ", " + y + ")";
148 }
149
150 public Point parseString(String str) {
151 x = Float.parseFloat(str.substring(1, str.indexOf(',')));
152 y = Float.parseFloat(str.substring(2 + str.indexOf(','), str.length() - 1));
153 return this;
154 }
155
156 public float distance(Point p) {
157
158 return (float)Math.sqrt(squareDistance(p));
159 }
160 public float squareDistance(Point p) {
161
162 return (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y);
163 }
164 public Point symetricPoint(Point p) {
165
166 return symetricPoint(p, this);
167 }
168 public Point symetricPoint(Point p, Point dest) {
169
170 dest.x = 2 * p.x - x;
171 dest.y = 2 * p.y - y;
172 return dest;
173 }
174
175 public float dotProduct(Point p) {
176
177 return x * p.x + y * p.y;
178 }
179
180 }