File: Rect.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (471 lines) | stat: -rw-r--r-- 10,289 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
//
// Authors:
//	Chris Toshok <toshok@novell.com>
//	Sebastien Pouliot  <sebastien@ximian.com>
//

using System;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Windows.Converters;
using System.Windows.Markup;
using System.Windows.Media;

namespace System.Windows {

	[Serializable]
	[ValueSerializer (typeof (RectValueSerializer))]
	[TypeConverter (typeof (RectConverter))]
	public struct Rect : IFormattable
	{
		public Rect (Size size)
		{
			_x = _y = 0.0;
			_width = size.Width;
			_height = size.Height;
		}

		public Rect (Point point, Vector vector) : this (point, Point.Add (point, vector))
		{ }

		public Rect (Point point1, Point point2)
		{
			if (point1.X < point2.X) {
				_x = point1.X;
				_width = point2.X - point1.X;
			}
			else {
				_x = point2.X;
				_width = point1.X - point2.X;
			}

			if (point1.Y < point2.Y) {
				_y = point1.Y;
				_height = point2.Y - point1.Y;
			}
			else {
				_y = point2.Y;
				_height = point1.Y - point2.Y;
			}
		}

		public Rect (double x, double y, double width, double height)
		{
			if (width < 0 || height < 0)
				throw new ArgumentException ("width and height must be non-negative.");
			this._x = x;
			this._y = y;
			this._width = width;
			this._height = height;
		}

		public Rect (Point location, Size size)
		{
			_x = location.X;
			_y = location.Y;
			_width = size.Width;
			_height = size.Height;
		}

		public bool Equals (Rect value)
		{
			return (_x == value.X &&
				_y == value.Y &&
				_width == value.Width &&
				_height == value.Height);
		}

		public static bool operator != (Rect rect1, Rect rect2)
		{
			return !(rect1.Location == rect2.Location && rect1.Size == rect2.Size);
		}

		public static bool operator == (Rect rect1, Rect rect2)
		{
			return rect1.Location == rect2.Location && rect1.Size == rect2.Size;
		}

		public override bool Equals (object o)
		{
			if (!(o is Rect))
				return false;

			return Equals ((Rect)o);
		}

		public static bool Equals (Rect rect1, Rect rect2)
		{
			return rect1.Equals (rect2);
		}

		public override int GetHashCode ()
		{
			throw new NotImplementedException ();
		}

		public bool Contains (Rect rect)
		{
			if (rect.Left < this.Left ||
			    rect.Right > this.Right)
				return false;

			if (rect.Top < this.Top ||
			    rect.Bottom > this.Bottom)
				return false;

			return true;
		}

		public bool Contains (double x, double y)
		{
			if (x < Left || x > Right)
				return false;
			if (y < Top || y > Bottom)
				return false;

			return true;
		}

		public bool Contains (Point point)
		{
			return Contains (point.X, point.Y);
		}

		public static Rect Inflate (Rect rect, double width, double height)
		{
			if (width < rect.Width * -2)
				return Rect.Empty;
			if (height < rect.Height * -2)
				return Rect.Empty;

			Rect result = rect;
			result.Inflate (width, height);
			return result;
		}

		public static Rect Inflate (Rect rect, Size size)
		{
			return Rect.Inflate (rect, size.Width, size.Height);
		}

		public void Inflate (double width, double height)
		{
			// XXX any error checking like in the static case?
			_x -= width;
			_y -= height;

			this._width += 2*width;
			this._height += 2*height;
		}

		public void Inflate (Size size)
		{
			Inflate (size.Width, size.Height);
		}

		public bool IntersectsWith(Rect rect)
		{
			return !((Left >= rect.Right) || (Right <= rect.Left) ||
			    (Top >= rect.Bottom) || (Bottom <= rect.Top));
		}

		public void Intersect(Rect rect)
		{
			double _x = Math.Max (this._x, rect._x);
			double _y = Math.Max (this._y, rect._y);
			double _width = Math.Min (Right, rect.Right) - _x;
			double _height = Math.Min (Bottom, rect.Bottom) - _y; 

			if (_width < 0 || _height < 0) {
				this._x = this._y = Double.PositiveInfinity;
				this._width = this._height = Double.NegativeInfinity;
			}
			else {
				this._x = _x;
				this._y = _y;
				this._width = _width;
				this._height = _height;
			}
		}

		public static Rect Intersect(Rect rect1, Rect rect2)
		{
			Rect result = rect1;
			result.Intersect (rect2);
			return result;
		}

		public void Offset(double offsetX, double offsetY)
		{
			_x += offsetX;
			_y += offsetY;
		}

		public static Rect Offset(Rect rect, double offsetX, double offsetY)
		{
			Rect result = rect;
			result.Offset (offsetX, offsetY);
			return result;
		}

