/[gr2lib]/trunk/gr2lib/core/coretypes/implementation/Vector2.cs
ViewVC logotype

Contents of /trunk/gr2lib/core/coretypes/implementation/Vector2.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 186 - (show annotations) (download)
Sat Jul 24 02:23:18 2010 UTC (9 years, 3 months ago) by william
File size: 13607 byte(s)
continue to add xml documentation

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Runtime.InteropServices;
6 using System.ComponentModel;
7 using gr2lib.core.ui.typeeditors;
8 using System.Drawing.Design;
9
10 namespace gr2lib.core.coretypes.implementation
11 {
12 /// <summary>
13 /// IVector2 interface
14 /// </summary>
15 public interface IVector2
16 {
17 /// <summary>
18 /// x
19 /// </summary>
20 float x { get; set; }
21 /// <summary>
22 /// y
23 /// </summary>
24 float y { get; set; }
25 }
26 /// <summary>
27 /// Vector2 class
28 /// </summary>
29 [StructLayout(LayoutKind.Sequential)]
30 [Editor(typeof(UIVectorEditor<Vector2>), typeof(UITypeEditor))]
31 public struct Vector2 : IVector2
32 {
33 #region Implicit Conversion Operators
34 /// <summary>
35 /// Converts string to Vector2
36 /// </summary>
37 /// <param name="vector">the string to convert</param>
38 /// <returns></returns>
39 public static implicit operator Vector2(string vector)
40 {
41 if (vector != null)
42 {
43 float x, y;
44 string[] _matrix = vector.Split(',');
45 x = Convert.ToSingle(_matrix[0].Replace("{", "").Replace("(", "").Replace(")", "").Replace("}", ""));
46 y = Convert.ToSingle(_matrix[1].Replace("{", "").Replace("(", "").Replace(")", "").Replace("}", ""));
47 return new Vector2(x, y);
48 }
49 else
50 {
51 return new Vector2();
52 }
53 }
54 /// <summary>
55 /// converts vector2 to string
56 /// </summary>
57 /// <param name="vector">the vector to convert</param>
58 /// <returns></returns>
59 public static implicit operator string(Vector2 vector)
60 {
61 if (vector != null) { return vector.ToString(); } else { return new Vector2().ToString(); }
62 }
63 #endregion
64 /// <summary>
65 /// Empty
66 /// </summary>
67 public static readonly Vector2 Empty;
68 /// <summary>
69 /// Invalid
70 /// </summary>
71 public static readonly Vector2 Invalid;
72 /// <summary>
73 /// OutOfWorld
74 /// </summary>
75 public static readonly Vector2 OutOfWorld;
76 /// <summary>
77 /// Zero
78 /// </summary>
79 public static readonly Vector2 Zero;
80 /// <summary>
81 /// Origin
82 /// </summary>
83 public static readonly Vector2 Origin;
84 /// <summary>
85 /// UnitX
86 /// </summary>
87 public static readonly Vector2 UnitX;
88 /// <summary>
89 /// UnitY
90 /// </summary>
91 public static readonly Vector2 UnitY;
92 /// <summary>
93 /// UnitZ
94 /// </summary>
95 public static readonly Vector2 UnitZ;
96 private float _x;
97 private float _y;
98
99 #region IVector2 Members
100 /// <summary>
101 /// x
102 /// </summary>
103 public float x { get { return _x; } set { _x = value; } }
104 /// <summary>
105 /// y
106 /// </summary>
107 public float y { get { return _y; } set { _y = value; } }
108 #endregion
109
110 // public Vector2()
111 //{
112 // this.x = 0f;
113 // this.y = 0f;
114 //}
115 /// <summary>
116 /// default contructor: x,y
117 /// </summary>
118 /// <param name="_x"></param>
119 /// <param name="_y"></param>
120 public Vector2(float _x, float _y)
121 {
122 this._x = _x;
123 this._y = _y;
124 }
125 /// <summary>
126 /// operator ==(Vector2 a, Vector2 b)
127 /// </summary>
128 /// <param name="a"></param>
129 /// <param name="b"></param>
130 /// <returns></returns>
131 public static bool operator ==(Vector2 a, Vector2 b)
132 {
133 return ((a.x == b.x) && (a.y == b.y));
134 }
135 /// <summary>
136 /// operator !=(Vector2 a, Vector2 b)
137 /// </summary>
138 /// <param name="a"></param>
139 /// <param name="b"></param>
140 /// <returns></returns>
141 public static bool operator !=(Vector2 a, Vector2 b)
142 {
143 if (a.x == b.x)
144 {
145 return (a.y != b.y);
146 }
147 return true;
148 }
149 /// <summary>
150 /// Test if instances are equal
151 /// </summary>
152 /// <param name="obj"></param>
153 /// <returns></returns>
154 public override bool Equals(object obj)
155 {
156 if ((obj == null) || (obj.GetType() != base.GetType()))
157 {
158 return false;
159 }
160 Vector2 vector = (Vector2)obj;
161 return ((this.x == vector.x) && (this.y == vector.y));
162 }
163 /// <summary>
164 /// Get Hash Code
165 /// </summary>
166 /// <returns></returns>
167 public override int GetHashCode()
168 {
169 return (this.x.GetHashCode() ^ this.y.GetHashCode());
170 }
171 /// <summary>
172 /// Get string representation
173 /// </summary>
174 /// <returns></returns>
175 public override string ToString()
176 {
177 StringBuilder builder = new StringBuilder();
178 builder.Append("(");
179 builder.Append(this.x.ToString("0.000000"));
180 builder.Append(", ");
181 builder.Append(this.y.ToString("0.000000"));
182 builder.Append(")");
183 return builder.ToString();
184 }
185 /// <summary>
186 /// TryParse
187 /// </summary>
188 /// <param name="values"></param>
189 /// <param name="vector2"></param>
190 /// <returns></returns>
191 public static bool TryParse(string values, out Vector2 vector2)
192 {
193 vector2 = new Vector2();
194 if (values == null)
195 {
196 return false;
197 }
198 string[] strArray = values.Replace(" ", "").Replace("f", "").Split(new char[] { ',' });
199 if (strArray.Length != 2)
200 {
201 return false;
202 }
203 if (!float.TryParse(strArray[0], out vector2._x))
204 {
205 return false;
206 }
207 if (!float.TryParse(strArray[1], out vector2._y))
208 {
209 return false;
210 }
211 return true;
212 }
213 /// <summary>
214 /// set: x,y
215 /// </summary>
216 /// <param name="_x"></param>
217 /// <param name="_y"></param>
218 public void Set(float _x, float _y)
219 {
220 this.x = _x;
221 this.y = _y;
222 }
223 /// <summary>
224 /// operator -(Vector2 vec)
225 /// </summary>
226 /// <param name="vec"></param>
227 /// <returns></returns>
228 public static Vector2 operator -(Vector2 vec)
229 {
230 return new Vector2(-vec.x, -vec.y);
231 }
232 /// <summary>
233 /// operator +(Vector2 vec, float scaler)
234 /// </summary>
235 /// <param name="vec"></param>
236 /// <param name="scaler"></param>
237 /// <returns></returns>
238 public static Vector2 operator +(Vector2 vec, float scaler)
239 {
240 return new Vector2(vec.x + scaler, vec.y + scaler);
241 }
242 /// <summary>
243 /// operator -(Vector2 vec, float scaler)
244 /// </summary>
245 /// <param name="vec"></param>
246 /// <param name="scaler"></param>
247 /// <returns></returns>
248 public static Vector2 operator -(Vector2 vec, float scaler)
249 {
250 return new Vector2(vec.x - scaler, vec.y - scaler);
251 }
252 /// <summary>
253 /// operator +(Vector2 a, Vector2 b)
254 /// </summary>
255 /// <param name="a"></param>
256 /// <param name="b"></param>
257 /// <returns></returns>
258 public static Vector2 operator +(Vector2 a, Vector2 b)
259 {
260 return new Vector2(a.x + b.x, a.y + b.y);
261 }
262 /// <summary>
263 /// operator -(Vector2 a, Vector2 b)
264 /// </summary>
265 /// <param name="a"></param>
266 /// <param name="b"></param>
267 /// <returns></returns>
268 public static Vector2 operator -(Vector2 a, Vector2 b)
269 {
270 return new Vector2(a.x - b.x, a.y - b.y);
271 }
272 /// <summary>
273 /// operator *(Vector2 vec, float scaler)
274 /// </summary>
275 /// <param name="vec"></param>
276 /// <param name="scaler"></param>
277 /// <returns></returns>
278 public static Vector2 operator *(Vector2 vec, float scaler)
279 {
280 return new Vector2(vec.x * scaler, vec.y * scaler);
281 }
282 /// <summary>
283 /// operator /(Vector2 vec, float scaler)
284 /// </summary>
285 /// <param name="vec"></param>
286 /// <param name="scaler"></param>
287 /// <returns></returns>
288 public static Vector2 operator /(Vector2 vec, float scaler)
289 {
290 return new Vector2(vec.x / scaler, vec.y / scaler);
291 }
292 /// <summary>
293 /// operator *(Vector2 a, Vector2 b)
294 /// </summary>
295 /// <param name="a"></param>
296 /// <param name="b"></param>
297 /// <returns></returns>
298 public static float operator *(Vector2 a, Vector2 b)
299 {
300 return ((a.x * b.x) + (a.y * b.y));
301 }
302 /// <summary>
303 /// Multiply(Vector2 b)
304 /// </summary>
305 /// <param name="b"></param>
306 /// <returns></returns>
307 public Vector2 Multiply(Vector2 b)
308 {
309 return new Vector2(this.x * b.x, this.y * b.y);
310 }
311 /// <summary>
312 /// operator /(Vector2 a, Vector2 b)
313 /// </summary>
314 /// <param name="a"></param>
315 /// <param name="b"></param>
316 /// <returns></returns>
317 public static Vector2 operator /(Vector2 a, Vector2 b)
318 {
319 return new Vector2(a.x / b.x, a.y / b.y);
320 }
321 /// <summary>
322 /// Vector2 Min(Vector2 a, Vector2 b)
323 /// </summary>
324 /// <param name="a"></param>
325 /// <param name="b"></param>
326 /// <returns></returns>
327 public static Vector2 Min(Vector2 a, Vector2 b)
328 {
329 return new Vector2(Math.Min(a.x, b.x), Math.Min(a.y, b.y));
330 }
331 /// <summary>
332 /// Vector2 Max(Vector2 a, Vector2 b)
333 /// </summary>
334 /// <param name="a"></param>
335 /// <param name="b"></param>
336 /// <returns></returns>
337 public static Vector2 Max(Vector2 a, Vector2 b)
338 {
339 return new Vector2(Math.Max(a.x, b.x), Math.Max(a.y, b.y));
340 }
341 /// <summary>
342 /// Floor(Vector2 v)
343 /// </summary>
344 /// <param name="v"></param>
345 /// <returns></returns>
346 public static Vector2 Floor(Vector2 v)
347 {
348 return new Vector2(MathUtils.Floor(v.x), MathUtils.Floor(v.y));
349 }
350 /// <summary>
351 /// Clamp(Vector2 a, Vector2 clampMin, Vector2 clampMax)
352 /// </summary>
353 /// <param name="a"></param>
354 /// <param name="clampMin"></param>
355 /// <param name="clampMax"></param>
356 /// <returns></returns>
357 public static Vector2 Clamp(Vector2 a, Vector2 clampMin, Vector2 clampMax)
358 {
359 return new Vector2(MathUtils.Clamp(a.x, clampMin.x, clampMax.x), MathUtils.Clamp(a.y, clampMin.y, clampMax.y));
360 }
361 /// <summary>
362 /// Length
363 /// </summary>
364 /// <returns></returns>
365 public float Length()
366 {
367 return (float)Math.Sqrt((double)this.LengthSqr());
368 }
369 /// <summary>
370 /// LengthSqr
371 /// </summary>
372 /// <returns></returns>
373 public float LengthSqr()
374 {
375 return ((this.x * this.x) + (this.y * this.y));
376 }
377 /// <summary>
378 /// Normalize(Vector2 vec, out float length)
379 /// </summary>
380 /// <param name="vec"></param>
381 /// <param name="length"></param>
382 /// <returns></returns>
383 public static Vector2 Normalize(Vector2 vec, out float length)
384 {
385 length = vec.Length();
386 if (Math.Abs(length) < 1E-05f)
387 {
388 return Zero;
389 }
390 float num = 1f / length;
391 return new Vector2(vec.x * num, vec.y * num);
392 }
393 /// <summary>
394 /// Normalize(Vector2 vec)
395 /// </summary>
396 /// <param name="vec"></param>
397 /// <returns></returns>
398 public static Vector2 Normalize(Vector2 vec)
399 {
400 float num;
401 return Normalize(vec, out num);
402 }
403
404 //public float Normalize()
405 //{
406 // float num;
407 // this = Normalize(this, out num);
408 // return num;
409 //}
410
411 static Vector2()
412 {
413 Empty = new Vector2(0f, 0f);
414 Invalid = new Vector2(float.PositiveInfinity, float.PositiveInfinity);
415 OutOfWorld = new Vector2(-20000f, -20000f);
416 Zero = Empty;
417 Origin = Empty;
418 UnitX = new Vector2(1f, 0f);
419 UnitY = new Vector2(0f, 1f);
420 UnitZ = UnitY;
421 }
422 }
423 }

  ViewVC Help
Powered by ViewVC 1.1.22