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
)
150 string[] points
= source
.Split(',');
153 throw new InvalidOperationException ("source does not contain two numbers");
154 if (points
.Length
> 2)
155 throw new InvalidOperationException ("source contains too many delimiters");
157 CultureInfo ci
= CultureInfo
.InvariantCulture
;
158 return new Point (Convert
.ToDouble(points
[0],ci
), Convert
.ToDouble(points
[1],ci
));
161 public override string ToString ()
163 return this.ToString(null, null);
166 public string ToString (IFormatProvider formatProvider
)
168 return this.ToString(null,formatProvider
);
171 private string ToString(string format
,IFormatProvider formatProvider
)
173 CultureInfo ci
= (CultureInfo
)formatProvider
;
176 ci
= CultureInfo
.CurrentCulture
;
177 string seperator
= ci
.NumberFormat
.NumberDecimalSeparator
;
178 if (seperator
.Equals(","))
182 object[] ob
= { this.x, seperator, this.y }
;
184 return string.Format(formatProvider
, "{0:" + format + "}{1}{2:" + format + "}", ob
);
187 string IFormattable
.ToString (string format
, IFormatProvider formatProvider
)
189 return this.ToString(format
, formatProvider
);