External/OxyPlot/OxyPlot/Foundation/ScreenPoint.cs
author Stephane Lenclud
Sat, 30 Jan 2016 23:01:51 +0100
branchMiniDisplay
changeset 454 f84878f52cd9
permissions -rw-r--r--
Disabling Nuvoton NCT6791D because of fan full speed bug on Asus Z97 WS.
     1 // --------------------------------------------------------------------------------------------------------------------
     2 // <copyright file="ScreenPoint.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 a point defined in the screen coordinate system.
    28 // </summary>
    29 // --------------------------------------------------------------------------------------------------------------------
    30 namespace OxyPlot
    31 {
    32     using System;
    33 
    34     /// <summary>
    35     /// Represents a point defined in the screen coordinate system.
    36     /// </summary>
    37     /// <remarks>
    38     /// The rendering methods transforms <see cref="DataPoint"/>s to <see cref="ScreenPoint"/>s.
    39     /// </remarks>
    40     public struct ScreenPoint
    41     {
    42         /// <summary>
    43         /// The undefined point.
    44         /// </summary>
    45         public static readonly ScreenPoint Undefined = new ScreenPoint(double.NaN, double.NaN);
    46 
    47         /// <summary>
    48         /// The x-coordinate.
    49         /// </summary>
    50         internal double x;
    51 
    52         /// <summary>
    53         /// The y-coordinate.
    54         /// </summary>
    55         internal double y;
    56 
    57         /// <summary>
    58         /// Initializes a new instance of the <see cref="ScreenPoint"/> struct.
    59         /// </summary>
    60         /// <param name="x">
    61         /// The x-coordinate.
    62         /// </param>
    63         /// <param name="y">
    64         /// The y-coordinate.
    65         /// </param>
    66         public ScreenPoint(double x, double y)
    67         {
    68             this.x = x;
    69             this.y = y;
    70         }
    71 
    72         /// <summary>
    73         /// Gets or sets the x-coordinate.
    74         /// </summary>
    75         /// <value> The x-coordinate. </value>
    76         public double X
    77         {
    78             get
    79             {
    80                 return this.x;
    81             }
    82 
    83             set
    84             {
    85                 this.x = value;
    86             }
    87         }
    88 
    89         /// <summary>
    90         /// Gets or sets the y-coordinate.
    91         /// </summary>
    92         /// <value> The y-coordinate. </value>
    93         public double Y
    94         {
    95             get
    96             {
    97                 return this.y;
    98             }
    99 
   100             set
   101             {
   102                 this.y = value;
   103             }
   104         }
   105 
   106         /// <summary>
   107         /// Determines whether the specified point is undefined.
   108         /// </summary>
   109         /// <param name="point">
   110         /// The point.
   111         /// </param>
   112         /// <returns>
   113         /// <c>true</c> if the specified point is undefined; otherwise, <c>false</c> .
   114         /// </returns>
   115         public static bool IsUndefined(ScreenPoint point)
   116         {
   117             return double.IsNaN(point.X) && double.IsNaN(point.Y);
   118         }
   119 
   120         /// <summary>
   121         /// Gets the distance to the specified point.
   122         /// </summary>
   123         /// <param name="point">
   124         /// The point.
   125         /// </param>
   126         /// <returns>
   127         /// The distance.
   128         /// </returns>
   129         public double DistanceTo(ScreenPoint point)
   130         {
   131             double dx = point.x - this.x;
   132             double dy = point.y - this.y;
   133             return Math.Sqrt((dx * dx) + (dy * dy));
   134         }
   135 
   136         /// <summary>
   137         /// Gets the squared distance to the specified point.
   138         /// </summary>
   139         /// <param name="point">
   140         /// The point.
   141         /// </param>
   142         /// <returns>
   143         /// The squared distance.
   144         /// </returns>
   145         public double DistanceToSquared(ScreenPoint point)
   146         {
   147             double dx = point.x - this.x;
   148             double dy = point.y - this.y;
   149             return (dx * dx) + (dy * dy);
   150         }
   151 
   152         /// <summary>
   153         /// Returns a <see cref="System.String"/> that represents this instance.
   154         /// </summary>
   155         /// <returns>
   156         /// A <see cref="System.String"/> that represents this instance.
   157         /// </returns>
   158         public override string ToString()
   159         {
   160             return this.x + " " + this.y;
   161         }
   162 
   163         /// <summary>
   164         /// Translates a <see cref="ScreenPoint"/> by a <see cref="ScreenVector"/>.
   165         /// </summary>
   166         /// <param name="p1"> The point. </param>
   167         /// <param name="p2"> The vector. </param>
   168         /// <returns> The translated point. </returns>
   169         public static ScreenPoint operator +(ScreenPoint p1, ScreenVector p2)
   170         {
   171             return new ScreenPoint(p1.x + p2.x, p1.y + p2.y);
   172         }
   173 
   174         /// <summary>
   175         /// Subtracts a <see cref="ScreenPoint"/> from a <see cref="ScreenPoint"/>
   176         /// and returns the result as a <see cref="ScreenVector"/>.
   177         /// </summary>
   178         /// <param name="p1"> The point on which to perform the subtraction. </param>
   179         /// <param name="p2"> The point to subtract from p1. </param>
   180         /// <returns> A <see cref="ScreenVector"/> structure that represents the difference between p1 and p2. </returns>
   181         public static ScreenVector operator -(ScreenPoint p1, ScreenPoint p2)
   182         {
   183             return new ScreenVector(p1.x - p2.x, p1.y - p2.y);
   184         }
   185 
   186         /// <summary>
   187         /// Subtracts a <see cref="ScreenVector"/> from a <see cref="ScreenPoint"/> 
   188         /// and returns the result as a <see cref="ScreenPoint"/>.
   189         /// </summary>
   190         /// <param name="point"> The point on which to perform the subtraction. </param>
   191         /// <param name="vector"> The vector to subtract from p1. </param>
   192         /// <returns> A <see cref="ScreenPoint"/> that represents point translated by the negative vector. </returns>
   193         public static ScreenPoint operator -(ScreenPoint point, ScreenVector vector)
   194         {
   195             return new ScreenPoint(point.x - vector.x, point.y - vector.y);
   196         }
   197     }
   198 }