Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Represents a point in 3 dimensional space
- /// </summary>
- public class Point3D : IEquatable<Point3D>
- {
- #region Public Properties
- public float X { get; set; }
- public float Y { get; set; }
- public float Z { get; set; }
- /// <summary>
- /// The zero value for a 3D point
- /// </summary>
- public static Point3D Zero
- {
- get
- {
- return new Point3D(0, 0, 0);
- }
- }
- #endregion
- public Point3D(float x = 0f, float y = 0f, float z = 0f)
- {
- X = x;
- Y = y;
- Z = z;
- }
- #region IEquatable<Point3D> Members
- public bool Equals(Point3D other)
- {
- if (ReferenceEquals(null, other))
- {
- return false;
- }
- if (ReferenceEquals(this, other))
- {
- return true;
- }
- return other.X.Equals(X) && other.Y.Equals(Y) && other.Z.Equals(Z);
- }
- #endregion
- #region Public Methods
- /// <summary>
- /// Calculate the distance between two 3D points
- /// </summary>
- public static float DistanceBetween(Point3D a, Point3D b)
- {
- return (float)Math.Sqrt(Math.Pow(a.X - b.X, 2) + Math.Pow(a.Y - b.Y, 2) + Math.Pow(a.Z - b.Z, 2));
- }
- /// <summary>
- /// Calculate the distance between this point and another point
- /// </summary>
- public float DistanceTo(Point3D Point)
- {
- return DistanceBetween(this, Point);
- }
- /// <summary>
- /// Converts the Point3D into an XElement, used for saving points
- /// </summary>
- public XElement ToXml()
- {
- return new XElement("Point", new XAttribute("X", X), new XAttribute("Y", Y), new XAttribute("Z", Z));
- }
- #endregion
- #region Overridden Methods
- public override bool Equals(object obj)
- {
- if (ReferenceEquals(null, obj))
- {
- return false;
- }
- if (ReferenceEquals(this, obj))
- {
- return true;
- }
- if (obj.GetType() != typeof(Point3D))
- {
- return false;
- }
- return Equals((Point3D)obj);
- }
- public override int GetHashCode()
- {
- unchecked
- {
- int result = X.GetHashCode();
- result = (result * 397) ^ Y.GetHashCode();
- result = (result * 397) ^ Z.GetHashCode();
- return result;
- }
- }
- public override string ToString()
- {
- return string.Format("Point: X={0}, Y={1}, Z={2}", X, Y, Z);
- }
- #endregion
- #region Operators
- /// <summary>
- /// Determine if two points are equal
- /// </summary>
- public static bool operator ==(Point3D p1, Point3D p2)
- {
- return (p1.X == p2.X && p1.Y == p2.Y && p1.Z == p2.Z);
- }
- /// <summary>
- /// Allows us to implicitly cast a 3D Point to a 2D Point
- /// </summary>
- public static implicit operator Point2D(Point3D point)
- {
- return new Point2D(point.X, point.Y);
- }
- /// <summary>
- /// Determine if two points are not equal
- /// </summary>
- public static bool operator !=(Point3D p1, Point3D p2)
- {
- return !(p1 == p2);
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment