//============================================================================
//PointPairBase Class
//Copyright ?2006 Jerry Vos & John Champion
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//=============================================================================
using System;
using System.Drawing;
using System.Runtime.Serialization;
using System.Security.Permissions;
using IComparer = System.Collections.IComparer;
namespace DrawGraph
{
///
/// This is a base class that provides base-level functionality for a data point consisting
/// of an (X,Y) pair of double values.
///
///
/// This class is typically a base class for actual type implementations.
///
///
/// Jerry Vos modified by John Champion
/// $Revision: 1.4 $ $Date: 2007/04/16 00:03:02 $
[Serializable]
public class PointPairBase : ISerializable
{
#region Member variables
///
/// Missing values are represented internally using .
///
public const double Missing = Double.MaxValue;
///
/// The default format to be used for displaying point values via the
/// method.
///
public const string DefaultFormat = "G";
///
/// This PointPair's X coordinate
///
public double X;
///
/// This PointPair's Y coordinate
///
public double Y;
#endregion
#region Constructors
///
/// Default Constructor
///
public PointPairBase()
: this( 0, 0 )
{
}
///
/// Creates a point pair with the specified X and Y.
///
/// This pair's x coordinate.
/// This pair's y coordinate.
public PointPairBase( double x, double y )
{
this.X = x;
this.Y = y;
}
///
/// Creates a point pair from the specified struct.
///
/// The struct from which to get the
/// new values.
public PointPairBase( PointF pt )
: this( pt.X, pt.Y )
{
}
///
/// The PointPairBase copy constructor.
///
/// The basis for the copy.
public PointPairBase( PointPairBase rhs )
{
this.X = rhs.X;
this.Y = rhs.Y;
}
#endregion
#region Serialization
///
/// Current schema value that defines the version of the serialized file
///
public const int schema = 11;
///
/// Constructor for deserializing objects
///
/// A instance that defines the serialized data
///
/// A instance that contains the serialized data
///
protected PointPairBase( SerializationInfo info, StreamingContext context )
{
// The schema value is just a file version parameter. You can use it to make future versions
// backwards compatible as new member variables are added to classes
int sch = info.GetInt32( "schema" );
X = info.GetDouble( "X" );
Y = info.GetDouble( "Y" );
}
///
/// Populates a instance with the data needed to serialize the target object
///
/// A instance that defines the serialized data
/// A instance that contains the serialized data
[SecurityPermissionAttribute( SecurityAction.Demand, SerializationFormatter = true )]
public virtual void GetObjectData( SerializationInfo info, StreamingContext context )
{
info.AddValue( "schema", schema );
info.AddValue( "X", X );
info.AddValue( "Y", Y );
}
#endregion
#region Properties
///
/// Readonly value that determines if either the X or the Y
/// coordinate in this PointPair is a missing value.
///
/// true if either value is missing
public bool IsMissing
{
get { return this.X == PointPairBase.Missing || this.Y == PointPairBase.Missing; }
}
///
/// Readonly value that determines if either the X or the Y
/// coordinate in this PointPair is an invalid (not plotable) value.
/// It is considered invalid if it is missing (equal to System.Double.Max),
/// Infinity, or NaN.
///
/// true if either value is invalid
public bool IsInvalid
{
get
{
return this.X == PointPairBase.Missing ||
this.Y == PointPairBase.Missing ||
Double.IsInfinity( this.X ) ||
Double.IsInfinity( this.Y ) ||
Double.IsNaN( this.X ) ||
Double.IsNaN( this.Y );
}
}
///
/// static method to determine if the specified point value is invalid.
///
/// The value is considered invalid if it is ,
/// ,
/// or .
/// The value to be checked for validity.
/// true if the value is invalid, false otherwise
public static bool IsValueInvalid( double value )
{
return ( value == PointPairBase.Missing ||
Double.IsInfinity( value ) ||
Double.IsNaN( value ) );
}
#endregion
#region Operator Overloads
///
/// Implicit conversion from PointPair to PointF. Note that this conversion
/// can result in data loss, since the data are being cast from a type
/// double (64 bit) to a float (32 bit).
///
/// The PointPair struct on which to operate
/// A PointF struct equivalent to the PointPair
public static implicit operator PointF( PointPairBase pair )
{
return new PointF( (float)pair.X, (float)pair.Y );
}
#endregion
#region Methods
///
/// Compare two objects for equality. To be equal, X and Y
/// must be exactly the same between the two objects.
///
/// The object to be compared with.
/// true if the objects are equal, false otherwise
public override bool Equals( object obj )
{
PointPairBase rhs = obj as PointPairBase;
return this.X == rhs.X && this.Y == rhs.Y;
}
///
/// Return the HashCode from the base class.
///
///
public override int GetHashCode()
{
return base.GetHashCode();
}
///
/// Format this PointPair value using the default format. Example: "( 12.345, -16.876 )".
/// The two double values are formatted with the "g" format type.
///
/// A string representation of the PointPair
public override string ToString()
{
return this.ToString( PointPairBase.DefaultFormat );
}
///
/// Format this PointPair value using a general format string.
/// Example: a format string of "e2" would give "( 1.23e+001, -1.69e+001 )".
///
/// A format string that will be used to format each of
/// the two double type values (see ).
/// A string representation of the PointPair
public string ToString( string format )
{
return "( " + this.X.ToString( format ) +
", " + this.Y.ToString( format ) +
" )";
}
///
/// Format this PointPair value using different general format strings for the X and Y values.
/// Example: a format string of "e2" would give "( 1.23e+001, -1.69e+001 )".
/// The Z value is not displayed (see ).
///
/// A format string that will be used to format the X
/// double type value (see ).
/// A format string that will be used to format the Y
/// double type value (see ).
/// A string representation of the PointPair
public string ToString( string formatX, string formatY )
{
return "( " + this.X.ToString( formatX ) +
", " + this.Y.ToString( formatY ) +
" )";
}
#endregion
}
}