External/OxyPlot/OxyPlot/Foundation/ScreenPoint.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).
moel@391
     1
// --------------------------------------------------------------------------------------------------------------------
moel@391
     2
// <copyright file="ScreenPoint.cs" company="OxyPlot">
moel@391
     3
//   The MIT License (MIT)
moel@391
     4
//
moel@391
     5
//   Copyright (c) 2012 Oystein Bjorke
moel@391
     6
//
moel@391
     7
//   Permission is hereby granted, free of charge, to any person obtaining a
moel@391
     8
//   copy of this software and associated documentation files (the
moel@391
     9
//   "Software"), to deal in the Software without restriction, including
moel@391
    10
//   without limitation the rights to use, copy, modify, merge, publish,
moel@391
    11
//   distribute, sublicense, and/or sell copies of the Software, and to
moel@391
    12
//   permit persons to whom the Software is furnished to do so, subject to
moel@391
    13
//   the following conditions:
moel@391
    14
//
moel@391
    15
//   The above copyright notice and this permission notice shall be included
moel@391
    16
//   in all copies or substantial portions of the Software.
moel@391
    17
//
moel@391
    18
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
moel@391
    19
//   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
moel@391
    20
//   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
moel@391
    21
//   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
moel@391
    22
//   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
moel@391
    23
//   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
moel@391
    24
//   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
moel@391
    25
// </copyright>
moel@391
    26
// <summary>
moel@391
    27
//   Describes a point defined in the screen coordinate system.
moel@391
    28
// </summary>
moel@391
    29
// --------------------------------------------------------------------------------------------------------------------
moel@391
    30
