Merge pull request #22 from cubrr/enhance-vector-structs

Expand vectors structs, separate Coordinates into diff files
This commit is contained in:
Drew DeVault 2015-02-07 11:12:17 -07:00
commit dfdf175451
6 changed files with 449 additions and 233 deletions

View File

@ -192,5 +192,13 @@ namespace TrueCraft.API
{
get { return Max.Z - Min.Z; }
}
public Vector3 Center
{
get
{
return (this.Min + this.Max) / 2;
}
}
}
}

View File

@ -0,0 +1,218 @@
using System;
namespace TrueCraft.API
{
public struct Coordinates2D : IEquatable<Coordinates2D>
{
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;
}
/// <summary>
/// Converts this Coordinates2D to a string in the format &lt;x,z&gt;.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return string.Format("<{0},{1}>", X, Z);
}
#region Math
/// <summary>
/// Calculates the distance between two Coordinates2D objects.
/// </summary>
public double DistanceTo(Coordinates2D other)
{
return Math.Sqrt(Square(other.X - X) +
Square(other.Z - Z));
}
/// <summary>
/// Calculates the square of a num.
/// </summary>
private int Square(int num)
{
return num * num;
}
/// <summary>
/// Finds the distance of this Coordinates2D from Coordinates2D.Zero
/// </summary>
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;
}
}
}
}

View File

@ -1,221 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TrueCraft.API
{
public struct Coordinates2D : IEquatable<Coordinates2D>
{
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;
}
/// <summary>
/// Converts this Coordinates2D to a string in the format &lt;x, z&gt;.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return string.Format("<{0},{1}>", X, Z);
}
#region Math
/// <summary>
/// Calculates the distance between two Coordinates2D objects.
/// </summary>
public double DistanceTo(Coordinates2D other)
{
return Math.Sqrt(Square(other.X - X) +
Square(other.Z - Z));
}
/// <summary>
/// Calculates the square of a num.
/// </summary>
private int Square(int num)
{
return num * num;
}
/// <summary>
/// Finds the distance of this Coordinates2D from Coordinates2D.Zero
/// </summary>
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<Coordinates3D>
{
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

View File

@ -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,
@ -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,16 +176,158 @@ 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<double, double, double> 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)
{
@ -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;
}
}
/// <summary>
/// Returns a string representing the <see cref="Size"/> object in the format of &lt;Width,Height,Depth&gt;.
/// </summary>
/// <returns>A string representation of the object</returns>
/// <inheritdoc cref="Object.ToString"/>
public override string ToString()
{
return string.Format("<{0},{1},{2}>", Width, Height, Depth);

View File

@ -31,6 +31,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ArmourMaterial.cs" />
<Compile Include="Coordinates3D.cs" />
<Compile Include="ToolMaterial.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Networking\IPacket.cs" />
@ -40,7 +41,7 @@
<Compile Include="Difficulty.cs" />
<Compile Include="Dimension.cs" />
<Compile Include="BoundingBox.cs" />
<Compile Include="Coordinates.cs" />
<Compile Include="Coordinates2D.cs" />
<Compile Include="ItemStack.cs" />
<Compile Include="MetadataByte.cs" />
<Compile Include="MetadataDictionary.cs" />

View File

@ -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()