1 package com.insanityengine.ghia.m3;
2
3 /***
4 *
5 * <P>
6 * I wrote this class back in 1999! It is still pretty handy.
7 * Here are the original comments:
8 * </P>
9 *
10 * <PRE>
11 * since java.awt.Point is integer based...
12 * also add some handy routines:
13 * - rotate around a given point at given angle
14 * - find the distance from another point
15 * - calculate the angle from another point
16 * </PRE>
17 **/
18
19 import java.awt.*;
20
21 public class Pt2 {
22
23
24 /***
25 *
26 * Constructor
27 *
28 */
29 public Pt2() {
30 x = y = 0;
31 }
32
33 /***
34 *
35 * Constructor
36 *
37 * @param x coordinate
38 * @param y coordinate
39 *
40 */
41 public Pt2( double x, double y ) {
42 setX( x );
43 setY( y );
44 }
45
46 /***
47 *
48 * Constructor
49 *
50 * @param point
51 *
52 */
53 public Pt2( Pt2 point ) {
54 set( point );
55 }
56
57 /***
58 *
59 * Constructor
60 *
61 * @param point
62 *
63 */
64 public Pt2( Point point ) {
65 set( point );
66 }
67
68 /***
69 *
70 * Equality operator
71 *
72 * @param x coordinate
73 * @param y coordinate
74 *
75 * @return true if equal, else false
76 *
77 */
78 public boolean equals( double x, double y ) {
79 return ( x == this.x && y == this.y );
80 }
81
82 /***
83 *
84 * Equality operator
85 *
86 * @param point to compare
87 *
88 * @return true if equal, else false
89 *
90 */
91 public boolean equals( Pt2 point ) {
92 return equals( point.getX(), point.getY() );
93 }
94
95 /***
96 *
97 * Equality operator
98 *
99 * @param point to compare
100 *
101 * @return true if equal, else false
102 *
103 */
104 public boolean equals( Point point ) {
105 return equals( point.getX(), point.getY() );
106 };
107
108 /***
109 *
110 * "Equality" operator
111 *
112 * @param x coordinate
113 * @param y coordinate
114 * @param errFactor to use
115 *
116 * @return true if equal, else false
117 *
118 */
119 public boolean equals( double x, double y, double errFactor ) {
120 return (
121 Math.abs( this.x - x ) <= errFactor
122 ) && (
123 Math.abs( this.y - y ) <= errFactor
124 );
125 }
126
127 /***
128 *
129 * "Equality" operator
130 *
131 * @param point to compare
132 * @param errFactor to use
133 *
134 * @return true if equal, else false
135 *
136 */
137 public boolean equals( Pt2 point, double errFactor ) {
138 return equals( point.getX(), point.getY(), errFactor );
139 }
140
141 /***
142 *
143 * "Equality" operator
144 *
145 * @param point to compare
146 * @param errFactor to use
147 *
148 * @return true if equal, else false
149 *
150 */
151 public boolean equals( Point point, double errFactor ) {
152 return equals( point.getX(), point.getY(), errFactor );
153 }
154
155 /***
156 *
157 * create Point equivalent
158 *
159 * @return Point equivalent
160 *
161 */
162 public Point toPoint() {
163 return new Point(
164 ( int ) x
165 ,
166 ( int ) y
167 );
168 }
169
170 /***
171 *
172 * create String equivalent
173 *
174 * @return String equivalent
175 *
176 */
177 public String toString() {
178 return "[Pt2, x=" + x + ", y=" + y + "]";
179 }
180
181 /***
182 *
183 * Translate this point
184 *
185 * @param tx value
186 * @param ty value
187 *
188 */
189 public void translate( double tx, double ty ) {
190 x += tx;
191 y += ty;
192 }
193
194 /***
195 *
196 * Rotate this point around another by a certain angle
197 *
198 * @param x coordinate
199 * @param y coordinate
200 * @param angle to rotate by
201 *
202 */
203 public void rotate( double x, double y, double angle ) {
204 double dist=distance(x,y);
205 this.x=x+dist*Math.cos(angle);
206 this.y=y+dist*Math.sin(angle);
207 }
208
209 /***
210 *
211 * Get the square of the distance to a point
212 *
213 * @param x coordinate
214 * @param y coordinate
215 *
216 * @return square of the distance
217 *
218 */
219 public double dist2( double x, double y ) {
220 return (
221 ( this.x - x ) * ( this.x - x )
222 +
223 ( this.y - y ) * ( this.y - y )
224 );
225 }
226
227 /***
228 *
229 * Get the distance to a point
230 *
231 * @param x coordinate
232 * @param y coordinate
233 *
234 * @return distance
235 *
236 */
237 public double distance( double x, double y ) {
238 return Math.sqrt( dist2( x, y ) );
239 }
240
241 public String uh( String s ) {
242 int idx = s.indexOf( "." ) + 4;
243 if ( idx >= s.length() ) idx = s.length();
244 return s.substring( 0, idx );
245 }
246
247 public String oh( double d ) {
248 return uh( "" + d );
249 }
250
251 /***
252 *
253 * Get the angle to a point
254 * Revised thx to http://www.geocities.com/SiliconValley/2151/math2d.html
255 *
256 * @param x coordinate
257 * @param y coordinate
258 *
259 * @return the angle
260 *
261 */
262 public double getAngle( double x, double y ) {
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307 /***/
308 double angle = 0;
309
310 double x0 = this.x - x;
311 double y0 = this.y - y;
312 double dist = Math.sqrt( x0 * x0 + y0 * y0 );
313
314 double bobo = 0;
315 if ( 0 != dist ) {
316 bobo = x0 / dist;
317 angle = Math.acos( x0 / dist );
318 if ( y0 < 0 ) angle = 44.0 / 7.0 - angle;
319 }
320 System.out.println( "" + " x0 = " + x0 + " y0 = " + y0 + " dist = " + dist + " bobo = " + bobo + " angle = " + angle );
321 return angle;
322 /***/
323 }
324
325 /***
326 *
327 * Get the angle to a point
328 *
329 * @param x coordinate
330 * @param y coordinate
331 *
332 * @return the angle
333 *
334 */
335 public double getAngle( Pt2 origin ) {
336 return getAngle( origin.x, origin.y );
337 }
338
339 /***
340 *
341 * Set the value
342 *
343 * @param newValue to use
344 *
345 */
346 public double getX() { return x; }
347
348 /***
349 *
350 * Set the value
351 *
352 * @param newValue to use
353 *
354 */
355 public void setX( double newValue ) { x = newValue; }
356
357 /***
358 *
359 * Set the value
360 *
361 * @param newValue to use
362 *
363 */
364 public double getY() { return y; }
365
366 /***
367 *
368 * Set the value
369 *
370 * @param newValue to use
371 *
372 */
373 public void setY( double newValue ) { y = newValue; }
374
375 /***
376 *
377 * Set to the point
378 *
379 * @param point to set to
380 *
381 * @return this
382 *
383 */
384 public Pt2 set( Pt2 point ) {
385 setX( point.getX() );
386 setY( point.getY() );
387 return this;
388 }
389
390 /***
391 *
392 * Set to the point
393 *
394 * @param point to set to
395 *
396 * @return this
397 *
398 */
399 public Pt2 set( Point point ) {
400 setX( point.getX() );
401 setY( point.getY() );
402 return this;
403 }
404
405 /***
406 *
407 * Set to the point
408 *
409 * @param x coordinate
410 * @param y coordinate
411 *
412 * @return this
413 *
414 */
415 public Pt2 set( double x, double y ) {
416 setX( x );
417 setY( y );
418 return this;
419 }
420
421 /***
422 *
423 * Convert to a normal vector
424 *
425 */
426 public void normalize() {
427 float f = ( float ) Math.sqrt( x * x + y * y );
428 if ( 0 != f ) {
429 x /= f;
430 y /= f;
431 }
432 }
433
434
435 private double x, y;
436 };