source: 2013/30/MiskaK/MW2(My Warfare 2)/Paranneltu Jypeli/Physics2DDotNet/ALVector2D.cs @ 4507

Revision 4507, 13.0 KB checked in by anlakane, 6 years ago (diff)

Talletus.

Line 
1#region MIT License
2/*
3 * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22#endregion
23
24
25
26
27#if UseDouble
28using Scalar = System.Double;
29#else
30using Scalar = System.Single;
31#endif
32using System;
33using System.Runtime.InteropServices;
34using AdvanceMath;
35using AdvanceMath.Design;
36namespace Physics2DDotNet
37{
38    /// <summary>
39    /// Class Used to store a Linear Value along with an Angular Value. Like Position and Orientation.
40    /// </summary>
41    [StructLayout(LayoutKind.Sequential, Size = ALVector2D.Size)]
42#if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT && !WINDOWS_PHONE
43    [Serializable]
44    [System.ComponentModel.TypeConverter(typeof(AdvTypeConverter<ALVector2D>))]
45#endif
46    [AdvBrowsableOrder("Angular,Linear")]
47    public struct ALVector2D : IEquatable<ALVector2D>
48    {
49        public static ALVector2D Parse(string text)
50        {
51            string[] vals = text.Trim(' ', '(', '[', '<', ')', ']', '>').Split(new char[] { ',' });
52            if (vals.Length != 2)
53            {
54                throw new FormatException(string.Format("Cannot parse the text '{0}' because it does not have 2 parts separated by commas in the form (x,y) with optional parenthesis.", text));
55            }
56            else
57            {
58                try
59                {
60                    ALVector2D returnvalue;
61                    returnvalue.Angular = Scalar.Parse(vals[0]);
62                    returnvalue.Linear = Vector2D.Parse(vals[1]);
63                    return returnvalue;
64                }
65                catch (Exception ex)
66                {
67                    throw new FormatException("The parts of the vectors must be decimal numbers", ex);
68                }
69            }
70        }
71
72        public const int Size = sizeof(Scalar) + Vector2D.Size;
73        /// <summary>
74        /// ALVector2D(0,Vector2D.Zero)
75        /// </summary>
76        public static readonly ALVector2D Zero = new ALVector2D(0, Vector2D.Zero);
77        /// <summary>
78        /// This is the Angular value of this ALVector2D.
79        /// </summary>
80        /// <remarks>Example: Angular would be Orientation and Linear would be Location Completly describing a Position.</remarks>
81        [AdvBrowsable]
82        public Scalar Angular;
83        /// <summary>
84        /// This is the Linear value of this ALVector2D.
85        /// </summary>
86        /// <remarks>Example: Angular would be Orientation and Linear would be Location Completly describing a Position.</remarks>
87        [AdvBrowsable]
88        public Vector2D Linear;
89
90        /// <summary>
91        /// Creates a new ALVector2D instance on the stack.
92        /// </summary>
93        /// <param name="Angular">The Angular value.</param>
94        /// <param name="Linear">The Linear value.</param>
95        [InstanceConstructor("Angular,Linear")]
96        public ALVector2D(Scalar angular, Vector2D linear)
97        {
98            this.Angular = angular;
99            this.Linear = linear;
100        }
101        public ALVector2D(Scalar angular, Scalar x, Scalar y)
102        {
103            this.Angular = angular;
104            this.Linear = new Vector2D(x, y);
105        }
106
107        /// <summary>
108        /// The Linear.X component
109        /// </summary>
110        public Scalar X
111        {
112            get { return Linear.X; }
113            set { Linear.X = value; }
114        }
115        /// <summary>
116        /// The Linear.Y component
117        /// </summary>
118        public Scalar Y
119        {
120            get { return Linear.Y; }
121            set { Linear.Y = value; }
122        }
123
124
125        public static ALVector2D Add(ALVector2D left, ALVector2D right)
126        {
127            ALVector2D result;
128            result.Angular = left.Angular + right.Angular;
129            result.Linear.X = left.Linear.X + right.Linear.X;
130            result.Linear.Y = left.Linear.Y + right.Linear.Y;
131            return result;
132        }
133        public static void Add(ref ALVector2D left, ref  ALVector2D right, out ALVector2D result)
134        {
135            result.Angular = left.Angular + right.Angular;
136            result.Linear.X = left.Linear.X + right.Linear.X;
137            result.Linear.Y = left.Linear.Y + right.Linear.Y;
138        }
139
140        /// <summary>
141        /// Does Addition of 2 ALVector2Ds.
142        /// </summary>
143        /// <param name="left">The left ALVector2D operand.</param>
144        /// <param name="right">The right ALVector2D operand.</param>
145        /// <returns>The Sum of the ALVector2Ds.</returns>
146        public static ALVector2D operator +(ALVector2D left, ALVector2D right)
147        {
148            ALVector2D result;
149            result.Angular = left.Angular + right.Angular;
150            result.Linear.X = left.Linear.X + right.Linear.X;
151            result.Linear.Y = left.Linear.Y + right.Linear.Y;
152            return result;
153        }
154        /// <summary>
155        /// Does Subtraction of 2 ALVector2Ds.
156        /// </summary>
157        /// <param name="left">The left ALVector2D operand.</param>
158        /// <param name="right">The right ALVector2D operand.</param>
159        /// <returns>The Difference of the ALVector2Ds.</returns>
160        public static ALVector2D operator -(ALVector2D left, ALVector2D right)
161        {
162            ALVector2D result;
163            result.Angular = left.Angular - right.Angular;
164            result.Linear.X = left.Linear.X - right.Linear.X;
165            result.Linear.Y = left.Linear.Y - right.Linear.Y;
166            return result;
167        }
168        public static ALVector2D Subtract(ALVector2D left, ALVector2D right)
169        {
170            ALVector2D result;
171            result.Angular = left.Angular - right.Angular;
172            result.Linear.X = left.Linear.X - right.Linear.X;
173            result.Linear.Y = left.Linear.Y - right.Linear.Y;
174            return result;
175        }
176        public static void Subtract(ref ALVector2D left,ref  ALVector2D right, out ALVector2D result)
177        {
178            result.Angular = left.Angular - right.Angular;
179            result.Linear.X = left.Linear.X - right.Linear.X;
180            result.Linear.Y = left.Linear.Y - right.Linear.Y;
181        }
182        /// <summary>
183        /// Does Multiplication of 2 ALVector2Ds
184        /// </summary>
185        /// <param name="source">The ALVector2D to be Multiplied.</param>
186        /// <param name="scalar">The Scalar multiplier.</param>
187        /// <returns>The Product of the ALVector2Ds.</returns>
188        /// <remarks>It does normal Multiplication of the Angular value but does Scalar Multiplication of the Linear value.</remarks>
189        public static ALVector2D operator *(ALVector2D source, Scalar scalar)
190        {
191            ALVector2D result;
192            result.Angular = source.Angular * scalar;
193            result.Linear.X = source.Linear.X * scalar;
194            result.Linear.Y = source.Linear.Y * scalar;
195            return result;
196        }
197        public static ALVector2D Multiply(ALVector2D source, Scalar scalar)
198        {
199            ALVector2D result;
200            result.Angular = source.Angular * scalar;
201            result.Linear.X = source.Linear.X * scalar;
202            result.Linear.Y = source.Linear.Y * scalar;
203            return result;
204        }
205        public static void Multiply(ref ALVector2D source,ref Scalar scalar,out ALVector2D result)
206        {
207            result.Angular = source.Angular * scalar;
208            result.Linear.X = source.Linear.X * scalar;
209            result.Linear.Y = source.Linear.Y * scalar;
210        }
211        /// <summary>
212        /// Does Multiplication of 2 ALVector2Ds
213        /// </summary>
214        /// <param name="scalar">The Scalar multiplier.</param>
215        /// <param name="source">The ALVector2D to be Multiplied.</param>
216        /// <returns>The Product of the ALVector2Ds.</returns>
217        /// <remarks>It does normal Multiplication of the Angular value but does Scalar Multiplication of the Linear value.</remarks>
218        public static ALVector2D operator *(Scalar scalar, ALVector2D source)
219        {
220            ALVector2D result;
221            result.Angular = source.Angular * scalar;
222            result.Linear.X = source.Linear.X * scalar;
223            result.Linear.Y = source.Linear.Y * scalar;
224            return result;
225        }
226        public static ALVector2D Multiply(Scalar scalar, ALVector2D source)
227        {
228            ALVector2D result;
229            result.Angular = source.Angular * scalar;
230            result.Linear.X = source.Linear.X * scalar;
231            result.Linear.Y = source.Linear.Y * scalar;
232            return result;
233        }
234        public static void Multiply(ref Scalar scalar,ref ALVector2D source, out ALVector2D result)
235        {
236            result.Angular = source.Angular * scalar;
237            result.Linear.X = source.Linear.X * scalar;
238            result.Linear.Y = source.Linear.Y * scalar;
239        }
240        public static void ToMatrix2x3(ref ALVector2D source, out Matrix2x3 result)
241        {
242            result.m00 = MathHelper.Cos(source.Angular);
243            result.m10 = MathHelper.Sin(source.Angular);
244            result.m01 = -result.m10;
245            result.m11 = result.m00;
246            result.m02 = source.Linear.X;
247            result.m12 = source.Linear.Y;
248        }
249
250       /* public static ALVector2D Transform(Matrix2D matrix, ALVector2D source)
251        {
252            ALVector2D result;
253            Transform(ref matrix,ref source,out result);
254            return result;
255        }
256        public static void Transform(ref Matrix2D matrix, ref ALVector2D source, out ALVector2D result)
257        {
258            Scalar dAngle;
259            Vector2D angular = Vector2D.XAxis;
260            Vector2D.Transform(ref matrix.Vertex, ref source.Linear, out result.Linear);
261            Vector2D.Transform(ref matrix.Normal, ref angular, out angular);
262            Vector2D.GetAngle(ref angular, out dAngle);
263            result.Angular = source.Angular + dAngle;
264        }*/
265        public static void Transform(ref Matrix2x3 matrix, ref ALVector2D source, out ALVector2D result)
266        {
267            Scalar dAngle;
268            Vector2D angular = Vector2D.XAxis;
269            Vector2D.Transform(ref matrix, ref source.Linear, out result.Linear);
270            Matrix2x2 normal;
271            Matrix2x2.CreateNormal(ref matrix, out normal);
272
273            Vector2D.Transform(ref normal, ref angular, out angular);
274            Vector2D.GetAngle(ref angular, out dAngle);
275            result.Angular = source.Angular + dAngle;
276        }
277       
278       
279        public static bool operator ==(ALVector2D left, ALVector2D right)
280        {
281            return left.Angular == right.Angular &&
282                Vector2D.Equals(ref left.Linear, ref right.Linear);
283        }
284        public static bool operator !=(ALVector2D left, ALVector2D right)
285        {
286            return !(left.Angular == right.Angular &&
287                Vector2D.Equals(ref left.Linear, ref right.Linear));
288        }
289        public static bool Equals(ALVector2D left, ALVector2D right)
290        {
291            return left.Angular == right.Angular &&
292                Vector2D.Equals(ref left.Linear, ref right.Linear);
293        }
294        [CLSCompliant(false)]
295        public static bool Equals(ref ALVector2D left,ref ALVector2D right)
296        {
297            return left.Angular == right.Angular &&
298                Vector2D.Equals(ref left.Linear, ref right.Linear);
299        }
300        public override bool Equals(object obj)
301        {
302            return
303                obj is ALVector2D &&
304                Equals((ALVector2D)obj);
305        }
306        public bool Equals(ALVector2D other)
307        {
308            return Equals(ref this, ref other);
309        }
310        public override int GetHashCode()
311        {
312            return Angular.GetHashCode() ^ Linear.GetHashCode();
313        }
314        public override string ToString()
315        {
316            return string.Format("({0}, {1})", Angular, Linear);
317        }
318    }
319}
Note: See TracBrowser for help on using the repository browser.