/[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 83 - (show annotations) (download)
Fri Jul 16 21:26:08 2010 UTC (10 years, 1 month ago) by william
File size: 6241 byte(s)
bulk commit --
** Enable Support for Skeletons & Bones

** A bug has found its way into the code
 -get an unhandled EHAccessViolation
 - I think the problem is in the Textures Class (or one of the sub-classes)

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Runtime.InteropServices;
6
7 namespace gr2lib.core.coretypes.implementation
8 {
9 [StructLayout(LayoutKind.Sequential)]
10 public struct Vector2
11 {
12 public static readonly Vector2 Empty;
13 public static readonly Vector2 Invalid;
14 public static readonly Vector2 OutOfWorld;
15 public static readonly Vector2 Zero;
16 public static readonly Vector2 Origin;
17 public static readonly Vector2 UnitX;
18 public static readonly Vector2 UnitY;
19 public static readonly Vector2 UnitZ;
20 public float x;
21 public float y;
22 public Vector2(float _x, float _y)
23 {
24 this.x = _x;
25 this.y = _y;
26 }
27
28 public static bool operator ==(Vector2 a, Vector2 b)
29 {
30 return ((a.x == b.x) && (a.y == b.y));
31 }
32
33 public static bool operator !=(Vector2 a, Vector2 b)
34 {
35 if (a.x == b.x)
36 {
37 return (a.y != b.y);
38 }
39 return true;
40 }
41
42 public override bool Equals(object obj)
43 {
44 if ((obj == null) || (obj.GetType() != base.GetType()))
45 {
46 return false;
47 }
48 Vector2 vector = (Vector2)obj;
49 return ((this.x == vector.x) && (this.y == vector.y));
50 }
51
52 public override int GetHashCode()
53 {
54 return (this.x.GetHashCode() ^ this.y.GetHashCode());
55 }
56
57 public override string ToString()
58 {
59 StringBuilder builder = new StringBuilder();
60 builder.Append("(");
61 builder.Append(this.x.ToString("0.000000"));
62 builder.Append(", ");
63 builder.Append(this.y.ToString("0.000000"));
64 builder.Append(")");
65 return builder.ToString();
66 }
67
68 public static bool TryParse(string values, out Vector2 vector2)
69 {
70 vector2 = new Vector2();
71 if (values == null)
72 {
73 return false;
74 }
75 string[] strArray = values.Replace(" ", "").Replace("f", "").Split(new char[] { ',' });
76 if (strArray.Length != 2)
77 {
78 return false;
79 }
80 if (!float.TryParse(strArray[0], out vector2.x))
81 {
82 return false;
83 }
84 if (!float.TryParse(strArray[1], out vector2.y))
85 {
86 return false;
87 }
88 return true;
89 }
90
91 public void Set(float _x, float _y)
92 {
93 this.x = _x;
94 this.y = _y;
95 }
96
97 public static Vector2 operator -(Vector2 vec)
98 {
99 return new Vector2(-vec.x, -vec.y);
100 }
101
102 public static Vector2 operator +(Vector2 vec, float scaler)
103 {
104 return new Vector2(vec.x + scaler, vec.y + scaler);
105 }
106
107 public static Vector2 operator -(Vector2 vec, float scaler)
108 {
109 return new Vector2(vec.x - scaler, vec.y - scaler);
110 }
111
112 public static Vector2 operator +(Vector2 a, Vector2 b)
113 {
114 return new Vector2(a.x + b.x, a.y + b.y);
115 }
116
117 public static Vector2 operator -(Vector2 a, Vector2 b)
118 {
119 return new Vector2(a.x - b.x, a.y - b.y);
120 }
121
122 public static Vector2 operator *(Vector2 vec, float scaler)
123 {
124 return new Vector2(vec.x * scaler, vec.y * scaler);
125 }
126
127 public static Vector2 operator /(Vector2 vec, float scaler)
128 {
129 return new Vector2(vec.x / scaler, vec.y / scaler);
130 }
131
132 public static float operator *(Vector2 a, Vector2 b)
133 {
134 return ((a.x * b.x) + (a.y * b.y));
135 }
136
137 public Vector2 Multiply(Vector2 b)
138 {
139 return new Vector2(this.x * b.x, this.y * b.y);
140 }
141
142 public static Vector2 operator /(Vector2 a, Vector2 b)
143 {
144 return new Vector2(a.x / b.x, a.y / b.y);
145 }
146
147 public static Vector2 Min(Vector2 a, Vector2 b)
148 {
149 return new Vector2(Math.Min(a.x, b.x), Math.Min(a.y, b.y));
150 }
151
152 public static Vector2 Max(Vector2 a, Vector2 b)
153 {
154 return new Vector2(Math.Max(a.x, b.x), Math.Max(a.y, b.y));
155 }
156
157 public static Vector2 Floor(Vector2 v)
158 {
159 return new Vector2(MathUtils.Floor(v.x), MathUtils.Floor(v.y));
160 }
161
162 public static Vector2 Clamp(Vector2 a, Vector2 clampMin, Vector2 clampMax)
163 {
164 return new Vector2(MathUtils.Clamp(a.x, clampMin.x, clampMax.x), MathUtils.Clamp(a.y, clampMin.y, clampMax.y));
165 }
166
167 public float Length()
168 {
169 return (float)Math.Sqrt((double)this.LengthSqr());
170 }
171
172 public float LengthSqr()
173 {
174 return ((this.x * this.x) + (this.y * this.y));
175 }
176
177 public static Vector2 Normalize(Vector2 vec, out float length)
178 {
179 length = vec.Length();
180 if (Math.Abs(length) < 1E-05f)
181 {
182 return Zero;
183 }
184 float num = 1f / length;
185 return new Vector2(vec.x * num, vec.y * num);
186 }
187
188 public static Vector2 Normalize(Vector2 vec)
189 {
190 float num;
191 return Normalize(vec, out num);
192 }
193
194 public float Normalize()
195 {
196 float num;
197 this = Normalize(this, out num);
198 return num;
199 }
200
201 static Vector2()
202 {
203 Empty = new Vector2(0f, 0f);
204 Invalid = new Vector2(float.PositiveInfinity, float.PositiveInfinity);
205 OutOfWorld = new Vector2(-20000f, -20000f);
206 Zero = Empty;
207 Origin = Empty;
208 UnitX = new Vector2(1f, 0f);
209 UnitY = new Vector2(0f, 1f);
210 UnitZ = UnitY;
211 }
212 }
213 }

  ViewVC Help
Powered by ViewVC 1.1.22