From e940734b1b29e0571c50d3bf01b22f9ed1ec6961 Mon Sep 17 00:00:00 2001 From: Cuber Date: Wed, 4 Feb 2015 17:00:13 +0200 Subject: [PATCH] Expand vectors structs, separate Coordinates into diff files --- TrueCraft.API/BoundingBox.cs | 8 + TrueCraft.API/Coordinates2D.cs | 218 ++++++++++++++++ .../{Coordinates.cs => Coordinates3D.cs} | 235 ++---------------- TrueCraft.API/Size.cs | 195 ++++++++++++++- TrueCraft.API/TrueCraft.API.csproj | 3 +- TrueCraft.API/Vector3.cs | 23 +- 6 files changed, 449 insertions(+), 233 deletions(-) create mode 100644 TrueCraft.API/Coordinates2D.cs rename TrueCraft.API/{Coordinates.cs => Coordinates3D.cs} (52%) diff --git a/TrueCraft.API/BoundingBox.cs b/TrueCraft.API/BoundingBox.cs index ae08522..59f8b5a 100644 --- a/TrueCraft.API/BoundingBox.cs +++ b/TrueCraft.API/BoundingBox.cs @@ -192,5 +192,13 @@ namespace TrueCraft.API { get { return Max.Z - Min.Z; } } + + public Vector3 Center + { + get + { + return (this.Min + this.Max) / 2; + } + } } } diff --git a/TrueCraft.API/Coordinates2D.cs b/TrueCraft.API/Coordinates2D.cs new file mode 100644 index 0000000..1689c19 --- /dev/null +++ b/TrueCraft.API/Coordinates2D.cs @@ -0,0 +1,218 @@ +using System; + +namespace TrueCraft.API +{ + public struct Coordinates2D : IEquatable + { + public int X, Z; + + public Coordinates2D(int value) + { + X = Z = value; + } + + public Coordinates2D(int x, int z) + { + X = x; + Z = z; + } + + public Coordinates2D(Coordinates2D v) + { + X = v.X; + Z = v.Z; + } + + /// + /// Converts this Coordinates2D to a string in the format <x,z>. + /// + /// + public override string ToString() + { + return string.Format("<{0},{1}>", X, Z); + } + + #region Math + + /// + /// Calculates the distance between two Coordinates2D objects. + /// + public double DistanceTo(Coordinates2D other) + { + return Math.Sqrt(Square(other.X - X) + + Square(other.Z - Z)); + } + + /// + /// Calculates the square of a num. + /// + private int Square(int num) + { + return num * num; + } + + /// + /// Finds the distance of this Coordinates2D from Coordinates2D.Zero + /// + public double Distance + { + get + { + return DistanceTo(Zero); + } + } + + public static Coordinates2D Min(Coordinates2D value1, Coordinates2D value2) + { + return new Coordinates2D( + Math.Min(value1.X, value2.X), + Math.Min(value1.Z, value2.Z) + ); + } + + public static Coordinates2D Max(Coordinates2D value1, Coordinates2D value2) + { + return new Coordinates2D( + Math.Max(value1.X, value2.X), + Math.Max(value1.Z, value2.Z) + ); + } + + #endregion + + #region Operators + + public static bool operator !=(Coordinates2D a, Coordinates2D b) + { + return !a.Equals(b); + } + + public static bool operator ==(Coordinates2D a, Coordinates2D b) + { + return a.Equals(b); + } + + public static Coordinates2D operator +(Coordinates2D a, Coordinates2D b) + { + return new Coordinates2D(a.X + b.X, a.Z + b.Z); + } + + public static Coordinates2D operator -(Coordinates2D a, Coordinates2D b) + { + return new Coordinates2D(a.X - b.X, a.Z - b.Z); + } + + public static Coordinates2D operator -(Coordinates2D a) + { + return new Coordinates2D( + -a.X, + -a.Z); + } + + public static Coordinates2D operator *(Coordinates2D a, Coordinates2D b) + { + return new Coordinates2D(a.X * b.X, a.Z * b.Z); + } + + public static Coordinates2D operator /(Coordinates2D a, Coordinates2D b) + { + return new Coordinates2D(a.X / b.X, a.Z / b.Z); + } + + public static Coordinates2D operator %(Coordinates2D a, Coordinates2D b) + { + return new Coordinates2D(a.X % b.X, a.Z % b.Z); + } + + public static Coordinates2D operator +(Coordinates2D a, int b) + { + return new Coordinates2D(a.X + b, a.Z + b); + } + + public static Coordinates2D operator -(Coordinates2D a, int b) + { + return new Coordinates2D(a.X - b, a.Z - b); + } + + public static Coordinates2D operator *(Coordinates2D a, int b) + { + return new Coordinates2D(a.X * b, a.Z * b); + } + + public static Coordinates2D operator /(Coordinates2D a, int b) + { + return new Coordinates2D(a.X / b, a.Z / b); + } + + public static Coordinates2D operator %(Coordinates2D a, int b) + { + return new Coordinates2D(a.X % b, a.Z % b); + } + + public static Coordinates2D operator +(int a, Coordinates2D b) + { + return new Coordinates2D(a + b.X, a + b.Z); + } + + public static Coordinates2D operator -(int a, Coordinates2D b) + { + return new Coordinates2D(a - b.X, a - b.Z); + } + + public static Coordinates2D operator *(int a, Coordinates2D b) + { + return new Coordinates2D(a * b.X, a * b.Z); + } + + public static Coordinates2D operator /(int a, Coordinates2D b) + { + return new Coordinates2D(a / b.X, a / b.Z); + } + + public static Coordinates2D operator %(int a, Coordinates2D b) + { + return new Coordinates2D(a % b.X, a % b.Z); + } + + public static explicit operator Coordinates2D(Coordinates3D a) + { + return new Coordinates2D(a.X, a.Z); + } + + #endregion + + #region Constants + + public static readonly Coordinates2D Zero = new Coordinates2D(0); + public static readonly Coordinates2D One = new Coordinates2D(1); + + public static readonly Coordinates2D Forward = new Coordinates2D(0, 1); + public static readonly Coordinates2D Backward = new Coordinates2D(0, -1); + public static readonly Coordinates2D Left = new Coordinates2D(-1, 0); + public static readonly Coordinates2D Right = new Coordinates2D(1, 0); + + #endregion + + public bool Equals(Coordinates2D other) + { + return other.X.Equals(X) && other.Z.Equals(Z); + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + if (obj.GetType() != typeof(Coordinates2D)) return false; + return Equals((Coordinates2D)obj); + } + + public override int GetHashCode() + { + unchecked + { + int result = X.GetHashCode(); + result = (result * 397) ^ Z.GetHashCode(); + return result; + } + } + } +} \ No newline at end of file diff --git a/TrueCraft.API/Coordinates.cs b/TrueCraft.API/Coordinates3D.cs similarity index 52% rename from TrueCraft.API/Coordinates.cs rename to TrueCraft.API/Coordinates3D.cs index 05216e9..84d62a2 100644 --- a/TrueCraft.API/Coordinates.cs +++ b/TrueCraft.API/Coordinates3D.cs @@ -1,221 +1,10 @@ using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; namespace TrueCraft.API { - public struct Coordinates2D : IEquatable - { - public int X, Z; - - public Coordinates2D(int value) - { - X = Z = value; - } - - public Coordinates2D(int x, int z) - { - X = x; - Z = z; - } - - public Coordinates2D(Coordinates2D v) - { - X = v.X; - Z = v.Z; - } - - /// - /// Converts this Coordinates2D to a string in the format <x, z>. - /// - /// - public override string ToString() - { - return string.Format("<{0},{1}>", X, Z); - } - - #region Math - - /// - /// Calculates the distance between two Coordinates2D objects. - /// - public double DistanceTo(Coordinates2D other) - { - return Math.Sqrt(Square(other.X - X) + - Square(other.Z - Z)); - } - - /// - /// Calculates the square of a num. - /// - private int Square(int num) - { - return num * num; - } - - /// - /// Finds the distance of this Coordinates2D from Coordinates2D.Zero - /// - public double Distance - { - get - { - return DistanceTo(Zero); - } - } - - public static Coordinates2D Min(Coordinates2D value1, Coordinates2D value2) - { - return new Coordinates2D( - Math.Min(value1.X, value2.X), - Math.Min(value1.Z, value2.Z) - ); - } - - public static Coordinates2D Max(Coordinates2D value1, Coordinates2D value2) - { - return new Coordinates2D( - Math.Max(value1.X, value2.X), - Math.Max(value1.Z, value2.Z) - ); - } - - #endregion - - #region Operators - - public static bool operator !=(Coordinates2D a, Coordinates2D b) - { - return !a.Equals(b); - } - - public static bool operator ==(Coordinates2D a, Coordinates2D b) - { - return a.Equals(b); - } - - public static Coordinates2D operator +(Coordinates2D a, Coordinates2D b) - { - return new Coordinates2D(a.X + b.X, a.Z + b.Z); - } - - public static Coordinates2D operator -(Coordinates2D a, Coordinates2D b) - { - return new Coordinates2D(a.X - b.X, a.Z - b.Z); - } - - public static Coordinates2D operator -(Coordinates2D a) - { - return new Coordinates2D( - -a.X, - -a.Z); - } - - public static Coordinates2D operator *(Coordinates2D a, Coordinates2D b) - { - return new Coordinates2D(a.X * b.X, a.Z * b.Z); - } - - public static Coordinates2D operator /(Coordinates2D a, Coordinates2D b) - { - return new Coordinates2D(a.X / b.X, a.Z / b.Z); - } - - public static Coordinates2D operator %(Coordinates2D a, Coordinates2D b) - { - return new Coordinates2D(a.X % b.X, a.Z % b.Z); - } - - public static Coordinates2D operator +(Coordinates2D a, int b) - { - return new Coordinates2D(a.X + b, a.Z + b); - } - - public static Coordinates2D operator -(Coordinates2D a, int b) - { - return new Coordinates2D(a.X - b, a.Z - b); - } - - public static Coordinates2D operator *(Coordinates2D a, int b) - { - return new Coordinates2D(a.X * b, a.Z * b); - } - - public static Coordinates2D operator /(Coordinates2D a, int b) - { - return new Coordinates2D(a.X / b, a.Z / b); - } - - public static Coordinates2D operator %(Coordinates2D a, int b) - { - return new Coordinates2D(a.X % b, a.Z % b); - } - - public static Coordinates2D operator +(int a, Coordinates2D b) - { - return new Coordinates2D(a + b.X, a + b.Z); - } - - public static Coordinates2D operator -(int a, Coordinates2D b) - { - return new Coordinates2D(a - b.X, a - b.Z); - } - - public static Coordinates2D operator *(int a, Coordinates2D b) - { - return new Coordinates2D(a * b.X, a * b.Z); - } - - public static Coordinates2D operator /(int a, Coordinates2D b) - { - return new Coordinates2D(a / b.X, a / b.Z); - } - - public static Coordinates2D operator %(int a, Coordinates2D b) - { - return new Coordinates2D(a % b.X, a % b.Z); - } - - public static explicit operator Coordinates2D(Coordinates3D a) - { - return new Coordinates2D(a.X, a.Z); - } - - #endregion - - #region Constants - - public static readonly Coordinates2D Zero = new Coordinates2D(0); - public static readonly Coordinates2D One = new Coordinates2D(1); - - public static readonly Coordinates2D Forward = new Coordinates2D(0, 1); - public static readonly Coordinates2D Backward = new Coordinates2D(0, -1); - public static readonly Coordinates2D Left = new Coordinates2D(-1, 0); - public static readonly Coordinates2D Right = new Coordinates2D(1, 0); - - #endregion - - public bool Equals(Coordinates2D other) - { - return other.X.Equals(X) && other.Z.Equals(Z); - } - - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - if (obj.GetType() != typeof(Coordinates2D)) return false; - return Equals((Coordinates2D)obj); - } - - public override int GetHashCode() - { - unchecked - { - int result = X.GetHashCode(); - result = (result * 397) ^ Z.GetHashCode(); - return result; - } - } - } - public struct Coordinates3D : IEquatable { public int X, Y, Z; @@ -391,19 +180,27 @@ namespace TrueCraft.API return new Coordinates3D(a % b.X, a % b.Y, a % b.Z); } + #endregion + + #region Conversion operators + public static explicit operator Coordinates3D(Coordinates2D a) { return new Coordinates3D(a.X, 0, a.Z); } - public static implicit operator Coordinates3D(Vector3 a) + public static explicit operator Coordinates3D(Vector3 a) { - return new Coordinates3D((int)a.X, (int)a.Y, (int)a.Z); + return new Coordinates3D((int)a.X, + (int)a.Y, + (int)a.Z); } - public static implicit operator Vector3(Coordinates3D a) + public static explicit operator Coordinates3D(Size s) { - return new Vector3(a.X, a.Y, a.Z); + return new Coordinates3D((int)s.Width, + (int)s.Height, + (int)s.Depth); } #endregion @@ -450,4 +247,4 @@ namespace TrueCraft.API } } } -} \ No newline at end of file +} diff --git a/TrueCraft.API/Size.cs b/TrueCraft.API/Size.cs index ff2ad04..11d7efa 100644 --- a/TrueCraft.API/Size.cs +++ b/TrueCraft.API/Size.cs @@ -17,6 +17,11 @@ namespace TrueCraft.API public double Depth; #region Constructors + + public Size(double d) + { + this.Width = this.Height = this.Depth = d; + } public Size(double width, double height, double depth) { this.Width = width; @@ -30,9 +35,11 @@ namespace TrueCraft.API this.Height = s.Height; this.Depth = s.Depth; } + #endregion #region Operators + public static Size operator /(Size a, double b) { return new Size(a.Width / b, @@ -70,9 +77,9 @@ namespace TrueCraft.API public static Size operator /(double a, Size b) { - return new Size(a/b.Width, - a/b.Height, - a/b.Depth); + return new Size(a / b.Width, + a / b.Height, + a / b.Depth); } public static Size operator *(double a, Size b) @@ -138,6 +145,30 @@ namespace TrueCraft.API a.Depth - b.Depth); } + public static Size operator -(Size a) + { + return new Size(-a.Width, -a.Height, -a.Depth); + } + + public static Size operator +(Size a) + { + return new Size(a); + } + + public static Size operator ++(Size a) + { + return new Size(a.Width++, + a.Height++, + a.Depth++); + } + + public static Size operator --(Size a) + { + return new Size(a.Width--, + a.Height--, + a.Depth--); + } + public static bool operator ==(Size a, Size b) { return a.Equals(b); @@ -145,17 +176,159 @@ namespace TrueCraft.API public static bool operator !=(Size a, Size b) { - return !(a.Equals(b)); + return !a.Equals(b); } + public static bool operator >(Size a, Size b) + { + return a.Volume > b.Volume; + } + + public static bool operator <(Size a, Size b) + { + return a.Volume < b.Volume; + } + + public static bool operator >=(Size a, Size b) + { + return a.Volume >= b.Volume; + } + + public static bool operator <=(Size a, Size b) + { + return a.Volume <= b.Volume; + } + + #endregion + + #region Conversion operators + public static implicit operator Size(Vector3 v) { return new Size(v.X, v.Y, v.Z); } + + public static implicit operator Size(Coordinates3D c) + { + return new Size(c.X, c.Y, c.Z); + } + + + public static explicit operator Size(Coordinates2D c) + { + return new Size(c.X, 0, c.Z); + } + + public static explicit operator Size(Tuple t) + { + return new Size(t.Item1, + t.Item2, + t.Item3); + } + #endregion - // TODO: Create math methods - + #region Math methods + + public static Size Min(Size a, Size b) + { + return new Size(Math.Min(a.Width, b.Width), + Math.Min(a.Height, b.Height), + Math.Min(a.Depth, b.Depth)); + } + + public Size Min(Size b) + { + return new Size(Math.Min(this.Width, b.Width), + Math.Min(this.Height, b.Height), + Math.Min(this.Depth, b.Depth)); + } + + public static Size Max(Size a, Size b) + { + return new Size(Math.Max(a.Width, b.Width), + Math.Max(a.Height, b.Height), + Math.Max(a.Depth, b.Depth)); + } + + public Size Max(Size b) + { + return new Size(Math.Max(this.Width, b.Width), + Math.Max(this.Height, b.Height), + Math.Max(this.Depth, b.Depth)); + } + + public static Size Negate(Size a) + { + return -a; + } + + public Size Negate() + { + return -this; + } + + public static Size Abs(Size a) + { + return new Size(Math.Abs(a.Width), + Math.Abs(a.Height), + Math.Abs(a.Depth)); + } + + public Size Abs() + { + return new Size(Math.Abs(this.Width), + Math.Abs(this.Height), + Math.Abs(this.Depth)); + } + + #endregion + + public double Volume + { + get + { + return Width * Height * Depth; + } + } + + public double SurfaceArea + { + get + { + return 2 * (Width * Depth + + Width * Height + + Depth * Height); + } + } + + public double LateralSurfaceArea + { + get + { + return 2 * (Width * Depth + + Depth * Height); + } + } + + public double Diagonal + { + get + { + return Math.Sqrt(Width * Width + + Height * Height + + Depth * Depth); + } + } + + public double Average + { + get + { + return (Width + Height + Depth) / 3; + } + } + public bool Equals(Size other) { return this.Width == other.Width && @@ -165,9 +338,7 @@ namespace TrueCraft.API public override bool Equals(object obj) { - if (obj is Size) - return Equals((Size) obj); - return false; + return obj is Size && Equals((Size)obj); } public override int GetHashCode() @@ -181,6 +352,12 @@ namespace TrueCraft.API return hash; } } + + /// + /// Returns a string representing the object in the format of <Width,Height,Depth>. + /// + /// A string representation of the object + /// public override string ToString() { return string.Format("<{0},{1},{2}>", Width, Height, Depth); diff --git a/TrueCraft.API/TrueCraft.API.csproj b/TrueCraft.API/TrueCraft.API.csproj index 0207950..901b0cc 100644 --- a/TrueCraft.API/TrueCraft.API.csproj +++ b/TrueCraft.API/TrueCraft.API.csproj @@ -31,6 +31,7 @@ + @@ -40,7 +41,7 @@ - + diff --git a/TrueCraft.API/Vector3.cs b/TrueCraft.API/Vector3.cs index ac50ce5..f95cf5e 100644 --- a/TrueCraft.API/Vector3.cs +++ b/TrueCraft.API/Vector3.cs @@ -89,7 +89,7 @@ namespace TrueCraft.API /// /// Finds the distance of this vector from Vector3.Zero /// - public double Distance + public double Distance { get { @@ -266,6 +266,23 @@ namespace TrueCraft.API #endregion + #region Conversion operators + public static implicit operator Vector3(Coordinates3D a) + { + return new Vector3(a.X, a.Y, a.Z); + } + + public static explicit operator Vector3(Coordinates2D c) + { + return new Vector3(c.X, 0, c.Z); + } + + public static implicit operator Vector3(Size s) + { + return new Vector3(s.Width, s.Height, s.Depth); + } + #endregion + #region Constants public static readonly Vector3 Zero = new Vector3(0); @@ -292,9 +309,7 @@ namespace TrueCraft.API public override bool Equals(object obj) { - if (obj is Vector3) - return Equals((Vector3)obj); - return false; + return obj is Vector3 && Equals((Vector3)obj); } public override int GetHashCode()