View Javadoc

1   /*
2    * Copyright (C) 2005 Erland Isaksson (erland_i@hotmail.com)
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU General Public License
6    * as published by the Free Software Foundation; either version 2
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   */
19  package erland.game.tileadventure;
20  
21  public class Vector3D implements Cloneable {
22      private float x;
23      private float y;
24      private float z;
25  
26      public Vector3D() {}
27  
28      public Vector3D(float x, float y, float z) {
29          setVector(x,y,z);
30      }
31  
32      public Vector3D(float x, float y, float z, float length) {
33          setVector(x,y,z);
34          setLength(length);
35      }
36      public void setVector(Vector3D p) {
37          this.x = p.x;
38          this.y = p.y;
39          this.z = p.z;
40      }
41      public void setVector(float x, float y, float z) {
42          this.x = x;
43          this.y = y;
44          this.z = z;
45      }
46  
47      public float getX() {
48          return x;
49      }
50  
51      public float getY() {
52          return y;
53      }
54  
55      public float getZ() {
56          return z;
57      }
58      public void setLength(float length) {
59          double multiplier = length/getLength();
60          this.x *= multiplier;
61          this.y *= multiplier;
62          this.z *= multiplier;
63      }
64      public float getLength() {
65          return (float) Math.sqrt(x*x+y*y+z*z);
66      }
67  
68      public Object clone() {
69          try {
70              return super.clone();
71          } catch (CloneNotSupportedException e) {
72              return null;
73          }
74      }
75  }