A game framework written with osu! in mind.
at master 44 lines 1.8 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using System.Collections.Generic; 5using NUnit.Framework; 6using osu.Framework.Utils; 7using osuTK; 8 9namespace osu.Framework.Tests.MathUtils 10{ 11 [TestFixture] 12 public class TestPathApproximator 13 { 14 [Test] 15 public void TestLagrange() 16 { 17 // lagrange of (0,0) (0.5,0.35) (1,1) is equal to 0.6x*x + 0.4x 18 Vector2[] points = { new Vector2(0, 0), new Vector2(0.5f, 0.35f), new Vector2(1, 1) }; 19 20 List<Vector2> approximated = PathApproximator.ApproximateLagrangePolynomial(points); 21 Assert.Greater(approximated.Count, 10, "Approximated polynomial should have at least 10 points to test"); 22 23 for (int i = 0; i < approximated.Count; i++) 24 { 25 float x = approximated[i].X; 26 Assert.GreaterOrEqual(x, 0); 27 Assert.LessOrEqual(x, 1); 28 Assert.AreEqual(0.6f * x * x + 0.4f * x, approximated[i].Y, 1e-4); 29 } 30 } 31 32 [Test] 33 public void TestBSpline() 34 { 35 Vector2[] points = { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, -1), new Vector2(-1, -1), new Vector2(-1, 1), new Vector2(3, 2), new Vector2(3, 0) }; 36 37 List<Vector2> approximated = PathApproximator.ApproximateBSpline(points, 4); 38 Assert.AreEqual(approximated.Count, 29, "Approximated path should have 29 points to test"); 39 Assert.True(Precision.AlmostEquals(approximated[0], points[0], 1e-4f)); 40 Assert.True(Precision.AlmostEquals(approximated[28], points[6], 1e-4f)); 41 Assert.True(Precision.AlmostEquals(approximated[10], new Vector2(-0.11415f, -0.69065f), 1e-4f)); 42 } 43 } 44}