2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / WindowsBase / System.Windows / Vector.cs
blob606f38b820a09242e7f9d5a94522c4c6a1d4774c
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.
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
22 // Authors:
23 // Chris Toshok (toshok@novell.com)
26 using System;
27 using System.ComponentModel;
28 using System.Windows.Converters;
29 using System.Windows.Markup;
30 using System.Windows.Media;
32 namespace System.Windows {
34 [Serializable]
35 [ValueSerializer (typeof (VectorValueSerializer))]
36 [TypeConverter (typeof (VectorConverter))]
37 public struct Vector : IFormattable
39 public Vector (double x, double y)
41 this.x = x;
42 this.y = y;
45 public bool Equals (Vector value)
47 return x == value.X && y == value.Y;
50 public override bool Equals (object o)
52 if (!(o is Vector))
53 return false;
55 return Equals ((Vector)o);
58 public override int GetHashCode ()
60 throw new NotImplementedException ();
63 string IFormattable.ToString(string format, IFormatProvider formatProvider) {
64 throw new NotImplementedException ();
67 public static bool Equals (Vector vector1, Vector vector2)
69 return vector1.Equals (vector2);
72 public static Point Add (Vector vector, Point point)
74 return new Point (vector.X + point.X, vector.Y + point.Y);
77 public static Vector Add (Vector vector1, Vector vector2)
79 return new Vector (vector1.X + vector2.X,
80 vector1.Y + vector2.Y);
83 public static double AngleBetween (Vector vector1, Vector vector2)
85 double cos_theta = (vector1.X * vector2.X + vector1.Y * vector2.Y) / (vector1.Length * vector2.Length);
87 return Math.Acos (cos_theta) / Math.PI * 180;
90 public static double CrossProduct (Vector vector1, Vector vector2)
92 // ... what operation is this exactly?
93 return vector1.X * vector2.Y - vector1.Y * vector2.X;
96 public static double Determinant (Vector vector1, Vector vector2)
98 // same as CrossProduct, it appears.
99 return vector1.X * vector2.Y - vector1.Y * vector2.X;
102 public static Vector Divide (Vector vector, double scalar)
104 return new Vector (vector.X / scalar, vector.Y / scalar);
107 public static double Multiply (Vector vector1, Vector vector2)
109 return vector1.X * vector2.X + vector1.Y * vector2.Y;
112 public static Vector Multiply (Vector vector, Matrix matrix)
114 return new Vector (vector.X * matrix.M11 + vector.Y * matrix.M21,
115 vector.X * matrix.M12 + vector.Y * matrix.M22);
118 public static Vector Multiply (double scalar, Vector vector)
120 return new Vector (scalar * vector.X, scalar * vector.Y);
123 public static Vector Multiply (Vector vector, double scalar)
125 return new Vector (scalar * vector.X, scalar * vector.Y);
128 public void Negate ()
130 x = -x;
131 y = -y;
134 public void Normalize ()
136 double ls = LengthSquared;
137 if (ls == 1)
138 return;
140 double l = Math.Sqrt (ls);
141 x /= l;
142 y /= l;
145 public static Vector Subtract (Vector vector1, Vector vector2)
147 return new Vector (vector1.X - vector2.X, vector1.Y - vector2.Y);
150 public static Vector Parse (string source)
152 throw new NotImplementedException ();
155 public override string ToString ()
157 return String.Format ("{0},{1}", x, y);
160 public string ToString (IFormatProvider provider)
162 throw new NotImplementedException ();
165 public double Length {
166 get { return Math.Sqrt (LengthSquared); }
169 public double LengthSquared {
170 get { return x * x + y * y; }
173 public double X {
174 get { return x; }
175 set { x = value; }
178 public double Y {
179 get { return y; }
180 set { y = value; }
183 /* operators */
184 public static explicit operator Point (Vector vector)
186 return new Point (vector.X, vector.Y);
189 public static explicit operator Size (Vector vector)
191 return new Size (vector.X, vector.Y);
194 public static Vector operator - (Vector vector1, Vector vector2)
196 return Subtract (vector1, vector2);
199 public static Vector operator - (Vector vector)
201 Vector result = vector;
202 result.Negate ();
203 return result;
206 public static bool operator != (Vector vector1, Vector vector2)
208 return !Equals (vector1, vector2);
211 public static bool operator == (Vector vector1, Vector vector2)
213 return Equals (vector1, vector2);
216 public static double operator * (Vector vector1, Vector vector2)
218 return Multiply (vector1, vector2);
221 public static Vector operator * (Vector vector, Matrix matrix)
223 return Multiply (vector, matrix);
226 public static Vector operator * (double scalar, Vector vector)
228 return Multiply (scalar, vector);
231 public static Vector operator * (Vector vector, double scalar)
233 return Multiply (vector, scalar);
236 public static Vector operator / (Vector vector, double scalar)
238 return Divide (vector, scalar);
241 public static Point operator + (Vector vector, Point point)
243 return Add (vector, point);
246 public static Vector operator + (Vector vector1, Vector vector2)
248 return Add (vector1, vector2);
251 double x;
252 double y;