		public void Offset (Vector offsetVector)
		{
			_x += offsetVector.X;
			_y += offsetVector.Y;
		}

		public static Rect Offset (Rect rect, Vector offsetVector)
		{
			Rect result = rect;
			result.Offset (offsetVector);
			return result;
		}

		public void Scale(double scaleX, double scaleY)
		{
			_x *= scaleX;
			_y *= scaleY;
			_width *= scaleX;
			_height *= scaleY;
		}

		public void Transform (Matrix matrix)
		{
			throw new NotImplementedException ();
		}

		public static Rect Transform (Rect rect, Matrix matrix)
		{
			Rect result = rect;
			result.Transform (matrix);
			return result;
		}

		public static Rect Union(Rect rect1, Rect rect2)
		{
			Rect result = rect1;
			result.Union (rect2);
			return result;
		}

		public static Rect Union(Rect rect, Point point)
		{
			Rect result = rect;
			result.Union (point);
			return result;
		}
		
		public void Union(Rect rect)
		{
			var left = Math.Min (Left, rect.Left);
			var top = Math.Min (Top, rect.Top);
			var right = Math.Max (Right, rect.Right);
			var bottom = Math.Max (Bottom, rect.Bottom);
			
			_x = left;
			_y = top;
			_width = right - left;
			_height = bottom - top;
		}

		public void Union(Point point)
		{
			Union (new Rect (point, point));
		}

		public static Rect Parse (string source)
		{
			throw new NotImplementedException ();
		}

		public override string ToString ()
		{
			return ToString (null);
		}

		public string ToString (IFormatProvider provider)
		{
			return ToString (null, provider);
		}

		string IFormattable.ToString (string format, IFormatProvider provider)
		{
			return ToString (format, provider);
		}

		private string ToString (string format, IFormatProvider provider)
		{
			if (IsEmpty)
				return "Empty";

			if (provider == null)
				provider = CultureInfo.CurrentCulture;

			if (format == null)
				format = string.Empty;

			string separator = ",";
			NumberFormatInfo numberFormat =
				provider.GetFormat (typeof (NumberFormatInfo)) as NumberFormatInfo;
			if (numberFormat != null &&
			    numberFormat.NumberDecimalSeparator == separator)
				separator = ";";

			string rectFormat = String.Format (
				"{{0:{0}}}{1}{{1:{0}}}{1}{{2:{0}}}{1}{{3:{0}}}",
				format, separator);
			return String.Format (provider, rectFormat,
				_x, _y, _width, _height);
		}

		public static Rect Empty { 
			get {
				Rect r = new Rect ();
				r._x = r._y = Double.PositiveInfinity;
				r._width = r._height = Double.NegativeInfinity;
				return r;
			} 
		}
		
		public bool IsEmpty { 
			get {
				return (_x == Double.PositiveInfinity &&
					_y == Double.PositiveInfinity &&
					_width == Double.NegativeInfinity &&
					_height == Double.NegativeInfinity);
			}
		}
		
		public Point Location { 
			get {
				return new Point (_x, _y);
			}
			set {
				if (IsEmpty)
					throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");

				_x = value.X;
				_y = value.Y;
			}
		}
		
		public Size Size { 
			get { 
				if (IsEmpty)
					return Size.Empty; 
				return new Size (_width, _height);
			}
			set {
				if (IsEmpty)
					throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");

				_width = value.Width;
				_height = value.Height;
			}
		}

		public double X {
			get { return _x; }
			set {
				if (IsEmpty)
					throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");

				_x = value;
			}
		}

		public double Y {
			get { return _y; }
			set {
				if (IsEmpty)
					throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");

				_y = value;
			}
		}

		public double Width {
			get { return _width; }
			set {
				if (IsEmpty)
					throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");

				if (value < 0)
					throw new ArgumentException ("width must be non-negative.");

				_width = value;
			}
		}

		public double Height {
			get { return _height; }
			set {
				if (IsEmpty)
					throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");

				if (value < 0)
					throw new ArgumentException ("height must be non-negative.");

				_height = value;
			}
		}

		public double Left { 
			get { return _x; }
		}

		public double Top { 
			get { return _y; }
		}
		
		public double Right { 
			get { return _x + _width; }
		}
		
		public double Bottom { 
			get { return _y + _height; }
		}
		
		public Point TopLeft { 
			get { return new Point (Left, Top); }
		}
		
		public Point TopRight { 
			get { return new Point (Right, Top); }
		}
		
		public Point BottomLeft { 
			get { return new Point (Left, Bottom); }
		}

		public Point BottomRight { 
			get { return new Point (Right, Bottom); }
		}
		
		double _x;
		double _y;
		double _width;
		double _height;
	}
}