(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Drawing / System.Drawing / RectangleF.cs
blobdd1505fbc09f83ce0d561ee627973df34fa91904
1 //
2 // System.Drawing.RectangleF.cs
3 //
4 // Author:
5 // Mike Kestner (mkestner@speakeasy.net)
6 //
7 // Copyright (C) 2001 Mike Kestner
8 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
9 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System;
35 using System.ComponentModel;
37 namespace System.Drawing
39 [Serializable]
40 public struct RectangleF
42 private float x, y, width, height;
44 /// <summary>
45 /// Empty Shared Field
46 /// </summary>
47 ///
48 /// <remarks>
49 /// An uninitialized RectangleF Structure.
50 /// </remarks>
52 public static readonly RectangleF Empty;
55 /// <summary>
56 /// FromLTRB Shared Method
57 /// </summary>
58 ///
59 /// <remarks>
60 /// Produces a RectangleF structure from left, top, right,
61 /// and bottom coordinates.
62 /// </remarks>
64 public static RectangleF FromLTRB (float left, float top,
65 float right, float bottom)
67 return new RectangleF (left, top, right - left, bottom - top);
70 /// <summary>
71 /// Inflate Shared Method
72 /// </summary>
73 ///
74 /// <remarks>
75 /// Produces a new RectangleF by inflating an existing
76 /// RectangleF by the specified coordinate values.
77 /// </remarks>
79 public static RectangleF Inflate (RectangleF r,
80 float x, float y)
82 RectangleF ir = new RectangleF (r.X, r.Y, r.Width, r.Height);
83 ir.Inflate (x, y);
84 return ir;
87 /// <summary>
88 /// Inflate Method
89 /// </summary>
90 ///
91 /// <remarks>
92 /// Inflates the RectangleF by a specified width and height.
93 /// </remarks>
95 public void Inflate (float width, float height)
97 x -= width;
98 y -= height;
99 width += width * 2;
100 height += height * 2;
103 /// <summary>
104 /// Inflate Method
105 /// </summary>
107 /// <remarks>
108 /// Inflates the RectangleF by a specified Size.
109 /// </remarks>
111 public void Inflate (SizeF sz)
113 Inflate (sz.Width, sz.Height);
116 /// <summary>
117 /// Intersect Shared Method
118 /// </summary>
120 /// <remarks>
121 /// Produces a new RectangleF by intersecting 2 existing
122 /// RectangleFs. Returns null if there is no intersection.
123 /// </remarks>
125 public static RectangleF Intersect (RectangleF r1,
126 RectangleF r2)
128 RectangleF r = new RectangleF (r1.X, r1.Y, r1.Width, r1.Height);
129 r.Intersect (r2);
130 return r;
133 /// <summary>
134 /// Intersect Method
135 /// </summary>
137 /// <remarks>
138 /// Replaces the RectangleF with the intersection of itself
139 /// and another RectangleF.
140 /// </remarks>
142 public void Intersect (RectangleF r)
144 if (!IntersectsWith (r)) {
145 x = 0;
146 y = 0;
147 width = 0;
148 height = 0;
151 x = Math.Max (Left, r.Left);
152 y = Math.Max (Top, r.Top);
153 width = Math.Min (Right, r.Right) - X;
154 height = Math.Min (Bottom, r.Bottom) - Y;
157 /// <summary>
158 /// Union Shared Method
159 /// </summary>
161 /// <remarks>
162 /// Produces a new RectangleF from the union of 2 existing
163 /// RectangleFs.
164 /// </remarks>
166 public static RectangleF Union (RectangleF r1, RectangleF r2)
168 return FromLTRB (Math.Min (r1.Left, r2.Left),
169 Math.Min (r1.Top, r2.Top),
170 Math.Max (r1.Right, r2.Right),
171 Math.Max (r1.Bottom, r2.Bottom));
174 /// <summary>
175 /// Equality Operator
176 /// </summary>
178 /// <remarks>
179 /// Compares two RectangleF objects. The return value is
180 /// based on the equivalence of the Location and Size
181 /// properties of the two RectangleFs.
182 /// </remarks>
184 public static bool operator == (RectangleF r1, RectangleF r2)
186 return (r1.X == r2.X) && (r1.Y == r2.Y) &&
187 (r1.Width == r2.Width) && (r1.Height == r2.Height);
190 /// <summary>
191 /// Inequality Operator
192 /// </summary>
194 /// <remarks>
195 /// Compares two RectangleF objects. The return value is
196 /// based on the equivalence of the Location and Size
197 /// properties of the two RectangleFs.
198 /// </remarks>
200 public static bool operator != (RectangleF r1, RectangleF r2)
202 return (r1.X != r2.X) && (r1.Y != r2.Y) &&
203 (r1.Width != r2.Width) && (r1.Height != r2.Height);
206 /// <summary>
207 /// Rectangle to RectangleF Conversion
208 /// </summary>
210 /// <remarks>
211 /// Converts a Rectangle object to a RectangleF.
212 /// </remarks>
214 public static implicit operator RectangleF (Rectangle r)
216 return new RectangleF (r.X, r.Y, r.Width, r.Height);
220 // -----------------------
221 // Public Constructors
222 // -----------------------
224 /// <summary>
225 /// RectangleF Constructor
226 /// </summary>
228 /// <remarks>
229 /// Creates a RectangleF from PointF and SizeF values.
230 /// </remarks>
232 public RectangleF (PointF loc, SizeF sz)
234 x = loc.X;
235 y = loc.Y;
236 width = sz.Width;
237 height = sz.Height;
240 /// <summary>
241 /// RectangleF Constructor
242 /// </summary>
244 /// <remarks>
245 /// Creates a RectangleF from a specified x,y location and
246 /// width and height values.
247 /// </remarks>
249 public RectangleF (float x, float y, float width, float height)
251 this.x = x;
252 this.y = y;
253 this.width = width;
254 this.height = height;
259 /// <summary>
260 /// Bottom Property
261 /// </summary>
263 /// <remarks>
264 /// The Y coordinate of the bottom edge of the RectangleF.
265 /// Read only.
266 /// </remarks>
268 [Browsable (false)]
269 public float Bottom {
270 get {
271 return Y + Height;
275 /// <summary>
276 /// Height Property
277 /// </summary>
279 /// <remarks>
280 /// The Height of the RectangleF.
281 /// </remarks>
283 public float Height {
284 get {
285 return height;
287 set {
288 height = value;
292 /// <summary>
293 /// IsEmpty Property
294 /// </summary>
296 /// <remarks>
297 /// Indicates if the width or height are zero. Read only.
298 /// </remarks>
300 // LAMESPEC: Documentation says "This property returns true if
301 // the Width, Height, X, and Y properties of this RectangleF all
302 // have values of zero; otherwise, false.". Reality returns TRUE if
303 // width or height are equal 0
305 [Browsable (false)]
306 public bool IsEmpty {
307 get {
308 return ((width == 0) || (height == 0));
312 /// <summary>
313 /// Left Property
314 /// </summary>
316 /// <remarks>
317 /// The X coordinate of the left edge of the RectangleF.
318 /// Read only.
319 /// </remarks>
321 [Browsable (false)]
322 public float Left {
323 get {
324 return X;
328 /// <summary>
329 /// Location Property
330 /// </summary>
332 /// <remarks>
333 /// The Location of the top-left corner of the RectangleF.
334 /// </remarks>
336 [Browsable (false)]
337 public PointF Location {
338 get {
339 return new PointF (x, y);
341 set {
342 x = value.X;
343 y = value.Y;
347 /// <summary>
348 /// Right Property
349 /// </summary>
351 /// <remarks>
352 /// The X coordinate of the right edge of the RectangleF.
353 /// Read only.
354 /// </remarks>
356 [Browsable (false)]
357 public float Right {
358 get {
359 return X + Width;
363 /// <summary>
364 /// Size Property
365 /// </summary>
367 /// <remarks>
368 /// The Size of the RectangleF.
369 /// </remarks>
371 [Browsable (false)]
372 public SizeF Size {
373 get {
374 return new SizeF (width, height);
376 set {
377 width = value.Width;
378 height = value.Height;
382 /// <summary>
383 /// Top Property
384 /// </summary>
386 /// <remarks>
387 /// The Y coordinate of the top edge of the RectangleF.
388 /// Read only.
389 /// </remarks>
391 [Browsable (false)]
392 public float Top {
393 get {
394 return Y;
398 /// <summary>
399 /// Width Property
400 /// </summary>
402 /// <remarks>
403 /// The Width of the RectangleF.
404 /// </remarks>
406 public float Width {
407 get {
408 return width;
410 set {
411 width = value;
415 /// <summary>
416 /// X Property
417 /// </summary>
419 /// <remarks>
420 /// The X coordinate of the RectangleF.
421 /// </remarks>
423 public float X {
424 get {
425 return x;
427 set {
428 x = value;
432 /// <summary>
433 /// Y Property
434 /// </summary>
436 /// <remarks>
437 /// The Y coordinate of the RectangleF.
438 /// </remarks>
440 public float Y {
441 get {
442 return y;
444 set {
445 y = value;
449 /// <summary>
450 /// Contains Method
451 /// </summary>
453 /// <remarks>
454 /// Checks if an x,y coordinate lies within this RectangleF.
455 /// </remarks>
457 public bool Contains (float x, float y)
459 return ((x >= Left) && (x <= Right) &&
460 (y >= Top) && (y <= Bottom));
463 /// <summary>
464 /// Contains Method
465 /// </summary>
467 /// <remarks>
468 /// Checks if a Point lies within this RectangleF.
469 /// </remarks>
471 public bool Contains (PointF pt)
473 return Contains (pt.X, pt.Y);
476 /// <summary>
477 /// Contains Method
478 /// </summary>
480 /// <remarks>
481 /// Checks if a RectangleF lies entirely within this
482 /// RectangleF.
483 /// </remarks>
485 public bool Contains (RectangleF rect)
487 return (rect == Intersect (this, rect));
490 /// <summary>
491 /// Equals Method
492 /// </summary>
494 /// <remarks>
495 /// Checks equivalence of this RectangleF and an object.
496 /// </remarks>
498 public override bool Equals (object o)
500 if (!(o is RectangleF))
501 return false;
503 return (this == (RectangleF) o);
506 /// <summary>
507 /// GetHashCode Method
508 /// </summary>
510 /// <remarks>
511 /// Calculates a hashing value.
512 /// </remarks>
514 public override int GetHashCode ()
516 return (int) (x + y + width + height);
519 /// <summary>
520 /// IntersectsWith Method
521 /// </summary>
523 /// <remarks>
524 /// Checks if a RectangleF intersects with this one.
525 /// </remarks>
527 public bool IntersectsWith (RectangleF r)
529 return !((Left > r.Right) || (Right < r.Left) ||
530 (Top > r.Bottom) || (Bottom < r.Top));
533 /// <summary>
534 /// Offset Method
535 /// </summary>
537 /// <remarks>
538 /// Moves the RectangleF a specified distance.
539 /// </remarks>
541 public void Offset (float dx, float dy)
543 X += dx;
544 Y += dy;
547 /// <summary>
548 /// Offset Method
549 /// </summary>
551 /// <remarks>
552 /// Moves the RectangleF a specified distance.
553 /// </remarks>
555 public void Offset (PointF pt)
557 Offset(pt.X, pt.Y);
560 /// <summary>
561 /// ToString Method
562 /// </summary>
564 /// <remarks>
565 /// Formats the RectangleF in (x,y,w,h) notation.
566 /// </remarks>
568 public override string ToString ()
570 return String.Format ("{{X={0},Y={1},Width={2},Height={3}}}",
571 x, y, width, height);