OSDN Git Service

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