|
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is WCPierce Web Controls.
*
* The Initial Developer of the Original Code is William C. Pierce.
* Portions created by the Initial Developer are Copyright (C) 2005
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
using System;
using System.Xml.Serialization;
namespace WCPierce.Web.UI.WebControls
{
/// <summary>
/// A GPoint represents a single, 2-dimensional coordinate. If a GPoint
/// represents a lat/lng, x is the longitude, and y is the latitude in decimal
/// notation.
/// </summary>
[Serializable()]
public class GPoint
{
private float _x = 0F;
private float _y = 0F;
/// <summary>
/// Default constructor required for XmlSerialization
/// </summary>
public GPoint() { }
/// <summary>
/// Creates a new point with the given coordinate values
/// </summary>
/// <param name="X"></param>
/// <param name="Y"></param>
public GPoint(float X, float Y)
{
_x = X;
_y = Y;
}
/// <summary>
/// The x (or horizontal) coordinate of the point
/// </summary>
[XmlAttribute()]
public float X
{
get { return _x; }
set { _x = value; }
}
/// <summary>
/// The y (or vertical) coordinate of the point
/// </summary>
[XmlAttribute()]
public float Y
{
get { return _y; }
set { _y = value; }
}
/// <summary>
/// Overloaded Equals to determine if two GPoints are indentical in X/Y coordinates
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType()) return false;
GPoint p = (GPoint)obj;
return (_x == p.X) && (_y == p.Y);
}
/// <summary>
/// Convenience operator
/// </summary>
/// <param name="gp1"></param>
/// <param name="gp2"></param>
/// <returns></returns>
public static bool operator ==(GPoint gp1, GPoint gp2)
{
return gp1.Equals(gp2);
}
/// <summary>
/// Convenience operator
/// </summary>
/// <param name="gp1"></param>
/// <param name="gp2"></param>
/// <returns></returns>
public static bool operator !=(GPoint gp1, GPoint gp2)
{
return (gp1.X != gp2.X) || (gp1.Y != gp2.Y);
}
/// <summary>
/// Used to uniquely identify this GPoint
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return _x.GetHashCode() ^ _y.GetHashCode();
}
/// <summary>
/// Ouputs a GPoint in (X,Y) format
/// </summary>
/// <returns></returns>
public override string ToString()
{
return String.Format("({0},{1})", _x, _y);
}
/// <summary>
/// Convert a GPoint to PointF to take advantage of PointF methods
/// </summary>
/// <returns>A PointF representation of the GPoint</returns>
public System.Drawing.PointF ToPointF()
{
return new System.Drawing.PointF(_x, _y);
}
/// <summary>
/// Convenicence function for converting from "X,Y" or "(X,Y)" to GBounds
/// </summary>
/// <param name="Point">A string in "X,Y" or "(X,Y)" format</param>
/// <returns></returns>
public static explicit operator GPoint(string Point)
{
try
{
string val = Point.Replace("(", String.Empty).Replace(")", String.Empty);
string[] vals = val.Split(',');
return new GPoint(Convert.ToSingle(vals[0], Utilities.UsCulture), Convert.ToSingle(vals[1], Utilities.UsCulture));
}
catch( Exception e )
{
throw new InvalidCastException(String.Format("Could not convert string \"{0}\" to GPoint", Point), e);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.