Implemented Size3 and Point3. Fixed bug in Point2. (#466)

* Implemented Size3 and Point3.  Fixed bug in Point2.  Documentation listed Point2, but code took in Vector2.  Changed to Point2

* Adding missed project references
This commit is contained in:
Mason11987
2018-02-07 20:44:02 +10:00
committed by Dylan Wilson
parent 5470a52e05
commit bcfb64fe35
4 changed files with 671 additions and 1 deletions
@@ -46,6 +46,7 @@
<Compile Include="FloatHelper.cs" />
<Compile Include="HslColor.cs" />
<Compile Include="IRectangular.cs" />
<Compile Include="Point3.cs" />
<Compile Include="Serialization\BaseTypeJsonConverter.cs" />
<Compile Include="Serialization\HslColorJsonConverter.cs" />
<Compile Include="Serialization\JsonReaderExtensions.cs" />
@@ -53,6 +54,7 @@
<Compile Include="Serialization\ShortNameJsonContractResolver.cs" />
<Compile Include="Serialization\NinePatchRegion2DJsonConverter.cs" />
<Compile Include="Serialization\ThicknessJsonConverter.cs" />
<Compile Include="Size3.cs" />
<Compile Include="Sprites\ISpriteBatchDrawable.cs" />
<Compile Include="Thickness.cs" />
<Compile Include="BoundingRectangle.cs" />
+1 -1
View File
@@ -320,7 +320,7 @@ namespace MonoGame.Extended
/// The the <see cref="Point2" /> that contains the maximal coordinate values from two <see cref="Point2" />
/// structures.
/// </returns>
public static Point2 Maximum(Vector2 first, Vector2 second)
public static Point2 Maximum(Point2 first, Point2 second)
{
return new Point2(first.X > second.X ? first.X : second.X,
first.Y > second.Y ? first.Y : second.Y);
+379
View File
@@ -0,0 +1,379 @@
using System;
using System.Diagnostics;
using Microsoft.Xna.Framework;
namespace MonoGame.Extended
{
/// <summary>
/// A three-dimensional point defined by a 3-tuple of real numbers, (x, y, z).
/// </summary>
/// <remarks>
/// <para>
/// A point is a position in three-dimensional space, the location of which is described in terms of a
/// three-dimensional coordinate system, given by a reference point, called the origin, and three coordinate axes.
/// </para>
/// <para>
/// A common three-dimensional coordinate system is the Cartesian (or rectangular) coordinate system where the
/// coordinate axes, conventionally denoted the X axis, Y axis and Z axis, are perpindicular to each other.
/// </para>
/// </remarks>
/// <seealso cref="IEquatable{T}" />
/// <seealso cref="IEquatableByRef{Point3}" />
[DebuggerDisplay("{DebugDisplayString,nq}")]
public struct Point3 : IEquatable<Point3>, IEquatableByRef<Point3>
{
/// <summary>
/// Returns a <see cref="Point3" /> with <see cref="X" /> <see cref="Y" /> and <see cref="Z" /> equal to <c>0.0f</c>.
/// </summary>
public static readonly Point3 Zero = new Point3();
/// <summary>
/// Returns a <see cref="Point3" /> with <see cref="X" /> <see cref="Y" /> and <see cref="Z" /> set to not a number.
/// </summary>
public static readonly Point3 NaN = new Point3(float.NaN, float.NaN, float.NaN);
/// <summary>
/// The x-coordinate of this <see cref="Point3" />.
/// </summary>
public float X;
/// <summary>
/// The y-coordinate of this <see cref="Point3" />.
/// </summary>
public float Y;
/// <summary>
/// The z-coordinate of this <see cref="Point3" />.
/// </summary>
public float Z;
/// <summary>
/// Initializes a new instance of the <see cref="Point3" /> structure from the specified coordinates.
/// </summary>
/// <param name="x">The x-coordinate.</param>
/// <param name="y">The y-coordinate.</param>
/// <param name="z">The z-coordinate.</param>
public Point3(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
/// <summary>
/// Compares two <see cref="Point3" /> structures. The result specifies
/// whether the values of the <see cref="X" /> <see cref="Y" /> and <see cref="Z" />
/// fields of the two <see cref="Point3" />
/// structures are equal.
/// </summary>
/// <param name="first">The first point.</param>
/// <param name="second">The second point.</param>
/// <returns>
/// <c>true</c> if the <see cref="X" /> <see cref="Y" /> and <see cref="Z" />
/// fields of the two <see cref="Point3" />
/// structures are equal; otherwise, <c>false</c>.
/// </returns>
public static bool operator ==(Point3 first, Point3 second)
{
return first.Equals(ref second);
}
/// <summary>
/// Indicates whether this <see cref="Point3" /> is equal to another <see cref="Point3" />.
/// </summary>
/// <param name="point">The point.</param>
/// <returns>
/// <c>true</c> if this <see cref="Point3" /> is equal to the <paramref name="point" />; otherwise,
/// <c>false</c>.
/// </returns>
public bool Equals(Point3 point)
{
return Equals(ref point);
}
/// <summary>
/// Indicates whether this <see cref="Point3" /> is equal to another <see cref="Point3" />.
/// </summary>
/// <param name="point">The point.</param>
/// <returns>
/// <c>true</c> if this <see cref="Point3" /> is equal to the <paramref name="point" /> parameter; otherwise,
/// <c>false</c>.
/// </returns>
public bool Equals(ref Point3 point)
{
// ReSharper disable CompareOfFloatsByEqualityOperator
return (point.X == X) && (point.Y == Y) && (point.Z == Z);
// ReSharper restore CompareOfFloatsByEqualityOperator
}
/// <summary>
/// Returns a value indicating whether this <see cref="Point3" /> is equal to a specified object.
/// </summary>
/// <param name="obj">The object to make the comparison with.</param>
/// <returns>
/// <c>true</c> if this <see cref="Point3" /> is equal to <paramref name="obj" />; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
if (obj is Point3)
return Equals((Point3) obj);
return false;
}
/// <summary>
/// Compares two <see cref="Point3" /> structures. The result specifies
/// whether the values of the <see cref="X" /> <see cref="Y" /> or <see cref="Z" />
/// fields of the two <see cref="Point3" />
/// structures are unequal.
/// </summary>
/// <param name="first">The first point.</param>
/// <param name="second">The second point.</param>
/// <returns>
/// <c>true</c> if the <see cref="X" /> <see cref="Y" /> or <see cref="Z" />
/// fields of the two <see cref="Point3" />
/// structures are unequal; otherwise, <c>false</c>.
/// </returns>
public static bool operator !=(Point3 first, Point3 second)
{
return !(first == second);
}
/// <summary>
/// Calculates the <see cref="Point3" /> representing the addition of a <see cref="Point3" /> and a
/// <see cref="Vector2" />.
/// </summary>
/// <param name="point">The point.</param>
/// <param name="vector">The vector.</param>
/// <returns>
/// The <see cref="Point3" /> representing the addition of a <see cref="Point3" /> and a <see cref="Vector3" />.
/// </returns>
public static Point3 operator +(Point3 point, Vector3 vector)
{
return Add(point, vector);
}
/// <summary>
/// Calculates the <see cref="Point3" /> representing the addition of a <see cref="Point3" /> and a
/// <see cref="Vector3" />.
/// </summary>
/// <param name="point">The point.</param>
/// <param name="vector">The vector.</param>
/// <returns>
/// The <see cref="Point3" /> representing the addition of a <see cref="Point3" /> and a <see cref="Vector3" />.
/// </returns>
public static Point3 Add(Point3 point, Vector3 vector)
{
Point3 p;
p.X = point.X + vector.X;
p.Y = point.Y + vector.Y;
p.Z = point.Z + vector.Z;
return p;
}
/// <summary>
/// Calculates the <see cref="Point3" /> representing the subtraction of a <see cref="Point3" /> and a
/// <see cref="Vector3" />.
/// </summary>
/// <param name="point">The point.</param>
/// <param name="vector">The vector.</param>
/// <returns>
/// The <see cref="Point3" /> representing the substraction of a <see cref="Point3" /> and a <see cref="Vector3" />.
/// </returns>
public static Point3 operator -(Point3 point, Vector3 vector)
{
return Subtract(point, vector);
}
/// <summary>
/// Calculates the <see cref="Point3" /> representing the addition of a <see cref="Point3" /> and a
/// <see cref="Vector3" />.
/// </summary>
/// <param name="point">The point.</param>
/// <param name="vector">The vector.</param>
/// <returns>
/// The <see cref="Point3" /> representing the substraction of a <see cref="Point3" /> and a <see cref="Vector3" />.
/// </returns>
public static Point3 Subtract(Point3 point, Vector3 vector)
{
Point3 p;
p.X = point.X - vector.X;
p.Y = point.Y - vector.Y;
p.Z = point.Z - vector.Z;
return p;
}
/// <summary>
/// Calculates the <see cref="Vector3" /> representing the displacement of two <see cref="Point3" /> structures.
/// </summary>
/// <param name="point2">The second point.</param>
/// <param name="point1">The first point.</param>
/// <returns>
/// The <see cref="Vector3" /> representing the displacement of two <see cref="Point3" /> structures.
/// </returns>
public static Vector3 operator -(Point3 point1, Point3 point2)
{
return Displacement(point1, point2);
}
/// <summary>
/// Calculates the <see cref="Vector3" /> representing the displacement of two <see cref="Point3" /> structures.
/// </summary>
/// <param name="point2">The second point.</param>
/// <param name="point1">The first point.</param>
/// <returns>
/// The <see cref="Vector3" /> representing the displacement of two <see cref="Point3" /> structures.
/// </returns>
public static Vector3 Displacement(Point3 point2, Point3 point1)
{
Vector3 vector;
vector.X = point2.X - point1.X;
vector.Y = point2.Y - point1.Y;
vector.Z = point2.Z - point1.Z;
return vector;
}
/// <summary>
/// Translates a <see cref='Point3' /> by a given <see cref='Size3' />.
/// </summary>
/// <param name="point">The point.</param>
/// <param name="size">The size.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Point3 operator +(Point3 point, Size3 size)
{
return Add(point, size);
}
/// <summary>
/// Translates a <see cref='Point3' /> by a given <see cref='Size3' />.
/// </summary>
/// <param name="point">The point.</param>
/// <param name="size">The size.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Point3 Add(Point3 point, Size3 size)
{
return new Point3(point.X + size.Width, point.Y + size.Height, point.Z + size.Depth);
}
/// <summary>
/// Translates a <see cref='Point3' /> by the negative of a given <see cref='Size3' />.
/// </summary>
/// <param name="point">The point.</param>
/// <param name="size">The size.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Point3 operator -(Point3 point, Size3 size)
{
return Subtract(point, size);
}
/// <summary>
/// Translates a <see cref='Point3' /> by the negative of a given <see cref='Size3' /> .
/// </summary>
/// <param name="point">The point.</param>
/// <param name="size">The size.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static Point3 Subtract(Point3 point, Size3 size)
{
return new Point3(point.X - size.Width, point.Y - size.Height, point.Z - size.Depth);
}
/// <summary>
/// Returns a hash code of this <see cref="Point3" /> suitable for use in hashing algorithms and data
/// structures like a hash table.
/// </summary>
/// <returns>
/// A hash code of this <see cref="Point3" />.
/// </returns>
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 23 + X.GetHashCode();
hash = hash * 23 + Y.GetHashCode();
hash = hash * 23 + Z.GetHashCode();
return hash;
}
}
/// <summary>
/// Calculates the <see cref="Point3" /> that contains the minimal coordinate values from two <see cref="Point3" />
/// structures.
/// structures.
/// </summary>
/// <param name="first">The first point.</param>
/// <param name="second">The second point.</param>
/// <returns>
/// The the <see cref="Point3" /> that contains the minimal coordinate values from two <see cref="Point3" />
/// structures.
/// </returns>
public static Point3 Minimum(Point3 first, Point3 second)
{
return new Point3(first.X < second.X ? first.X : second.X,
first.Y < second.Y ? first.Y : second.Y,
first.Z < second.Z ? first.Z : second.Z);
}
/// <summary>
/// Calculates the <see cref="Point3" /> that contains the maximal coordinate values from two <see cref="Point3" />
/// structures.
/// structures.
/// </summary>
/// <param name="first">The first point.</param>
/// <param name="second">The second point.</param>
/// <returns>
/// The the <see cref="Point3" /> that contains the maximal coordinate values from two <see cref="Point3" />
/// structures.
/// </returns>
public static Point3 Maximum(Point3 first, Point3 second)
{
return new Point3(first.X > second.X ? first.X : second.X,
first.Y > second.Y ? first.Y : second.Y,
first.Z > second.Z ? first.Z : second.Z);
}
/// <summary>
/// Performs an implicit conversion from a <see cref="Point3" /> to a position <see cref="Vector3" />.
/// </summary>
/// <param name="point">The point.</param>
/// <returns>
/// The resulting <see cref="Vector3" />.
/// </returns>
public static implicit operator Vector3(Point3 point)
{
return new Vector3(point.X, point.Y, point.Z);
}
/// <summary>
/// Performs an implicit conversion from a <see cref="Vector3" /> to a position <see cref="Point3" />.
/// </summary>
/// <param name="vector">The vector.</param>
/// <returns>
/// The resulting <see cref="Point3" />.
/// </returns>
public static implicit operator Point3(Vector3 vector)
{
return new Point3(vector.X, vector.Y, vector.Z);
}
/// <summary>
/// Returns a <see cref="string" /> that represents this <see cref="Point3" />.
/// </summary>
/// <returns>
/// A <see cref="string" /> that represents this <see cref="Point3" />.
/// </returns>
public override string ToString()
{
return $"({X}, {Y}, {Z})";
}
internal string DebugDisplayString => ToString();
}
}
+289
View File
@@ -0,0 +1,289 @@
using System;
using Microsoft.Xna.Framework;
namespace MonoGame.Extended
{
/// <summary>
/// A three dimensional size defined by two real numbers, a width a height and a depth.
/// </summary>
/// <remarks>
/// <para>
/// A size is a subspace of three-dimensional space, the area of which is described in terms of a three-dimensional
/// coordinate system, given by a reference point and three coordinate axes.
/// </para>
/// </remarks>
/// <seealso cref="IEquatable{T}" />
/// <seealso cref="IEquatableByRef{Size3}" />
public struct Size3 : IEquatable<Size3>, IEquatableByRef<Size3>
{
/// <summary>
/// Returns a <see cref="Size3" /> with <see cref="Width" /> <see cref="Height" /> and <see cref="Depth" /> equal to <c>0.0f</c>.
/// </summary>
public static readonly Size3 Empty = new Size3();
/// <summary>
/// The horizontal component of this <see cref="Size3" />.
/// </summary>
public float Width;
/// <summary>
/// The vertical component of this <see cref="Size3" />.
/// </summary>
public float Height;
/// <summary>
/// The vertical component of this <see cref="Size3" />.
/// </summary>
public float Depth;
/// <summary>
/// Gets a value that indicates whether this <see cref="Size3" /> is empty.
/// </summary>
// ReSharper disable CompareOfFloatsByEqualityOperator
public bool IsEmpty => (Width == 0) && (Height == 0) && (Depth == 0);
// ReSharper restore CompareOfFloatsByEqualityOperator
/// <summary>
/// Initializes a new instance of the <see cref="Size3" /> structure from the specified dimensions.
/// </summary>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="depth">The depth.</param>
public Size3(float width, float height, float depth)
{
Width = width;
Height = height;
Depth = depth;
}
/// <summary>
/// Compares two <see cref="Size3" /> structures. The result specifies
/// whether the values of the <see cref="Width" /> <see cref="Height" /> and <see cref="Depth" />
/// fields of the two <see cref="Point3" /> structures are equal.
/// </summary>
/// <param name="first">The first size.</param>
/// <param name="second">The second size.</param>
/// <returns>
/// <c>true</c> if the <see cref="Width" /> <see cref="Height" /> and <see cref="Depth" />
/// fields of the two <see cref="Point3" /> structures are equal; otherwise, <c>false</c>.
/// </returns>
public static bool operator ==(Size3 first, Size3 second)
{
return first.Equals(ref second);
}
/// <summary>
/// Indicates whether this <see cref="Size3" /> is equal to another <see cref="Size3" />.
/// </summary>
/// <param name="size">The size.</param>
/// <returns>
/// <c>true</c> if this <see cref="Point3" /> is equal to the <paramref name="size" /> parameter; otherwise,
/// <c>false</c>.
/// </returns>
public bool Equals(Size3 size)
{
return Equals(ref size);
}
/// <summary>
/// Indicates whether this <see cref="Size3" /> is equal to another <see cref="Size3" />.
/// </summary>
/// <param name="size">The size.</param>
/// <returns>
/// <c>true</c> if this <see cref="Point3" /> is equal to the <paramref name="size" />; otherwise,
/// <c>false</c>.
/// </returns>
public bool Equals(ref Size3 size)
{
// ReSharper disable CompareOfFloatsByEqualityOperator
return (Width == size.Width) && (Height == size.Height) && (Depth == size.Depth);
// ReSharper restore CompareOfFloatsByEqualityOperator
}
/// <summary>
/// Returns a value indicating whether this <see cref="Size3" /> is equal to a specified object.
/// </summary>
/// <param name="obj">The object to make the comparison with.</param>
/// <returns>
/// <c>true</c> if this <see cref="Size3" /> is equal to <paramref name="obj" />; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
if (obj is Size3)
return Equals((Size3) obj);
return false;
}
/// <summary>
/// Compares two <see cref="Size3" /> structures. The result specifies
/// whether the values of the <see cref="Width" /> <see cref="Height" /> or <see cref="Depth" />
/// fields of the two <see cref="Size3" /> structures are unequal.
/// </summary>
/// <param name="first">The first size.</param>
/// <param name="second">The second size.</param>
/// <returns>
/// <c>true</c> if the <see cref="Width" /> <see cref="Height" /> or <see cref="Depth" />
/// fields of the two <see cref="Size3" /> structures are unequal; otherwise, <c>false</c>.
/// </returns>
public static bool operator !=(Size3 first, Size3 second)
{
return !(first == second);
}
/// <summary>
/// Calculates the <see cref="Size3" /> representing the vector addition of two <see cref="Size3" /> structures as if
/// they were <see cref="Vector3" /> structures.
/// </summary>
/// <param name="first">The first size.</param>
/// <param name="second">The second size.</param>
/// <returns>
/// The <see cref="Size3" /> representing the vector addition of two <see cref="Size3" /> structures as if they
/// were <see cref="Vector3" /> structures.
/// </returns>
public static Size3 operator +(Size3 first, Size3 second)
{
return Add(first, second);
}
/// <summary>
/// Calculates the <see cref="Size3" /> representing the vector addition of two <see cref="Size3" /> structures.
/// </summary>
/// <param name="first">The first size.</param>
/// <param name="second">The second size.</param>
/// <returns>
/// The <see cref="Size3" /> representing the vector addition of two <see cref="Size3" /> structures.
/// </returns>
public static Size3 Add(Size3 first, Size3 second)
{
Size3 size;
size.Width = first.Width + second.Width;
size.Height = first.Height + second.Height;
size.Depth = first.Depth + second.Depth;
return size;
}
/// <summary>
/// Calculates the <see cref="Size3" /> representing the vector subtraction of two <see cref="Size3" /> structures.
/// </summary>
/// <param name="first">The first size.</param>
/// <param name="second">The second size.</param>
/// <returns>
/// The <see cref="Size3" /> representing the vector subtraction of two <see cref="Size3" /> structures.
/// </returns>
public static Size3 operator -(Size3 first, Size3 second)
{
return Subtract(first, second);
}
public static Size3 operator /(Size3 size, float value)
{
return new Size3(size.Width / value, size.Height / value, size.Depth / value);
}
public static Size3 operator *(Size3 size, float value)
{
return new Size3(size.Width * value, size.Height * value, size.Depth * value);
}
/// <summary>
/// Calculates the <see cref="Size3" /> representing the vector subtraction of two <see cref="Size3" /> structures.
/// </summary>
/// <param name="first">The first size.</param>
/// <param name="second">The second size.</param>
/// <returns>
/// The <see cref="Size3" /> representing the vector subtraction of two <see cref="Size3" /> structures.
/// </returns>
public static Size3 Subtract(Size3 first, Size3 second)
{
Size3 size;
size.Width = first.Width - second.Width;
size.Height = first.Height - second.Height;
size.Depth = first.Depth - second.Depth;
return size;
}
/// <summary>
/// Returns a hash code of this <see cref="Size3" /> suitable for use in hashing algorithms and data
/// structures like a hash table.
/// </summary>
/// <returns>
/// A hash code of this <see cref="Point3" />.
/// </returns>
public override int GetHashCode()
{
unchecked
{
unchecked
{
int hash = 17;
hash = hash * 23 + Width.GetHashCode();
hash = hash * 23 + Height.GetHashCode();
hash = hash * 23 + Depth.GetHashCode();
return hash;
}
}
}
/// <summary>
/// Performs an implicit conversion from a <see cref="Point3" /> to a <see cref="Size3" />.
/// </summary>
/// <param name="point">The point.</param>
/// <returns>
/// The resulting <see cref="Size3" />.
/// </returns>
public static implicit operator Size3(Point3 point)
{
return new Size3(point.X, point.Y, point.Z);
}
/// <summary>
/// Performs an implicit conversion from a <see cref="Point3" /> to a <see cref="Size3" />.
/// </summary>
/// <param name="size">The size.</param>
/// <returns>
/// The resulting <see cref="Point3" />.
/// </returns>
public static implicit operator Point3(Size3 size)
{
return new Point3(size.Width, size.Height, size.Depth);
}
/// <summary>
/// Performs an implicit conversion from a <see cref="Size3" /> to a <see cref="Vector3" />.
/// </summary>
/// <param name="size">The size.</param>
/// <returns>
/// The resulting <see cref="Vector3" />.
/// </returns>
public static implicit operator Vector3(Size3 size)
{
return new Vector3(size.Width, size.Height, size.Depth);
}
/// <summary>
/// Performs an implicit conversion from a <see cref="Vector3" /> to a <see cref="Size3" />.
/// </summary>
/// <param name="vector">The vector.</param>
/// <returns>
/// The resulting <see cref="Size3" />.
/// </returns>
public static implicit operator Size3(Vector3 vector)
{
return new Size3(vector.X, vector.Y, vector.Z);
}
/// <summary>
/// Returns a <see cref="string" /> that represents this <see cref="Size3" />.
/// </summary>
/// <returns>
/// A <see cref="string" /> that represents this <see cref="Size3" />.
/// </returns>
public override string ToString()
{
return $"Width: {Width}, Height: {Height}, Depth: {Depth}";
}
internal string DebugDisplayString => ToString();
}
}