public void V2_Equals() { Vector2 a = Vector2.UnitX; Vector2 b = Vector2.UnitX * 1.000001f; Vector2X.Equals(a, b, 0.0001f).Should().BeTrue(); }
public void V2_RotateEquals() { Vector2 a = new Vector2(3, 4); Vector2 b = a.RotateDegrees(90); var expected = new Vector2(4, -3); Vector2X.Equals(b, expected).Should().BeTrue(); }
public void Line_SegmentIntersectionLowSymmetry() { var result = LineAlgorithms.LineSegmentIntersection( new Vector2(704, 336), new Vector2(569, 299), new Vector2(436, 218), new Vector2(446, 357)); result.Should().NotBeNull(); Vector2X.Equals(result.IntersectionPoint, new Vector2(439.269f, 263.444f), 1e-3f).Should().BeTrue( "Intersection was not in correct position."); }
private static void ProjectionTest(Vector2 v, Vector2 direction, Vector2 expected) { Vector2 result = v.ProjectionOn(direction); var perp = v - result; Vector2.Dot(direction, perp).Should().BeApproximately(0, 1e-5f); Vector2X.Equals(result, expected).Should().BeTrue( $"Projection of {v} on {direction} should yield {expected} but got {result} instead."); }
public void StarBuild_SixPointedStarCenter() { var center = new Vector2(10, 12); var star = new StarBuilder().BuildStar(6, 8, 4, center, 1); star.Points.Count.Should().Be(12); var avg = star.Points.Average(); Vector2X.Equals(center, avg, 1e-6f).Should().BeTrue($"Expected {center}, actual {avg}"); }
public void Poly_Rotate() { var a = RectangleX.FromLTRB(0, 0, 10, 5).ToPolygon(); var b = a.RotateDegrees(90, new Vector2(10, 5)); b.Count.Should().Be(4); b.Any(x => Vector2X.Equals(x, new Vector2(5, 5), 1e-5f)).Should().BeTrue(); b.Any(x => Vector2X.Equals(x, new Vector2(10, 5), 1e-5f)).Should().BeTrue(); b.Any(x => Vector2X.Equals(x, new Vector2(10, 15), 1e-5f)).Should().BeTrue(); b.Any(x => Vector2X.Equals(x, new Vector2(5, 15), 1e-5f)).Should().BeTrue(); }
public void V2_FromPolar() { Vector2 a = Vector2X.FromPolar(1, MathF.PI * 0.5f); Vector2 b = Vector2X.FromPolar(1, MathF.PI * 1.0f); Vector2 c = Vector2X.FromPolar(1, MathF.PI * 1.5f); Vector2 d = Vector2X.FromPolar(1, MathF.PI * 2.0f); Vector2X.Equals(a, Vector2.UnitY, .000001f); Vector2X.Equals(b, Vector2.UnitX, .000001f); Vector2X.Equals(c, Vector2.UnitY, .000001f); Vector2X.Equals(d, Vector2.UnitX, .000001f); }
public void Line_SegmentIntersectionHighSymmetry() { var result = LineAlgorithms.LineSegmentIntersection( Vector2.UnitX, -Vector2.UnitX, Vector2.UnitY, -Vector2.UnitY); result.Should().NotBeNull(); result.U1.Should().BeApproximately(0.5f, 0.0001f); result.U2.Should().BeApproximately(0.5f, 0.0001f); Vector2X.Equals(Vector2.Zero, result.IntersectionPoint).Should().BeTrue("Lines should intersect at origin"); }
public void CD_ContactPoint() { var pa = Diamond; var pb = new RectangleF(0.9f, -1, 1, 2).ToPolygon(); var contactPoint = collider.FindConvexContactPoint(pa, pb); contactPoint.FirstPolygon.Should().BeEquivalentTo(pa); contactPoint.SecondPolygon.Should().BeEquivalentTo(pb); Vector2X.Equals(new Vector2(0.1f, 0), contactPoint.PenetrationDepth, 1e-6f).Should().BeTrue( $"Penetration depth failed. Expected (0.1, 0) but got {contactPoint.PenetrationDepth}."); contactPoint.FirstPolygonContactPoint.Should().Be(new Vector2(1, 0)); contactPoint.SecondPolygonContactPoint.Should().Be(new Vector2(-0.5f, 0)); }
public void CD_ContactPointGradual() { for (float d = 0.1f; d < 1; d += 0.05f) { var pa = new Polygon(Diamond.Points.Select(x => 10 * x)); var pb = new RectangleF(10 - d, -10, 10, 20).ToPolygon(); var contactPoint = collider.FindConvexContactPoint(pa, pb); contactPoint.FirstPolygon.Should().BeEquivalentTo(pa); contactPoint.SecondPolygon.Should().BeEquivalentTo(pb); Vector2X.Equals(new Vector2(d, 0), contactPoint.PenetrationDepth, 1e-6f).Should().BeTrue($"Penetration depth test failed at {d}."); Vector2X.Equals(new Vector2(10, 0), contactPoint.FirstPolygonContactPoint, 1e-6f).Should().BeTrue( $"Contact point on diamond failed at {d}. Expected (10, 0) but got {contactPoint.FirstPolygonContactPoint}"); Vector2X.Equals(new Vector2(-5, 0), contactPoint.SecondPolygonContactPoint, 1e-6f).Should().BeTrue( $"Contact point on rectangle failed at {d}. Expected (-5, 0) but got {contactPoint.SecondPolygonContactPoint}"); } }
public void CD_GJK_EPA_GradualPenetrationDepth() { for (float d = 0.05f; d < 1; d += 0.05f) { var pa = new Polygon(Diamond.Points.Select(x => 10 * x)); var pb = new RectangleF(10 - d, -10, 10, 20).ToPolygon(); var gjk = new GilbertJohnsonKeerthiAlgorithm(); var simplex = gjk.FindMinkowskiSimplex(pa, pb); var epa = new ExpandingPolytopeAlgorithm(); var pv = epa.PenetrationDepth( v => GilbertJohnsonKeerthiAlgorithm.PolygonSupport(pa, v), v => GilbertJohnsonKeerthiAlgorithm.PolygonSupport(pb, v), simplex); simplex.ContainsOrigin.Should().BeTrue($"Collision not detected by Minkowski simplex at {d}."); Vector2X.Equals(new Vector2(d, 0), pv.Value, 1e-6f).Should().BeTrue($"Penetration depth test failed at {d}."); } }