External/OxyPlot/OxyPlot/Foundation/OxyRect.cs
author moel.mich
Sat, 08 Jun 2013 16:53:22 +0000
changeset 391 5be8f2773237
permissions -rw-r--r--
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)
     4 //
     5 //   Copyright (c) 2012 Oystein Bjorke
     6 //
     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:
    14 //
    15 //   The above copyright notice and this permission notice shall be included
    16 //   in all copies or substantial portions of the Software.
    17 //
    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.
    25 // </copyright>
    26 // <summary>
    27 //   Describes the width, height, and point origin of a rectangle.
    28 // </summary>
    29 // --------------------------------------------------------------------------------------------------------------------
    30 namespace OxyPlot
    31 {
    32     using System;
    33     using System.Diagnostics;
    34     using System.Globalization;
    35 
    36     /// <summary>
    37     /// Describes the width, height, and point origin of a rectangle.
    38     /// </summary>
    39     public struct OxyRect
    40     {
    41         /// <summary>
    42         /// The height of the rectangle.
    43         /// </summary>
    44         private double height;
    45 
    46         /// <summary>
    47         /// The x-coordinate location of the left side of the rectangle.
    48         /// </summary>
    49         private double left;
    50 
    51         /// <summary>
    52         /// The y-coordinate location of the top side of the rectangle.
    53         /// </summary>
    54         private double top;
    55 
    56         /// <summary>
    57         /// The width of the rectangle.
    58         /// </summary>
    59         private double width;
    60 
    61         /// <summary>
    62         /// Initializes a new instance of the <see cref="OxyRect"/> structure that has the specified x-coordinate, y-coordinate, width, and height.
    63         /// </summary>
    64         /// <param name="left">
    65         /// The x-coordinate location of the left side of the rectangle.
    66         /// </param>
    67         /// <param name="top">
    68         /// The y-coordinate location of the top side of the rectangle.
    69         /// </param>
    70         /// <param name="width">
    71         /// The width of the rectangle.
    72         /// </param>
    73         /// <param name="height">
    74         /// The height of the rectangle.
    75         /// </param>
    76         public OxyRect(double left, double top, double width, double height)
    77         {
    78             this.left = left;
    79             this.top = top;
    80             this.width = width;
    81             this.height = height;
    82             Debug.Assert(width >= 0, "Width should be larger than 0.");
    83             Debug.Assert(height >= 0, "Height should be larger than 0.");
    84         }
    85 
    86         /// <summary>
    87         /// Gets or sets the y-axis value of the bottom of the rectangle.
    88         /// </summary>
    89         /// <value>
    90         /// The bottom.
    91         /// </value>
    92         public double Bottom
    93         {
    94             get
    95             {
    96                 return this.top + this.height;
    97             }
    98 
    99             set
   100             {
   101                 this.height = value - this.top;
   102             }
   103         }
   104 
   105         /// <summary>
   106         /// Gets or sets the height of the rectangle.
   107         /// </summary>
   108         /// <value>
   109         /// The height.
   110         /// </value>
   111         public double Height
   112         {
   113             get
   114             {
   115                 return this.height;
   116             }
   117 
   118             set
   119             {
   120                 this.height = value;
   121             }
   122         }
   123 
   124         /// <summary>
   125         /// Gets or sets the x-axis value of the left side of the rectangle.
   126         /// </summary>
   127         /// <value>
   128         /// The left.
   129         /// </value>
   130         public double Left
   131         {
   132             get
   133             {
   134                 return this.left;
   135             }
   136 
   137             set
   138             {
   139                 this.left = value;
   140             }
   141         }
   142 
   143         /// <summary>
   144         /// Gets or sets the x-axis value of the right side of the rectangle.
   145         /// </summary>
   146         /// <value>
   147         /// The right.
   148         /// </value>
   149         public double Right
   150         {
   151             get
   152             {
   153                 return this.left + this.width;
   154             }
   155 
   156             set
   157             {
   158                 this.width = value - this.left;
   159             }
   160         }
   161 
   162         /// <summary>
   163         /// Gets or sets the y-axis position of the top of the rectangle.
   164         /// </summary>
   165         /// <value>
   166         /// The top.
   167         /// </value>
   168         public double Top
   169         {
   170             get
   171             {
   172                 return this.top;
   173             }
   174 
   175             set
   176             {
   177                 this.top = value;
   178             }
   179         }
   180 
   181         /// <summary>
   182         /// Gets or sets the width of the rectangle.
   183         /// </summary>
   184         /// <value>
   185         /// The width.
   186         /// </value>
   187         public double Width
   188         {
   189             get
   190             {
   191                 return this.width;
   192             }
   193 
   194             set
   195             {
   196                 this.width = value;
   197             }
   198         }
   199 
   200         /// <summary>
   201         /// Gets the center point of the rectangle.
   202         /// </summary>
   203         /// <value>The center.</value>
   204         public ScreenPoint Center
   205         {
   206             get
   207             {
   208                 return new ScreenPoint(this.left + (this.width * 0.5), this.top + (this.height * 0.5));
   209             }
   210         }
   211 
   212         /// <summary>
   213         /// Creates a rectangle from the specified corner coordinates.
   214         /// </summary>
   215         /// <param name="x0">
   216         /// The x0.
   217         /// </param>
   218         /// <param name="y0">
   219         /// The y0.
   220         /// </param>
   221         /// <param name="x1">
   222         /// The x1.
   223         /// </param>
   224         /// <param name="y1">
   225         /// The y1.
   226         /// </param>
   227         /// <returns>
   228         /// A rectangle.
   229         /// </returns>
   230         public static OxyRect Create(double x0, double y0, double x1, double y1)
   231         {
   232             return new OxyRect(Math.Min(x0, x1), Math.Min(y0, y1), Math.Abs(x1 - x0), Math.Abs(y1 - y0));
   233         }
   234 
   235         /// <summary>
   236         /// Creates a rectangle from the specified corner coordinates.
   237         /// </summary>
   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)
   242         {
   243             return Create(p0.X, p0.Y, p1.X, p1.Y);
   244         }
   245 
   246         /// <summary>
   247         /// Determines whether the specified point is inside the rectangle.
   248         /// </summary>
   249         /// <param name="x">
   250         /// The x coordinate.
   251         /// </param>
   252         /// <param name="y">
   253         /// The y coordinate.
   254         /// </param>
   255         /// <returns>
   256         /// <c>true</c> if the rectangle contains the specified point; otherwise, <c>false</c>.
   257         /// </returns>
   258         public bool Contains(double x, double y)
   259         {
   260             return x >= this.Left && x <= this.Right && y >= this.Top && y <= this.Bottom;
   261         }
   262 
   263         /// <summary>
   264         /// Determines whether the specified point is inside the rectangle.
   265         /// </summary>
   266         /// <param name="p">The point.</param>
   267         /// <returns>
   268         /// <c>true</c> if the rectangle contains the specified point; otherwise, <c>false</c>.
   269         /// </returns>
   270         public bool Contains(ScreenPoint p)
   271         {
   272             return this.Contains(p.x, p.y);
   273         }
   274 
   275         /// <summary>
   276         /// Returns a <see cref="System.String"/> that represents this instance.
   277         /// </summary>
   278         /// <returns>
   279         /// A <see cref="System.String"/> that represents this instance.
   280         /// </returns>
   281         public override string ToString()
   282         {
   283             return string.Format(
   284                 CultureInfo.InvariantCulture, "({0}, {1}, {2}, {3})", this.left, this.top, this.width, this.height);
   285         }
   286     }
   287 }