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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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)
23 // Chris Toshok (toshok@novell.com)
27 using System.ComponentModel;
28 using System.Windows.Converters;
29 using System.Windows.Markup;
30 using System.Windows.Media;
31 using System.Globalization;
33 namespace System.Windows {
36 [TypeConverter (typeof (PointConverter))]
37 [ValueSerializer (typeof (PointValueSerializer))]
38 public struct Point : IFormattable
40 public Point (double x, double y)
56 public override bool Equals (object o)
60 return Equals ((Point)o);
63 public bool Equals (Point value)
65 return _x == value.X && _y == value.Y;
68 public override int GetHashCode ()
70 return (_x.GetHashCode() ^ _y.GetHashCode());
74 public void Offset (double offsetX, double offsetY)
80 public static Point Add (Point point, Vector vector)
82 return new Point (point.X + vector.X, point.Y + vector.Y);
85 public static bool Equals (Point point1, Point point2)
87 return point1.Equals (point2);
90 public static Point Multiply (Point point, Matrix matrix)
92 return new Point (point.X * matrix.M11 + point.Y * matrix.M21 + matrix.OffsetX,
93 point.X * matrix.M12 + point.Y * matrix.M22 + matrix.OffsetY);
96 public static Vector Subtract (Point point1, Point point2)
98 return new Vector (point1.X - point2.X, point1.Y - point2.Y);
101 public static Point Subtract (Point point, Vector vector)
103 return new Point (point.X - vector.X, point.Y - vector.Y);
108 public static Vector operator -(Point point1, Point point2)
110 return Subtract (point1, point2);
113 public static Point operator -(Point point, Vector vector)
115 return Subtract (point, vector);
118 public static Point operator + (Point point, Vector vector)
120 return Add (point, vector);
123 public static Point operator * (Point point, Matrix matrix)
125 return Multiply (point, matrix);
128 public static bool operator != (Point point1, Point point2)
130 return !point1.Equals(point2);
133 public static bool operator == (Point point1, Point point2)
135 return point1.Equals(point2);
138 public static explicit operator Size (Point point)
140 return new Size (point.X, point.Y);
143 public static explicit operator Vector (Point point)
145 return new Vector (point.X, point.Y);
148 public static Point Parse (string source)
151 throw new ArgumentNullException ("source");
152 var tokenizer = new NumericListTokenizer (source, CultureInfo.InvariantCulture);
155 if (!double.TryParse (tokenizer.GetNextToken (), NumberStyles.Float, CultureInfo.InvariantCulture, out x) ||
156 !double.TryParse (tokenizer.GetNextToken (), NumberStyles.Float, CultureInfo.InvariantCulture, out y))
158 throw new FormatException (string.Format ("Invalid Point format: {0}", source));
160 if (!tokenizer.HasNoMoreTokens ())
162 throw new InvalidOperationException ("Invalid Point format: " + source);
164 return new Point(x, y);
167 public override string ToString ()
169 return this.ToString(null, null);
172 public string ToString (IFormatProvider provider)
174 return this.ToString(null, provider);
177 private string ToString(string format,IFormatProvider formatProvider)
179 if (formatProvider == null)
180 formatProvider = CultureInfo.CurrentCulture;
182 format = string.Empty;
183 var separator = NumericListTokenizer.GetSeparator (formatProvider);
184 var pointFormat = string.Format ("{{0:{0}}}{1}{{1:{0}}}", format, separator);
185 return string.Format (formatProvider, pointFormat, _x, _y);
188 string IFormattable.ToString (string format, IFormatProvider formatProvider)
190 return this.ToString(format, formatProvider);