Merge pull request #2734 from nealef/master
[mono.git] / mcs / class / WindowsBase / System.Windows / Vector.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Chris Toshok (toshok@novell.com)
24 //
25
26 using System;
27 using System.ComponentModel;
28 using System.Windows.Converters;
29 using System.Windows.Markup;
30 using System.Windows.Media;
31
32 namespace System.Windows {
33
34         [Serializable]
35         [ValueSerializer (typeof (VectorValueSerializer))]
36         [TypeConverter (typeof (VectorConverter))]
37         public struct Vector : IFormattable
38         {
39                 public Vector (double x, double y)
40                 {
41                         this._x = x;
42                         this._y = y;
43                 }
44
45                 public bool Equals (Vector value)
46                 {
47                         return _x == value.X && _y == value.Y;
48                 }
49
50                 public override bool Equals (object o)
51                 {
52                         if (!(o is Vector))
53                                 return false;
54
55                         return Equals ((Vector)o);
56                 }
57
58                 public override int GetHashCode ()
59                 {
60                         throw new NotImplementedException ();
61                 }
62
63                 string IFormattable.ToString (string format, IFormatProvider provider)
64                 {
65                         return string.Format (provider, "{0:" + format + "},{1:" + format + "}", _x, _y);
66                 }
67
68                 public static bool Equals (Vector vector1, Vector vector2)
69                 {
70                         return vector1.Equals (vector2);
71                 }
72
73                 public static Point Add (Vector vector, Point point)
74                 {
75                         return new Point (vector.X + point.X, vector.Y + point.Y);
76                 }
77
78                 public static Vector Add (Vector vector1, Vector vector2)
79                 {
80                         return new Vector (vector1.X + vector2.X,
81                                            vector1.Y + vector2.Y);
82                 }
83
84                 public static double AngleBetween (Vector vector1, Vector vector2)
85                 {
86                         double cos_theta = (vector1.X * vector2.X + vector1.Y * vector2.Y) / (vector1.Length * vector2.Length);
87
88                         return Math.Acos (cos_theta) / Math.PI * 180;
89                 }
90
91                 public static double CrossProduct (Vector vector1, Vector vector2)
92                 {
93                         // ... what operation is this exactly?
94                         return vector1.X * vector2.Y - vector1.Y * vector2.X;
95                 }
96
97                 public static double Determinant (Vector vector1, Vector vector2)
98                 {
99                         // same as CrossProduct, it appears.
100                         return vector1.X * vector2.Y - vector1.Y * vector2.X;
101                 }
102
103                 public static Vector Divide (Vector vector, double scalar)
104                 {
105                         return new Vector (vector.X / scalar, vector.Y / scalar);
106                 }
107
108                 public static double Multiply (Vector vector1, Vector vector2)
109                 {
110                         return vector1.X * vector2.X + vector1.Y * vector2.Y;
111                 }
112
113                 public static Vector Multiply (Vector vector, Matrix matrix)
114                 {
115                         return new Vector (vector.X * matrix.M11 + vector.Y * matrix.M21,
116                                            vector.X * matrix.M12 + vector.Y * matrix.M22);
117                 }
118
119                 public static Vector Multiply (double scalar, Vector vector)
120                 {
121                         return new Vector (scalar * vector.X, scalar * vector.Y);
122                 }
123
124                 public static Vector Multiply (Vector vector, double scalar)
125                 {
126                         return new Vector (scalar * vector.X, scalar * vector.Y);
127                 }
128
129                 public void Negate ()
130                 {
131                         _x = -_x;
132                         _y = -_y;
133                 }
134
135                 public void Normalize ()
136                 {
137                         double ls = LengthSquared;
138                         if (ls == 1)
139                                 return;
140
141                         double l = Math.Sqrt (ls);
142                         _x /= l;
143                         _y /= l;
144                 }
145
146                 public static Vector Subtract (Vector vector1, Vector vector2)
147                 {
148                         return new Vector (vector1.X - vector2.X, vector1.Y - vector2.Y);
149                 }
150
151                 public static Vector Parse (string source)
152                 {
153                         throw new NotImplementedException ();
154                 }
155
156                 public override string ToString ()
157                 {
158                         return String.Format ("{0},{1}", _x, _y);
159                 }
160
161                 public string ToString (IFormatProvider provider)
162                 {
163                         throw new NotImplementedException ();
164                 }
165
166                 public double Length {
167                         get { return Math.Sqrt (LengthSquared); }
168                 }
169
170                 public double LengthSquared {
171                         get { return _x * _x + _y * _y; }
172                 }
173
174                 public double X {
175                         get { return _x; }
176                         set { _x = value; }
177                 }
178
179                 public double Y {
180                         get { return _y; }
181                         set { _y = value; }
182                 }
183
184                 /* operators */
185                 public static explicit operator Point (Vector vector)
186                 {
187                         return new Point (vector.X, vector.Y);
188                 }
189
190                 public static explicit operator Size (Vector vector)
191                 {
192                         return new Size (vector.X, vector.Y);
193                 }
194
195                 public static Vector operator - (Vector vector1, Vector vector2)
196                 {
197                         return Subtract (vector1, vector2);
198                 }
199
200                 public static Vector operator - (Vector vector)
201                 {
202                         Vector result = vector;
203                         result.Negate ();
204                         return result;
205                 }
206
207                 public static bool operator != (Vector vector1, Vector vector2)
208                 {
209                         return !Equals (vector1, vector2);
210                 }
211
212                 public static bool operator == (Vector vector1, Vector vector2)
213                 {
214                         return Equals (vector1, vector2);
215                 }
216
217                 public static double operator * (Vector vector1, Vector vector2)
218                 {
219                         return Multiply (vector1, vector2);
220                 }
221
222                 public static Vector operator * (Vector vector, Matrix matrix)
223                 {
224                         return Multiply (vector, matrix);
225                 }
226
227                 public static Vector operator * (double scalar, Vector vector)
228                 {
229                         return Multiply (scalar, vector);
230                 }
231
232                 public static Vector operator * (Vector vector, double scalar)
233                 {
234                         return Multiply (vector, scalar);
235                 }
236
237                 public static Vector operator / (Vector vector, double scalar)
238                 {
239                         return Divide (vector, scalar);
240                 }
241
242                 public static Point operator + (Vector vector, Point point)
243                 {
244                         return Add (vector, point);
245                 }
246
247                 public static Vector operator + (Vector vector1, Vector vector2)
248                 {
249                         return Add (vector1, vector2);
250                 }
251
252                 double _x;
253                 double _y;
254         }
255
256 }