Guest User

Point3D

a guest
Jun 13th, 2011
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.71 KB | None | 0 0
  1. /// <summary>
  2.     ///   Represents a point in 3 dimensional space
  3.     /// </summary>
  4.     public class Point3D : IEquatable<Point3D>
  5.     {
  6.         #region Public Properties
  7.  
  8.         public float X { get; set; }
  9.  
  10.         public float Y { get; set; }
  11.  
  12.         public float Z { get; set; }
  13.  
  14.         /// <summary>
  15.         ///   The zero value for a 3D point
  16.         /// </summary>
  17.         public static Point3D Zero
  18.         {
  19.             get
  20.             {
  21.                 return new Point3D(0, 0, 0);
  22.             }
  23.         }
  24.  
  25.         #endregion
  26.  
  27.         public Point3D(float x = 0f, float y = 0f, float z = 0f)
  28.         {
  29.             X = x;
  30.             Y = y;
  31.             Z = z;
  32.         }
  33.  
  34.         #region IEquatable<Point3D> Members
  35.  
  36.         public bool Equals(Point3D other)
  37.         {
  38.             if (ReferenceEquals(null, other))
  39.             {
  40.                 return false;
  41.             }
  42.             if (ReferenceEquals(this, other))
  43.             {
  44.                 return true;
  45.             }
  46.             return other.X.Equals(X) && other.Y.Equals(Y) && other.Z.Equals(Z);
  47.         }
  48.  
  49.         #endregion
  50.  
  51.         #region Public Methods
  52.  
  53.         /// <summary>
  54.         ///   Calculate the distance between two 3D points
  55.         /// </summary>
  56.         public static float DistanceBetween(Point3D a, Point3D b)
  57.         {
  58.             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));
  59.         }
  60.  
  61.         /// <summary>
  62.         ///   Calculate the distance between this point and another point
  63.         /// </summary>
  64.         public float DistanceTo(Point3D Point)
  65.         {
  66.             return DistanceBetween(this, Point);
  67.         }
  68.  
  69.         /// <summary>
  70.         ///   Converts the Point3D into an XElement, used for saving points
  71.         /// </summary>
  72.         public XElement ToXml()
  73.         {
  74.             return new XElement("Point", new XAttribute("X", X), new XAttribute("Y", Y), new XAttribute("Z", Z));
  75.         }
  76.  
  77.         #endregion
  78.  
  79.         #region Overridden Methods
  80.  
  81.         public override bool Equals(object obj)
  82.         {
  83.             if (ReferenceEquals(null, obj))
  84.             {
  85.                 return false;
  86.             }
  87.             if (ReferenceEquals(this, obj))
  88.             {
  89.                 return true;
  90.             }
  91.             if (obj.GetType() != typeof(Point3D))
  92.             {
  93.                 return false;
  94.             }
  95.             return Equals((Point3D)obj);
  96.         }
  97.  
  98.         public override int GetHashCode()
  99.         {
  100.             unchecked
  101.             {
  102.                 int result = X.GetHashCode();
  103.                 result = (result * 397) ^ Y.GetHashCode();
  104.                 result = (result * 397) ^ Z.GetHashCode();
  105.                 return result;
  106.             }
  107.         }
  108.  
  109.         public override string ToString()
  110.         {
  111.             return string.Format("Point: X={0}, Y={1}, Z={2}", X, Y, Z);
  112.         }
  113.  
  114.         #endregion
  115.  
  116.         #region Operators
  117.  
  118.         /// <summary>
  119.         ///   Determine if two points are equal
  120.         /// </summary>
  121.         public static bool operator ==(Point3D p1, Point3D p2)
  122.         {
  123.             return (p1.X == p2.X && p1.Y == p2.Y && p1.Z == p2.Z);
  124.         }
  125.  
  126.         /// <summary>
  127.         ///   Allows us to implicitly cast a 3D Point to a 2D Point
  128.         /// </summary>
  129.         public static implicit operator Point2D(Point3D point)
  130.         {
  131.             return new Point2D(point.X, point.Y);
  132.         }
  133.  
  134.         /// <summary>
  135.         ///   Determine if two points are not equal
  136.         /// </summary>
  137.         public static bool operator !=(Point3D p1, Point3D p2)
  138.         {
  139.             return !(p1 == p2);
  140.         }
  141.  
  142.         #endregion
  143.     }
Advertisement
Add Comment
Please, Sign In to add comment