namespace OxyPlot
moel@391
    31
{
moel@391
    32
    using System;
moel@391
    33
moel@391
    34
    /// <summary>
moel@391
    35
    /// Represents a point defined in the screen coordinate system.
moel@391
    36
    /// </summary>
moel@391
    37
    /// <remarks>
moel@391
    38
    /// The rendering methods transforms <see cref="DataPoint"/>s to <see cref="ScreenPoint"/>s.
moel@391
    39
    /// </remarks>
moel@391
    40
    public struct ScreenPoint
moel@391
    41
    {
moel@391
    42
        /// <summary>
moel@391
    43
        /// The undefined point.
moel@391
    44
        /// </summary>
moel@391
    45
        public static readonly ScreenPoint Undefined = new ScreenPoint(double.NaN, double.NaN);
moel@391
    46
moel@391
    47
        /// <summary>
moel@391
    48
        /// The x-coordinate.
moel@391
    49
        /// </summary>
moel@391
    50
        internal double x;
moel@391
    51
moel@391
    52
        /// <summary>
moel@391
    53
        /// The y-coordinate.
moel@391
    54
        /// </summary>
moel@391
    55
        internal double y;
moel@391
    56
moel@391
    57
        /// <summary>
moel@391
    58
        /// Initializes a new instance of the <see cref="ScreenPoint"/> struct.
moel@391
    59
        /// </summary>
moel@391
    60
        /// <param name="x">
moel@391
    61
        /// The x-coordinate.
moel@391
    62
        /// </param>
moel@391
    63
        /// <param name="y">
moel@391
    64
        /// The y-coordinate.
moel@391
    65
        /// </param>
moel@391
    66
        public ScreenPoint(double x, double y)
moel@391
    67
        {
moel@391
    68
            this.x = x;
moel@391
    69
            this.y = y;
moel@391
    70
        }
moel@391
    71
moel@391
    72
        /// <summary>
moel@391
    73
        /// Gets or sets the x-coordinate.
moel@391
    74
        /// </summary>
moel@391
    75
        /// <value> The x-coordinate. </value>
moel@391
    76
        public double X
moel@391
    77
        {
moel@391
    78
            get
moel@391
    79
            {
moel@391
    80
                return this.x;
moel@391
    81
            }
moel@391
    82
moel@391
    83
            set
moel@391
    84
            {
moel@391
    85
                this.x = value;
moel@391
    86
            }
moel@391
    87
        }
moel@391
    88
moel@391
    89
        /// <summary>
moel@391
    90
        /// Gets or sets the y-coordinate.
moel@391
    91
        /// </summary>
moel@391
    92
        /// <value> The y-coordinate. </value>
moel@391
    93
        public double Y
moel@391
    94
        {
moel@391
    95
            get
moel@391
    96
            {
moel@391
    97
                return this.y;
moel@391
    98
            }
moel@391
    99
moel@391
   100
            set
moel@391
   101
            {
moel@391
   102
                this.y = value;
moel@391
   103
            }
moel@391
   104
        }
moel@391
   105
moel@391
   106
        /// <summary>
moel@391
   107
        /// Determines whether the specified point is undefined.
moel@391
   108
        /// </summary>
moel@391
   109
        /// <param name="point">
moel@391
   110
        /// The point.
moel@391
   111
        /// </param>
moel@391
   112
        /// <returns>
moel@391
   113
        /// <c>true</c> if the specified point is undefined; otherwise, <c>false</c> .
moel@391
   114
        /// </returns>
moel@391
   115
        public static bool IsUndefined(ScreenPoint point)
moel@391
   116
        {
moel@391
   117
            return double.IsNaN(point.X) && double.IsNaN(point.Y);
moel@391
   118
        }
moel@391
   119
moel@391
   120
        /// <summary>
moel@391
   121
        /// Gets the distance to the specified point.
moel@391
   122
        /// </summary>
moel@391
   123
        /// <param name="point">
moel@391
   124
        /// The point.
moel@391
   125
        /// </param>
moel@391
   126
        /// <returns>
moel@391
   127
        /// The distance.
moel@391
   128
        /// </returns>
moel@391
   129
        public double DistanceTo(ScreenPoint point)
moel@391
   130
        {
moel@391
   131
            double dx = point.x - this.x;
moel@391
   132
            double dy = point.y - this.y;
moel@391
   133
            return Math.Sqrt((dx * dx) + (dy * dy));
moel@391
   134
        }
moel@391
   135
moel@391
   136
        /// <summary>
moel@391
   137
        /// Gets the squared distance to the specified point.
moel@391
   138
        /// </summary>
moel@391
   139
        /// <param name="point">
moel@391
   140
        /// The point.
moel@391
   141
        /// </param>
moel@391
   142
        /// <returns>
moel@391
   143
        /// The squared distance.
moel@391
   144
        /// </returns>
moel@391
   145
        public double DistanceToSquared(ScreenPoint point)
moel@391
   146
        {
moel@391
   147
            double dx = point.x - this.x;
moel@391
   148
            double dy = point.y - this.y;
moel@391
   149
            return (dx * dx) + (dy * dy);
moel@391
   150
        }
moel@391
   151
moel@391
   152
        /// <summary>
moel@391
   153
        /// Returns a <see cref="System.String"/> that represents this instance.
moel@391
   154
        /// </summary>
moel@391
   155
        /// <returns>
moel@391
   156
        /// A <see cref="System.String"/> that represents this instance.
moel@391
   157
        /// </returns>
moel@391
   158
        public override string ToString()
moel@391
   159
        {
moel@391
   160
            return this.x + " " + this.y;
moel@391
   161
        }
moel@391
   162
moel@391
   163
        /// <summary>
moel@391
   164
        /// Translates a <see cref="ScreenPoint"/> by a <see cref="ScreenVector"/>.
moel@391
   165
        /// </summary>
moel@391
   166
        /// <param name="p1"> The point. </param>
moel@391
   167
        /// <param name="p2"> The vector. </param>
moel@391
   168
        /// <returns> The translated point. </returns>
moel@391
   169
        public static ScreenPoint operator +(ScreenPoint p1, ScreenVector p2)
moel@391
   170
        {
moel@391
   171
            return new ScreenPoint(p1.x + p2.x, p1.y + p2.y);
moel@391
   172
        }
moel@391
   173
moel@391
   174
        /// <summary>
moel@391
   175
        /// Subtracts a <see cref="ScreenPoint"/> from a <see cref="ScreenPoint"/>
moel@391
   176
        /// and returns the result as a <see cref="ScreenVector"/>.
moel@391
   177
        /// </summary>
moel@391
   178
        /// <param name="p1"> The point on which to perform the subtraction. </param>
moel@391
   179
        /// <param name="p2"> The point to subtract from p1. </param>
moel@391
   180
        /// <returns> A <see cref="ScreenVector"/> structure that represents the difference between p1 and p2. </returns>
moel@391
   181
        public static ScreenVector operator -(ScreenPoint p1, ScreenPoint p2)
moel@391
   182
        {
moel@391
   183
            return new ScreenVector(p1.x - p2.x, p1.y - p2.y);
moel@391
   184
        }
moel@391
   185
moel@391
   186
        /// <summary>
moel@391
   187
        /// Subtracts a <see cref="ScreenVector"/> from a <see cref="ScreenPoint"/> 
moel@391
   188
        /// and returns the result as a <see cref="ScreenPoint"/>.
moel@391
   189
        /// </summary>
moel@391
   190
        /// <param name="point"> The point on which to perform the subtraction. </param>
moel@391
   191
        /// <param name="vector"> The vector to subtract from p1. </param>
moel@391
   192
        /// <returns> A <see cref="ScreenPoint"/> that represents point translated by the negative vector. </returns>
moel@391
   193
        public static ScreenPoint operator -(ScreenPoint point, ScreenVector vector)
moel@391
   194
        {
moel@391
   195
            return new ScreenPoint(point.x - vector.x, point.y - vector.y);
moel@391
   196
        }
moel@391
   197
    }
moel@391
   198
}