implements efficient equality comparators
parent
c0f7ca3dff
commit
c68427eba7
|
@ -3,7 +3,7 @@ version: 2.1
|
||||||
defaults: &defaults
|
defaults: &defaults
|
||||||
working_directory: ~/repo
|
working_directory: ~/repo
|
||||||
docker:
|
docker:
|
||||||
- image: mcr.microsoft.com/dotnet/core/sdk:2.2
|
- image: mcr.microsoft.com/dotnet/core/sdk:3.0
|
||||||
environment:
|
environment:
|
||||||
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
|
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
|
||||||
DOTNET_CLI_TELEMETRY_OPTOUT: 1
|
DOTNET_CLI_TELEMETRY_OPTOUT: 1
|
||||||
|
|
|
@ -3,7 +3,7 @@ using Microsoft.Xna.Framework;
|
||||||
|
|
||||||
namespace MoonTools.Core.Structs
|
namespace MoonTools.Core.Structs
|
||||||
{
|
{
|
||||||
public struct Position2D
|
public struct Position2D : System.IEquatable<Position2D>
|
||||||
{
|
{
|
||||||
private Vector2 remainder;
|
private Vector2 remainder;
|
||||||
|
|
||||||
|
@ -146,5 +146,37 @@ namespace MoonTools.Core.Structs
|
||||||
{
|
{
|
||||||
return (int)Math.Floor(value);
|
return (int)Math.Floor(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override bool Equals(object other)
|
||||||
|
{
|
||||||
|
if (other is Position2D otherPosition)
|
||||||
|
{
|
||||||
|
return Equals(otherPosition);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Equals(Position2D other)
|
||||||
|
{
|
||||||
|
return
|
||||||
|
X == other.X &&
|
||||||
|
Y == other.Y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
return X.GetHashCode() ^ Y.GetHashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator ==(Position2D a, Position2D b)
|
||||||
|
{
|
||||||
|
return a.Equals(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator !=(Position2D a, Position2D b)
|
||||||
|
{
|
||||||
|
return !(a == b);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -58,13 +58,16 @@ namespace MoonTools.Core.Structs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Transform2D DefaultTransform {
|
public static Transform2D DefaultTransform
|
||||||
get {
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
return _defaultTransform;
|
return _defaultTransform;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Transform2D(Position2D position) {
|
public Transform2D(Position2D position)
|
||||||
|
{
|
||||||
_position = position;
|
_position = position;
|
||||||
_rotation = 0f;
|
_rotation = 0f;
|
||||||
_scale = new Vector2(1, 1);
|
_scale = new Vector2(1, 1);
|
||||||
|
@ -79,7 +82,8 @@ namespace MoonTools.Core.Structs
|
||||||
TransformMatrix = CreateTransformMatrix(_position, _rotation, _scale);
|
TransformMatrix = CreateTransformMatrix(_position, _rotation, _scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Transform2D(Position2D position, float rotation) {
|
public Transform2D(Position2D position, float rotation)
|
||||||
|
{
|
||||||
_position = position;
|
_position = position;
|
||||||
_rotation = rotation;
|
_rotation = rotation;
|
||||||
_scale = new Vector2(1, 1);
|
_scale = new Vector2(1, 1);
|
||||||
|
@ -94,27 +98,25 @@ namespace MoonTools.Core.Structs
|
||||||
TransformMatrix = CreateTransformMatrix(_position, _rotation, _scale);
|
TransformMatrix = CreateTransformMatrix(_position, _rotation, _scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Transform2D(Position2D position, float rotation, Vector2 scale) {
|
public Transform2D(Position2D position, float rotation, Vector2 scale)
|
||||||
|
{
|
||||||
_position = position;
|
_position = position;
|
||||||
_rotation = rotation;
|
_rotation = rotation;
|
||||||
_scale = scale;
|
_scale = scale;
|
||||||
TransformMatrix = CreateTransformMatrix(_position, _rotation, _scale);
|
TransformMatrix = CreateTransformMatrix(_position, _rotation, _scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Transform2D(Vector2 position, float rotation, Vector2 scale) {
|
public Transform2D(Vector2 position, float rotation, Vector2 scale)
|
||||||
|
{
|
||||||
_position = new Position2D(position.X, position.Y);
|
_position = new Position2D(position.X, position.Y);
|
||||||
_rotation = rotation;
|
_rotation = rotation;
|
||||||
_scale = scale;
|
_scale = scale;
|
||||||
TransformMatrix = CreateTransformMatrix(_position, _rotation, _scale);
|
TransformMatrix = CreateTransformMatrix(_position, _rotation, _scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Transform2D Compose(Transform2D other) {
|
public Transform2D Compose(Transform2D other)
|
||||||
return new Transform2D(Position + other.Position, Rotation + other.Rotation, Scale * other.Scale);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Equals(Transform2D other)
|
|
||||||
{
|
{
|
||||||
return TransformMatrix == other.TransformMatrix;
|
return new Transform2D(Position + other.Position, Rotation + other.Rotation, Scale * other.Scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Matrix CreateTransformMatrix(Position2D translation, float rotationDegrees, Vector2 scale)
|
private static Matrix CreateTransformMatrix(Position2D translation, float rotationDegrees, Vector2 scale)
|
||||||
|
@ -123,5 +125,35 @@ namespace MoonTools.Core.Structs
|
||||||
Matrix.CreateRotationZ(Microsoft.Xna.Framework.MathHelper.ToRadians(rotationDegrees)) *
|
Matrix.CreateRotationZ(Microsoft.Xna.Framework.MathHelper.ToRadians(rotationDegrees)) *
|
||||||
Matrix.CreateTranslation(translation.X, translation.Y, 0);
|
Matrix.CreateTranslation(translation.X, translation.Y, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override bool Equals(Object other)
|
||||||
|
{
|
||||||
|
if (other is Transform2D otherTransform)
|
||||||
|
{
|
||||||
|
return Equals(otherTransform);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Equals(Transform2D other)
|
||||||
|
{
|
||||||
|
return TransformMatrix == other.TransformMatrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
return TransformMatrix.GetHashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator ==(Transform2D a, Transform2D b)
|
||||||
|
{
|
||||||
|
return a.Equals(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator !=(Transform2D a, Transform2D b)
|
||||||
|
{
|
||||||
|
return !(a == b);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,5 +61,41 @@ namespace Tests
|
||||||
result.X.Should().BeApproximately(1.2f, 0.01f);
|
result.X.Should().BeApproximately(1.2f, 0.01f);
|
||||||
result.Y.Should().BeApproximately(6.4f, 0.01f);
|
result.Y.Should().BeApproximately(6.4f, 0.01f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Equals()
|
||||||
|
{
|
||||||
|
var one = new Position2D(5.2f, 4.7f);
|
||||||
|
var two = new Position2D(5.1f, 4.4f);
|
||||||
|
|
||||||
|
one.Equals(two).Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void EqualsOperator()
|
||||||
|
{
|
||||||
|
var one = new Position2D(5.2f, 4.7f);
|
||||||
|
var two = new Position2D(5.1f, 4.4f);
|
||||||
|
|
||||||
|
(one == two).Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void NotEquals()
|
||||||
|
{
|
||||||
|
var one = new Position2D(5.2f, 4.7f);
|
||||||
|
var two = new Position2D(3.1f, 2.4f);
|
||||||
|
|
||||||
|
one.Equals(two).Should().BeFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void NotEqualsOperator()
|
||||||
|
{
|
||||||
|
var one = new Position2D(5.2f, 4.7f);
|
||||||
|
var two = new Position2D(3.1f, 2.4f);
|
||||||
|
|
||||||
|
(one != two).Should().BeTrue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -25,6 +25,24 @@ namespace Tests
|
||||||
transformA.Should().NotBeEquivalentTo(transformB);
|
transformA.Should().NotBeEquivalentTo(transformB);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void EqualsOperator()
|
||||||
|
{
|
||||||
|
var transformA = new Transform2D(new Position2D(0, 1), 4f, new Vector2(2, 1));
|
||||||
|
var transformB = new Transform2D(new Position2D(0, 1), 4f, new Vector2(2, 1));
|
||||||
|
|
||||||
|
(transformA == transformB).Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void NotEqualsOperator()
|
||||||
|
{
|
||||||
|
var transformA = new Transform2D(new Position2D(2, 3));
|
||||||
|
var transformB = new Transform2D(new Position2D(5, 1));
|
||||||
|
|
||||||
|
(transformA != transformB).Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void Compose()
|
public void Compose()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue