Added the source code of OxyPlot as of commit d190d7748a73 (6.5.2013).
1 // --------------------------------------------------------------------------------------------------------------------
2 // <copyright file="OxyRect.cs" company="OxyPlot">
3 // The MIT License (MIT)
5 // Copyright (c) 2012 Oystein Bjorke
7 // Permission is hereby granted, free of charge, to any person obtaining a
8 // copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be included
16 // in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 // Describes the width, height, and point origin of a rectangle.
29 // --------------------------------------------------------------------------------------------------------------------
33 using System.Diagnostics;
34 using System.Globalization;
37 /// Describes the width, height, and point origin of a rectangle.
42 /// The height of the rectangle.
44 private double height;
47 /// The x-coordinate location of the left side of the rectangle.
52 /// The y-coordinate location of the top side of the rectangle.
57 /// The width of the rectangle.
62 /// Initializes a new instance of the <see cref="OxyRect"/> structure that has the specified x-coordinate, y-coordinate, width, and height.
64 /// <param name="left">
65 /// The x-coordinate location of the left side of the rectangle.
67 /// <param name="top">
68 /// The y-coordinate location of the top side of the rectangle.
70 /// <param name="width">
71 /// The width of the rectangle.
73 /// <param name="height">
74 /// The height of the rectangle.
76 public OxyRect(double left, double top, double width, double height)
82 Debug.Assert(width >= 0, "Width should be larger than 0.");
83 Debug.Assert(height >= 0, "Height should be larger than 0.");
87 /// Gets or sets the y-axis value of the bottom of the rectangle.
96 return this.top + this.height;
101 this.height = value - this.top;
106 /// Gets or sets the height of the rectangle.
125 /// Gets or sets the x-axis value of the left side of the rectangle.
144 /// Gets or sets the x-axis value of the right side of the rectangle.
153 return this.left + this.width;
158 this.width = value - this.left;
163 /// Gets or sets the y-axis position of the top of the rectangle.
182 /// Gets or sets the width of the rectangle.
201 /// Gets the center point of the rectangle.
203 /// <value>The center.</value>
204 public ScreenPoint Center
208 return new ScreenPoint(this.left + (this.width * 0.5), this.top + (this.height * 0.5));
213 /// Creates a rectangle from the specified corner coordinates.
215 /// <param name="x0">
218 /// <param name="y0">
221 /// <param name="x1">
224 /// <param name="y1">
230 public static OxyRect Create(double x0, double y0, double x1, double y1)
232 return new OxyRect(Math.Min(x0, x1), Math.Min(y0, y1), Math.Abs(x1 - x0), Math.Abs(y1 - y0));
236 /// Creates a rectangle from the specified corner coordinates.
238 /// <param name="p0">The first corner.</param>
239 /// <param name="p1">The second corner.</param>
240 /// <returns>A rectangle.</returns>
241 public static OxyRect Create(ScreenPoint p0, ScreenPoint p1)
243 return Create(p0.X, p0.Y, p1.X, p1.Y);
247 /// Determines whether the specified point is inside the rectangle.
250 /// The x coordinate.
253 /// The y coordinate.
256 /// <c>true</c> if the rectangle contains the specified point; otherwise, <c>false</c>.
258 public bool Contains(double x, double y)
260 return x >= this.Left && x <= this.Right && y >= this.Top && y <= this.Bottom;
264 /// Determines whether the specified point is inside the rectangle.
266 /// <param name="p">The point.</param>
268 /// <c>true</c> if the rectangle contains the specified point; otherwise, <c>false</c>.
270 public bool Contains(ScreenPoint p)
272 return this.Contains(p.x, p.y);
276 /// Returns a <see cref="System.String"/> that represents this instance.
279 /// A <see cref="System.String"/> that represents this instance.
281 public override string ToString()
283 return string.Format(
284 CultureInfo.InvariantCulture, "({0}, {1}, {2}, {3})", this.left, this.top, this.width, this.height);