OSDN Git Service

85efdb420aff5a1196c3d0383041a870ce559dc2
[mikumikustudio/libgdx-mikumikustudio.git] / gdx / src / com / badlogic / gdx / math / Vector2.java
1 /*******************************************************************************\r
2  * Copyright 2011 See AUTHORS file.\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  * \r
8  *   http://www.apache.org/licenses/LICENSE-2.0\r
9  * \r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  ******************************************************************************/\r
16 package com.badlogic.gdx.math;\r
17 \r
18 import java.io.Serializable;\r
19 \r
20 \r
21 /**\r
22  * Encapsulates a 2D vector. Allows chaining methods by returning a reference to itself\r
23  * @author badlogicgames@gmail.com\r
24  * \r
25  */\r
26 public class Vector2 implements Serializable {  \r
27         private static final long serialVersionUID = 913902788239530931L;\r
28 \r
29         /** static temporary vector **/\r
30         private final static Vector2 tmp = new Vector2();\r
31 \r
32         /** the x-component of this vector **/\r
33         public float x;\r
34         /** the y-component of this vector **/\r
35         public float y;\r
36 \r
37         /**\r
38          * Constructs a new vector at (0,0)\r
39          */\r
40         public Vector2 () {\r
41 \r
42         }\r
43 \r
44         /**\r
45          * Constructs a vector with the given components\r
46          * @param x The x-component\r
47          * @param y The y-component\r
48          */\r
49         public Vector2 (float x, float y) {\r
50                 this.x = x;\r
51                 this.y = y;\r
52         }\r
53 \r
54         /**\r
55          * Constructs a vector from the given vector\r
56          * @param v The vector\r
57          */\r
58         public Vector2 (Vector2 v) {\r
59                 set(v);\r
60         }\r
61 \r
62         /**\r
63          * @return a copy of this vector\r
64          */\r
65         public Vector2 cpy () {\r
66                 return new Vector2(this);\r
67         }\r
68 \r
69         /**\r
70          * @return The euclidian length\r
71          */\r
72         public float len () {\r
73                 return (float)Math.sqrt(x * x + y * y);\r
74         }\r
75 \r
76         /**\r
77          * @return The squared euclidian length\r
78          */\r
79         public float len2 () {\r
80                 return x * x + y * y;\r
81         }\r
82 \r
83         /**\r
84          * Sets this vector from the given vector\r
85          * @param v The vector\r
86          * @return This vector for chaining\r
87          */\r
88         public Vector2 set (Vector2 v) {\r
89                 x = v.x;\r
90                 y = v.y;\r
91                 return this;\r
92         }\r
93 \r
94         /**\r
95          * Sets the components of this vector\r
96          * @param x The x-component\r
97          * @param y The y-component\r
98          * @return This vector for chaining\r
99          */\r
100         public Vector2 set (float x, float y) {\r
101                 this.x = x;\r
102                 this.y = y;\r
103                 return this;\r
104         }\r
105 \r
106         /**\r
107          * Substracts the given vector from this vector.\r
108          * @param v The vector\r
109          * @return This vector for chaining\r
110          */\r
111         public Vector2 sub (Vector2 v) {\r
112                 x -= v.x;\r
113                 y -= v.y;\r
114                 return this;\r
115         }\r
116 \r
117         /**\r
118          * Normalizes this vector\r
119          * @return This vector for chaining\r
120          */\r
121         public Vector2 nor () {\r
122                 float len = len();\r
123                 if (len != 0) {\r
124                         x /= len;\r
125                         y /= len;\r
126                 }\r
127                 return this;\r
128         }\r
129 \r
130         /**\r
131          * Adds the given vector to this vector\r
132          * @param v The vector\r
133          * @return This vector for chaining\r
134          */\r
135         public Vector2 add (Vector2 v) {\r
136                 x += v.x;\r
137                 y += v.y;\r
138                 return this;\r
139         }\r
140 \r
141         /**\r
142          * Adds the given components to this vector\r
143          * @param x The x-component\r
144          * @param y The y-component\r
145          * @return This vector for chaining\r
146          */\r
147         public Vector2 add (float x, float y) {\r
148                 this.x += x;\r
149                 this.y += y;\r
150                 return this;\r
151         }\r
152 \r
153         /**\r
154          * @param v The other vector\r
155          * @return The dot product between this and the other vector\r
156          */\r
157         public float dot (Vector2 v) {\r
158                 return x * v.x + y * v.y;\r
159         }\r
160 \r
161         /**\r
162          * Multiplies this vector by a scalar\r
163          * @param scalar The scalar\r
164          * @return This vector for chaining\r
165          */\r
166         public Vector2 mul (float scalar) {\r
167                 x *= scalar;\r
168                 y *= scalar;\r
169                 return this;\r
170         }\r
171 \r
172         /**\r
173          * @param v The other vector\r
174          * @return the distance between this and the other vector\r
175          */\r
176         public float dst (Vector2 v) {\r
177                 final float x_d = v.x - x;\r
178                 final float y_d = v.y - y;\r
179                 return (float)Math.sqrt(x_d * x_d + y_d * y_d);\r
180         }\r
181 \r
182         /**\r
183          * @param x The x-component of the other vector\r
184          * @param y The y-component of the other vector\r
185          * @return the distance between this and the other vector\r
186          */\r
187         public float dst (float x, float y) {\r
188                 final float x_d = x - this.x;\r
189                 final float y_d = y - this.y;\r
190                 return (float)Math.sqrt(x_d * x_d + y_d * y_d);\r
191         }\r
192 \r
193         /**\r
194          * @param v The other vector\r
195          * @return the squared distance between this and the other vector\r
196          */\r
197         public float dst2 (Vector2 v) {\r
198                 final float x_d = v.x - x;\r
199                 final float y_d = v.y - y;\r
200                 return x_d * x_d + y_d * y_d;\r
201         }\r
202 \r
203         public String toString () {\r
204                 return "[" + x + ":" + y + "]";\r
205         }\r
206 \r
207         /**\r
208          * Substracts the other vector from this vector.\r
209          * @param x The x-component of the other vector\r
210          * @param y The y-component of the other vector\r
211          * @return This vector for chaining\r
212          */\r
213         public Vector2 sub (float x, float y) {\r
214                 this.x -= x;\r
215                 this.y -= y;\r
216                 return this;\r
217         }\r
218 \r
219         /**\r
220          * @return a temporary copy of this vector. Use with care as this is backed by a single static Vector2 instance. v1.tmp().add(\r
221          *         v2.tmp() ) will not work!\r
222          */\r
223         public Vector2 tmp () {\r
224                 return tmp.set(this);\r
225         }\r
226 \r
227         /**\r
228          * Multiplies this vector by the given matrix\r
229          * @param mat the matrix\r
230          * @return this vector\r
231          */\r
232         public Vector2 mul (Matrix3 mat) {\r
233                 float x = this.x * mat.vals[0] + this.y * mat.vals[3] + mat.vals[6];\r
234                 float y = this.x * mat.vals[1] + this.y * mat.vals[4] + mat.vals[7];\r
235                 this.x = x;\r
236                 this.y = y;\r
237                 return this;\r
238         }\r
239         \r
240         /**\r
241          * Calculates the 2D cross product between this\r
242          * and the given vector.\r
243          * @param v the other vector\r
244          * @return the cross product\r
245          */\r
246         public float crs(Vector2 v) {           \r
247                 return this.x * v.y - this.y * v.x;\r
248         }\r
249         \r
250         /**\r
251          * Calculates the 2D cross product between this\r
252          * and the given vector.\r
253          * @param x the x-coordinate of the other vector\r
254          * @param y the y-coordinate of the other vector\r
255          * @return the cross product\r
256          */\r
257         public float crs(float x, float y) {\r
258                  return this.x * y - this.y * x;\r
259         }\r
260         \r
261         /**\r
262          * @return the angle in degrees of this vector (point) relative to the x-axis. Angles are counter-clockwise and between 0 and 360.\r
263          */\r
264         public float angle() {\r
265       float angle = (float)Math.atan2(y, x) * MathUtils.radiansToDegrees;\r
266       if(angle < 0)\r
267           angle += 360;\r
268       return angle;\r
269   }\r
270         \r
271         /**\r
272          * Rotates the Vector2 by the given angle, counter-clockwise.\r
273          * @param angle the angle in degrees\r
274          * @return the \r
275          */\r
276    public Vector2 rotate(float angle) {\r
277       float rad = angle * MathUtils.degreesToRadians;\r
278       float cos = (float)Math.cos(rad);\r
279       float sin = (float)Math.sin(rad);\r
280       \r
281       float newX = this.x * cos - this.y * sin;\r
282       float newY = this.x * sin + this.y * cos;\r
283       \r
284       this.x = newX;\r
285       this.y = newY;\r
286       \r
287       return this;\r
288   }\r
289 }\r