optimize simplex equality

generics
Evan Hemsley 2020-01-01 20:14:47 -08:00
parent 2dca5f716c
commit d553c2e213
2 changed files with 24 additions and 9 deletions

View File

@ -1,5 +1,4 @@
using System.Linq;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Numerics;
using MoonTools.Core.Structs;
using System;
@ -80,11 +79,15 @@ namespace MoonTools.Core.Bonk
public bool Equals(Simplex2D other)
{
var q = from a in Vertices
join b in other.Vertices on a equals b
select a;
if (Count != other.Count) { return false; }
return Count == other.Count && q.Count() == Count;
return
(A == other.A && B == other.B && C == other.C) ||
(A == other.A && B == other.C && C == other.B) ||
(A == other.B && B == other.A && C == other.C) ||
(A == other.B && B == other.C && C == other.A) ||
(A == other.C && B == other.A && C == other.B) ||
(A == other.C && B == other.B && C == other.A);
}
public override int GetHashCode()

View File

@ -455,14 +455,26 @@ namespace Tests
public void TwoSimplexEquals()
{
var simplexA = new Simplex2D(Vector2.One, Vector2.Zero, Vector2.UnitX);
var simplexB = new Simplex2D(Vector2.One, Vector2.Zero, Vector2.UnitX);
simplexA.Equals(simplexB).Should().BeTrue();
var simplexC = new Simplex2D(Vector2.One, Vector2.Zero, Vector2.UnitX);
var simplexD = new Simplex2D(Vector2.Zero, Vector2.One, Vector2.UnitX);
var simplexC = new Simplex2D(Vector2.Zero, Vector2.One, Vector2.UnitX);
simplexC.Equals(simplexD).Should().BeTrue();
simplexA.Equals(simplexC).Should().BeTrue();
var simplexD = new Simplex2D(Vector2.UnitX, Vector2.Zero, Vector2.One);
simplexA.Equals(simplexD).Should().BeTrue();
var simplexE = new Simplex2D(Vector2.One, Vector2.UnitX, Vector2.Zero);
simplexA.Equals(simplexE).Should().BeTrue();
var simplexF = new Simplex2D(Vector2.Zero, Vector2.UnitX, Vector2.One);
simplexA.Equals(simplexF).Should().BeTrue();
}
[Test]