Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorCore/Svg/SvgRectF.cs @ 12762

Last change on this file since 12762 was 12762, checked in by aballeit, 9 years ago

#2283 GUI updates, Tree-chart, MCTS Version 2 (prune leaves)

File size: 23.3 KB
Line 
1using System;
2using System.Globalization;
3
4namespace SharpVectors.Dom.Svg
5{
6    /// <summary>
7    /// This stores a set of four float precision numbers that represent the
8    /// location and size of a rectangle.
9    /// </summary>
10    [Serializable]
11    public struct SvgRectF : IEquatable<SvgRectF>
12    {
13        #region Private Fields
14
15        /// <summary>
16        /// Represents an instance of the <see cref="SvgRectF"/> structure
17        /// with its members uninitialized.
18        /// </summary>
19        public static readonly SvgRectF Empty = new SvgRectF();
20                   
21        private float _x;
22        private float _y;
23        private float _width;
24        private float _height;
25
26        #endregion
27
28        #region Constructors and Destructor
29
30        /// <summary>
31        /// Initializes a new instance of the <see cref="SvgRectF"/>
32        /// structure with the specified location and size.
33        /// </summary>
34        /// <param name="x">
35        /// The x-coordinate of the upper-left corner of the rectangle.
36        /// </param>
37        /// <param name="y">
38        /// The y-coordinate of the upper-left corner of the rectangle.
39        /// </param>
40        /// <param name="width">The width of the rectangle. </param>
41        /// <param name="height">The height of the rectangle. </param>
42        public SvgRectF(float x, float y, float width, float height)
43        {
44            _x      = x;
45            _y      = y;
46            _width  = width;
47            _height = height;
48        }
49
50        /// <summary>
51        /// Initializes a new instance of the <see cref="SvgRectF"/> structure
52        /// with the specified location and size.
53        /// </summary>
54        /// <param name="size">
55        /// A <see cref="SvgSizeF"/> that represents the width and height of the
56        /// rectangular region.
57        /// </param>
58        /// <param name="location">
59        /// A <see cref="SvgPointF"/> that represents the upper-left corner
60        /// of the rectangular region.
61        /// </param>
62        public SvgRectF(SvgPointF location, SvgSizeF size)
63        {
64            _x      = location.X;
65            _y      = location.Y;
66            _width  = size.Width;
67            _height = size.Height;
68        }
69
70        /// <summary>
71        /// Creates a <see cref="SvgRectF"/> structure with upper-left corner
72        /// and lower-right corner at the specified locations.
73        /// </summary>
74        /// <param name="left">
75        /// The x-coordinate of the upper-left corner of the rectangular region.
76        /// </param>
77        /// <param name="top">
78        /// The y-coordinate of the upper-left corner of the rectangular region.
79        /// </param>
80        /// <param name="right">
81        /// The x-coordinate of the lower-right corner of the rectangular region.
82        /// </param>
83        /// <param name="bottom">
84        /// The y-coordinate of the lower-right corner of the rectangular region.
85        /// </param>
86        /// <returns>
87        /// The new <see cref="SvgRectF"/> that this method creates.
88        /// </returns>
89        public static SvgRectF Create(float left, float top,
90            float right, float bottom)
91        {
92            return new SvgRectF(left, top, right - left, bottom - top);
93        }
94
95        #endregion
96
97        #region Public Properties
98
99        /// <summary>
100        /// Gets or sets the coordinates of the upper-left corner of this
101        /// <see cref="SvgRectF"/> structure.
102        /// </summary>
103        /// <value>
104        /// A <see cref="SvgPointF"/> that represents the upper-left corner of this
105        /// <see cref="SvgRectF"/> structure.
106        /// </value>
107        public SvgPointF Location
108        {
109            get
110            {
111                return new SvgPointF(_x, _y);
112            }
113            set
114            {
115                _x = value.X;
116                _y = value.Y;
117            }
118        }
119
120        /// <summary>
121        /// Gets or sets the size of this <see cref="SvgRectF"/>.
122        /// </summary>
123        /// <value>
124        /// A <see cref="SvgSizeF"/> that represents the width and height of this
125        /// <see cref="SvgRectF"/> structure.
126        /// </value>
127        public SvgSizeF Size
128        {
129            get
130            {
131                return new SvgSizeF(_width, _height);
132            }
133            set
134            {
135                _width  = value.Width;
136                _height = value.Height;
137            }
138        }
139
140        /// <summary>
141        /// Gets or sets the x-coordinate of the upper-left corner of this
142        /// <see cref="SvgRectF"/> structure.
143        /// </summary>
144        /// <value>
145        /// The x-coordinate of the upper-left corner of this
146        /// <see cref="SvgRectF"/> structure.
147        /// </value>
148        public float X
149        {
150            get
151            {
152                return _x;
153            }
154            set
155            {
156                _x = value;
157            }
158        }
159
160        /// <summary>
161        /// Gets or sets the y-coordinate of the upper-left corner of this
162        /// <see cref="SvgRectF"/> structure.
163        /// </summary>
164        /// <value>
165        /// The y-coordinate of the upper-left corner of this
166        /// <see cref="SvgRectF"/> structure.
167        /// </value>
168        public float Y
169        {
170            get
171            {
172                return _y;
173            }
174            set
175            {
176                _y = value;
177            }
178        }
179
180        /// <summary>
181        /// Gets or sets the width of this <see cref="SvgRectF"/> structure.
182        /// </summary>
183        /// <value>
184        /// The width of this <see cref="SvgRectF"/> structure.
185        /// </value>
186        public float Width
187        {
188            get
189            {
190                return _width;
191            }
192            set
193            {
194                _width = value;
195            }
196        }
197
198        /// <summary>
199        /// Gets or sets the height of this <see cref="SvgRectF"/> structure.
200        /// </summary>
201        /// <value>
202        /// The height of this <see cref="SvgRectF"/> structure.
203        /// </value>
204        public float Height
205        {
206            get
207            {
208                return _height;
209            }
210            set
211            {
212                _height = value;
213            }
214        }
215
216        /// <summary>
217        /// Gets the x-coordinate of the left edge of this <see cref="SvgRectF"/>
218        /// structure.
219        /// </summary>
220        /// <value>
221        /// The x-coordinate of the left edge of this <see cref="SvgRectF"/>
222        /// structure.
223        /// </value>
224        public float Left
225        {
226            get
227            {
228                return _x;
229            }
230        }
231
232        /// <summary>
233        /// Gets the y-coordinate of the top edge of this <see cref="SvgRectF"/>
234        /// structure.
235        /// </summary>
236        /// <value>
237        /// The y-coordinate of the top edge of this <see cref="SvgRectF"/>
238        /// structure.
239        /// </value>
240        public float Top
241        {
242            get
243            {
244                return _y;
245            }
246        }
247
248        /// <summary>
249        /// Gets the x-coordinate that is the sum of <see cref="SvgRectF.X"/>
250        /// and <see cref="SvgRectF.Width"/> of this <see cref="SvgRectF"/>
251        /// structure.
252        /// </summary>
253        /// <value>
254        /// The x-coordinate that is the sum of <see cref="SvgRectF.X"/> and
255        /// <see cref="SvgRectF.Width"/> of this <see cref="SvgRectF"/>
256        /// structure.
257        /// </value>
258        public float Right
259        {
260            get
261            {
262                return (_x + _width);
263            }
264        }
265
266        /// <summary>
267        /// Gets the y-coordinate that is the sum of <see cref="SvgRectF.Y"/>
268        /// and <see cref="SvgRectF.Height"/> of this <see cref="SvgRectF"/>
269        /// structure.
270        /// </summary>
271        /// <value>
272        /// The y-coordinate that is the sum of <see cref="SvgRectF.Y"/> and
273        /// <see cref="SvgRectF.Height"/> of this <see cref="SvgRectF"/>
274        /// structure.
275        /// </value>
276        public float Bottom
277        {
278            get
279            {
280                return (_y + _height);
281            }
282        }
283
284        /// <summary>
285        /// Tests whether the <see cref="SvgRectF.Width"/> or
286        /// <see cref="SvgRectF.Height"/> property of this
287        /// <see cref="SvgRectF"/> has a value of zero.
288        /// </summary>
289        /// <value>
290        /// This property returns true if the <see cref="SvgRectF.Width"/> or
291        /// <see cref="SvgRectF.Height"/> property of this
292        /// <see cref="SvgRectF"/> has a value of zero; otherwise, false.
293        /// </value>
294        public bool IsEmpty
295        {
296            get
297            {
298                if (_width > 0f)
299                {
300                    return (_height <= 0f);
301                }
302
303                return true;
304            }
305        }
306
307        #endregion
308
309        #region Public Methods
310
311        /// <summary>
312        /// This tests whether an object is a <see cref="SvgRectF"/> with the
313        /// same location and size of this <see cref="SvgRectF"/>.
314        /// </summary>
315        /// <param name="obj">
316        /// The <see cref="System.Object"/> to test.
317        /// </param>
318        /// <returns>
319        /// This returns <see langword="true"/> if the specified object is a
320        /// <see cref="SvgRectF"/> and its <see cref="X"/>, <see cref="Y"/>,
321        /// <see cref="Width"/>, and <see cref="Height"/> properties are equal
322        /// to the corresponding properties of this <see cref="SvgRectF"/>;
323        /// otherwise, <see langword="false"/>.
324        /// </returns>
325        public override bool Equals(object obj)
326        {
327            if (obj is SvgRectF)
328            {
329                return Equals((SvgRectF)obj);
330            }
331
332            return false;
333        }
334
335        /// <summary>
336        /// This tests whether the specified <see cref="SvgRectF"/> is with
337        /// the same location and size of this <see cref="SvgRectF"/>.
338        /// </summary>
339        /// <param name="other">
340        /// The <see cref="SvgRectF"/> to test.
341        /// </param>
342        /// <returns>
343        /// This returns <see langword="true"/> if specified <see cref="SvgRectF"/>
344        /// has its <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and
345        /// <see cref="Height"/> properties are equal to the corresponding
346        /// properties of this <see cref="SvgRectF"/>; otherwise,
347        /// <see langword="false"/>.
348        /// </returns>
349        public bool Equals(SvgRectF other)
350        {
351            if (((other.X == _x) && (other.Y == _y)) && (other.Width == _width))
352            {
353                return (other.Height == _height);
354            }
355
356            return false;
357        }
358
359        /// <overloads>
360        /// This determines if the specified point or rectangle is contained
361        /// within this <see cref="SvgRectF"/> structure.
362        /// </overloads>
363        /// <summary>
364        /// This determines if the specified point is contained within this
365        /// <see cref="SvgRectF"/> structure.
366        /// </summary>
367        /// <param name="x">The x-coordinate of the point to test. </param>
368        /// <param name="y">The y-coordinate of the point to test. </param>
369        /// <returns>
370        /// This method returns true if the point defined by x and y is
371        /// contained within this <see cref="SvgRectF"/> structure; otherwise
372        /// false.
373        /// </returns>
374        public bool Contains(float x, float y)
375        {
376            if (((_x <= x) && (x < (_x + _width))) && (_y <= y))
377            {
378                return (y < (_y + _height));
379            }
380
381            return false;
382        }
383
384        /// <summary>
385        /// This determines if the specified point is contained within this
386        /// <see cref="SvgRectF"/> structure.
387        /// </summary>
388        /// <param name="pt">The <see cref="SvgPointF"/> to test. </param>
389        /// <returns>
390        /// This method returns true if the point represented by the pt
391        /// parameter is contained within this <see cref="SvgRectF"/>
392        /// structure; otherwise false.
393        /// </returns>
394        public bool Contains(SvgPointF pt)
395        {
396            return this.Contains(pt.X, pt.Y);
397        }
398
399        /// <summary>
400        /// This determines if the rectangular region represented by rect is
401        /// entirely contained within this <see cref="SvgRectF"/> structure.
402        /// </summary>
403        /// <param name="rect">The <see cref="SvgRectF"/> to test. </param>
404        /// <returns>
405        /// This method returns true if the rectangular region represented by
406        /// rect is entirely contained within the rectangular region represented
407        /// by this <see cref="SvgRectF"/>; otherwise false.
408        /// </returns>
409        public bool Contains(SvgRectF rect)
410        {
411            if (((_x <= rect.X) && ((rect.X + rect.Width) <=
412                (_x + _width))) && (_y <= rect.Y))
413            {
414                return ((rect.Y + rect.Height) <= (_y + _height));
415            }
416
417            return false;
418        }
419
420        /// <summary>
421        /// Gets the hash code for this <see cref="SvgRectF"/> structure.
422        /// For information about the use of hash codes, see Object.GetHashCode.
423        /// </summary>
424        /// <returns>The hash code for this <see cref="SvgRectF"/>.</returns>
425        public override int GetHashCode()
426        {
427            return (_x.GetHashCode() ^ _y.GetHashCode() ^
428                _width.GetHashCode() ^ _height.GetHashCode());
429        }
430
431        /// <overloads>
432        /// Inflates this <see cref="SvgRectF"/> structure by the specified
433        /// amount.
434        /// </overloads>
435        /// <summary>
436        /// Inflates this <see cref="SvgRectF"/> structure by the specified
437        /// amount.
438        /// </summary>
439        /// <param name="x">The amount to inflate this <see cref="SvgRectF"/> structure horizontally. </param>
440        /// <param name="y">The amount to inflate this <see cref="SvgRectF"/> structure vertically. </param>
441        /// <returns>This method does not return a value.</returns>
442        public void Inflate(float x, float y)
443        {
444            _x      -= x;
445            _y      -= y;
446            _width  += 2f * x;
447            _height += 2f * y;
448        }
449
450        /// <summary>
451        /// Inflates this <see cref="SvgRectF"/> by the specified amount.
452        /// </summary>
453        /// <param name="size">The amount to inflate this rectangle. </param>
454        /// <returns>This method does not return a value.</returns>
455        public void Inflate(SvgSizeF size)
456        {
457            this.Inflate(size.Width, size.Height);
458        }
459
460        public void Intersect(SvgRectF rect)
461        {
462            SvgRectF ef = Intersection(rect, this);
463            _x      = ef.X;
464            _y      = ef.Y;
465            _width  = ef.Width;
466            _height = ef.Height;
467        }
468
469        /// <summary>
470        /// This replaces this <see cref="SvgRectF"/> structure with the
471        /// intersection of itself and the specified <see cref="SvgRectF"/>
472        /// structure.
473        /// </summary>
474        /// <param name="rect">The rectangle to intersect. </param>
475        /// <returns>This method does not return a value.</returns>
476        public void Intersection(SvgRectF rect)
477        {
478            SvgRectF result = SvgRectF.Intersection(rect, this);
479            _x      = result.X;
480            _y      = result.Y;
481            _width  = result.Width;
482            _height = result.Height;
483        }
484
485        /// <summary>
486        /// This determines if this rectangle intersects with rect.
487        /// </summary>
488        /// <param name="rect">The rectangle to test. </param>
489        /// <returns>
490        /// This method returns <see langword="true"/> if there is any
491        /// intersection; otherwise, <see langword="false"/>.
492        /// </returns>
493        public bool Intersects(SvgRectF rect)
494        {
495            if (((rect.X < (_x + _width)) && (_x < (rect.X + rect.Width))) &&
496                (rect.Y < (_y + _height)))
497            {
498                return (_y < (rect.Y + rect.Height));
499            }
500
501            return false;
502        }
503
504        /// <overloads>
505        /// Adjusts the location of this rectangle by the specified amount.
506        /// </overloads>
507        /// <summary>
508        /// Adjusts the location of this rectangle by the specified amount.
509        /// </summary>
510        /// <param name="pos">The amount to offset the location. </param>
511        /// <returns>This method does not return a value.</returns>
512        public void Offset(SvgPointF pos)
513        {
514            this.Offset(pos.X, pos.Y);
515        }
516
517        /// <summary>
518        /// Adjusts the location of this rectangle by the specified amount.
519        /// </summary>
520        /// <param name="y">The amount to offset the location vertically. </param>
521        /// <param name="x">The amount to offset the location horizontally. </param>
522        /// <returns>This method does not return a value.</returns>
523        public void Offset(float x, float y)
524        {
525            _x += x;
526            _y += y;
527        }
528
529        /// <summary>
530        /// Converts the Location and <see cref="Size"/> of this
531        /// <see cref="SvgRectF"/> to a human-readable string.
532        /// </summary>
533        /// <returns>
534        /// A string that contains the position, width, and height of this
535        /// <see cref="SvgRectF"/> structure; for example,
536        /// "{X=20, Y=20, Width=100, Height=50}".
537        /// </returns>
538        public override string ToString()
539        {
540            CultureInfo culture = CultureInfo.CurrentCulture;
541
542            return ("{X=" + _x.ToString(culture) + ",Y=" + _y.ToString(culture)
543                + ",Width=" + _width.ToString(culture)
544                + ",Height=" + _height.ToString(culture) + "}");
545        }
546
547        #endregion
548
549        #region Public Operators
550
551        /// <summary>
552        /// This tests whether two <see cref="SvgRectF"/> structures have equal
553        /// location and size.
554        /// </summary>
555        /// <param name="left">
556        /// The <see cref="SvgRectF"/> structure that is to the left of the
557        /// equality operator.
558        /// </param>
559        /// <param name="right">
560        /// The <see cref="SvgRectF"/> structure that is to the right of the
561        /// equality operator.
562        /// </param>
563        /// <returns>
564        /// This operator returns true if the two specified
565        /// <see cref="SvgRectF"/> structures have equal
566        /// <see cref="SvgRectF.X"/>, <see cref="SvgRectF.Y"/>,
567        /// <see cref="SvgRectF.Width"/>, and <see cref="SvgRectF.Height"/>
568        /// properties.
569        /// </returns>
570        public static bool operator ==(SvgRectF left, SvgRectF right)
571        {
572            if (((left.X == right.X) && (left.Y == right.Y)) &&
573                (left.Width == right.Width))
574            {
575                return (left.Height == right.Height);
576            }
577
578            return false;
579        }
580
581        /// <summary>
582        /// This tests whether two <see cref="SvgRectF"/> structures differ in
583        /// location or size.</summary>
584        /// <param name="left">
585        /// The <see cref="SvgRectF"/> structure that is to the left of the
586        /// inequality operator.
587        /// </param>
588        /// <param name="right">
589        /// The <see cref="SvgRectF"/> structure that is to the right of the
590        /// inequality operator.
591        /// </param>
592        /// <returns>
593        /// This operator returns true if any of the <see cref="SvgRectF.X"/>,
594        /// <see cref="SvgRectF.Y"/>, <see cref="SvgRectF.Width"/>, or
595        /// <see cref="SvgRectF.Height"/> properties of the two
596        /// <see cref="SvgRectF"/> structures are unequal; otherwise false.
597        /// </returns>
598        public static bool operator !=(SvgRectF left, SvgRectF right)
599        {
600            return !(left == right);
601        }
602
603        #endregion
604
605        #region Public Static Methods
606
607        /// <summary>
608        /// Creates and returns an inflated copy of the specified
609        /// <see cref="SvgRectF"/> structure. The copy is inflated by the
610        /// specified amount. The original rectangle remains unmodified.
611        /// </summary>
612        /// <param name="rect">
613        /// The <see cref="SvgRectF"/> to be copied. This rectangle is not
614        /// modified.
615        /// </param>
616        /// <param name="x">
617        /// The amount to inflate the copy of the rectangle horizontally.
618        /// </param>
619        /// <param name="y">
620        /// The amount to inflate the copy of the rectangle vertically.
621        /// </param>
622        /// <returns>The inflated <see cref="SvgRectF"/>.</returns>
623        public static SvgRectF Inflate(SvgRectF rect, float x, float y)
624        {
625            SvgRectF result = rect;
626            result.Inflate(x, y);
627
628            return result;
629        }
630
631        /// <summary>
632        /// Returns a <see cref="SvgRectF"/> structure that represents the
633        /// intersection of two rectangles. If there is no intersection, and
634        /// empty <see cref="SvgRectF"/> is returned.
635        /// </summary>
636        /// <param name="a">A rectangle to intersect. </param>
637        /// <param name="b">A rectangle to intersect. </param>
638        /// <returns>
639        /// A third <see cref="SvgRectF"/> structure the size of which
640        /// represents the overlapped area of the two specified rectangles.
641        /// </returns>
642        public static SvgRectF Intersection(SvgRectF a, SvgRectF b)
643        {
644            float dLeft   = Math.Max(a.X, b.X);
645            float dRight  = Math.Min(a.X + a.Width, b.X + b.Width);
646            float dTop    = Math.Max(a.Y, b.Y);
647            float dBottom = Math.Min(a.Y + a.Height, b.Y + b.Height);
648            if ((dRight >= dLeft) && (dBottom >= dTop))
649            {
650                return new SvgRectF(dLeft, dTop, dRight - dLeft,
651                    dBottom - dTop);
652            }
653
654            return SvgRectF.Empty;
655        }
656
657        /// <summary>
658        /// Creates the smallest possible third rectangle that can contain both
659        /// of two rectangles that form a union.
660        /// </summary>
661        /// <param name="a">A rectangle to union. </param>
662        /// <param name="b">A rectangle to union. </param>
663        /// <returns>
664        /// A third <see cref="SvgRectF"/> structure that contains both of
665        /// the two rectangles that form the union.
666        /// </returns>
667        public static SvgRectF Union(SvgRectF a, SvgRectF b)
668        {
669            float dLeft   = Math.Min(a.X, b.X);
670            float dRight  = Math.Max(a.X + a.Width, b.X + b.Width);
671            float dTop    = Math.Min(a.Y, b.Y);
672            float dBottom = Math.Max(a.Y + a.Height, b.Y + b.Height);
673
674            return new SvgRectF(dLeft, dTop, dRight - dLeft, dBottom - dTop);
675        }
676
677        #endregion
678    }
679}
Note: See TracBrowser for help on using the repository browser.