source: java/client_3D/src/pl/vorg/mowa/core/graphics/Vec2.java @ 66

Last change on this file since 66 was 66, checked in by Maciej Komosinski, 13 years ago

set 'eol-style' to 'native'

  • Property svn:eol-style set to native
File size: 1.6 KB
Line 
1package pl.vorg.mowa.core.graphics;
2
3/**
4 * A vector in 2d space.
5 *
6 * @author vorg
7 */
8public class Vec2 {
9        private float[] v = {0,0};
10       
11        public Vec2() {
12               
13        }
14       
15        public Vec2(float x, float y) {
16                v[0] = x;
17                v[1] = y;
18        }
19       
20        public Vec2(Vec2 vec) {
21                float[] w = vec.getVector();
22                v[0] = w[0];
23                v[1] = w[1];
24        }
25       
26        public Vec2(float[] vec) {
27                v[0] = vec[0];
28                v[1] = vec[1];
29        }
30       
31        @Override
32        public boolean equals(java.lang.Object obj) {
33                float[] w = ((Vec2)obj).getVector();
34                return (v[0] == w[0]) && (v[1] == w[1]);
35        }
36       
37        public float getX() {
38                return v[0];
39        }
40       
41        public float getY() {
42                return v[1];
43        }
44       
45        public float getS() {
46                return v[0];
47        }
48       
49        public float getT() {
50                return v[1];
51        }
52       
53        public void setX(float x) {
54                v[0] = x;
55        }
56       
57        public void setY(float y) {
58                v[1] = y;
59        }
60       
61        public void setS(float s) {
62                v[0] = s;
63        }
64       
65        public void setT(float t) {
66                v[1] = t;
67        }
68       
69        public float[] getVector() {
70                return v;
71        }
72       
73        public void normalize() {
74                float length = length();
75                if (length > 0) {
76                        v[0] /= length;
77                        v[1] /= length;
78                }
79        }
80       
81        public float length() {
82                return (float)Math.sqrt(v[0]*v[0]+v[1]*v[1]);
83        }
84       
85        public float dot(Vec2 a) {
86                float[] va = a.getVector();
87                return v[0]*va[0] + v[1]*va[1];
88        }
89       
90        public float angle(Vec2 a) {
91                float len = length();
92                float alen = a.length();
93                if ((len == 0) || (alen == 0)) {
94                        return 0;
95                }
96                return (float)Math.acos(this.dot(a)/(len*alen));
97        }
98       
99        public static float angle(Vec2 a, Vec2 b) {
100                return a.angle(b);
101        }
102       
103        public static float angleTan(Vec2 a, Vec2 b) {
104                double angle = Math.atan2(a.getY(),a.getX()) - Math.atan2(b.getY(),b.getX());
105                return (float)angle;
106        }
107}
Note: See TracBrowser for help on using the repository browser.