MoonTools.Curve/Test/CubicBezierCurve2D.cs

189 lines
6.7 KiB
C#
Raw Normal View History

2019-10-28 18:17:14 +00:00
using NUnit.Framework;
using FluentAssertions;
2020-02-21 01:37:52 +00:00
using MoonTools.Curve;
2019-10-29 01:48:36 +00:00
using System;
2019-10-29 01:55:18 +00:00
using System.Numerics;
2019-10-28 18:17:14 +00:00
namespace Tests
{
public class CubicBezierCurve2DTests
{
[Test]
public void Point()
{
var p0 = new Vector2(-4, -4);
var p1 = new Vector2(-2, 4);
var p2 = new Vector2(2, -4);
var p3 = new Vector2(4, 4);
CubicBezierCurve2D.Point(p0, p1, p2, p3, 0.5f).Should().BeEquivalentTo(new Vector2(0, 0));
CubicBezierCurve2D.Point(p0, p1, p2, p3, 0.5f).Should().BeEquivalentTo(new Vector2(0, 0));
CubicBezierCurve2D.Point(p0, p1, p2, p3, 0.25f).Should().BeEquivalentTo(new Vector2(-2.1875f, -0.5f));
CubicBezierCurve2D.Point(p0, p1, p2, p3, 0.75f).Should().BeEquivalentTo(new Vector2(2.1875f, 0.5f));
2019-10-29 01:48:36 +00:00
Action invalidTime = () => CubicBezierCurve2D.Point(p0, p1, p2, p3, 1.5f);
invalidTime.Should().Throw<ArgumentException>();
2019-10-28 18:17:14 +00:00
}
[Test]
public void PointNormalized()
{
var p0 = new Vector2(-4, -4);
var p1 = new Vector2(-2, 4);
var p2 = new Vector2(2, -4);
var p3 = new Vector2(4, 4);
CubicBezierCurve2D.Point(p0, p1, p2, p3, 3, 2, 4).Should().BeEquivalentTo(new Vector2(0, 0));
CubicBezierCurve2D.Point(p0, p1, p2, p3, 2, 1, 5).Should().BeEquivalentTo(new Vector2(-2.1875f, -0.5f));
CubicBezierCurve2D.Point(p0, p1, p2, p3, 11, 2, 14).Should().BeEquivalentTo(new Vector2(2.1875f, 0.5f));
2019-10-29 01:48:36 +00:00
Action invalidTime = () => CubicBezierCurve2D.Point(p0, p1, p2, p3, 15, 2, 5);
invalidTime.Should().Throw<ArgumentException>();
2019-10-28 18:17:14 +00:00
}
[Test]
public void Velocity()
{
var p0 = new Vector2(-4, -4);
var p1 = new Vector2(-2, 4);
var p2 = new Vector2(2, -4);
var p3 = new Vector2(4, 4);
CubicBezierCurve2D.Velocity(p0, p1, p2, p3, 0.5f).Should().BeEquivalentTo(new Vector2(9, 0));
CubicBezierCurve2D.Velocity(p0, p1, p2, p3, 0.25f).Should().BeEquivalentTo(new Vector2(8.25f, 6f));
CubicBezierCurve2D.Velocity(p0, p1, p2, p3, 0.75f).Should().BeEquivalentTo(new Vector2(8.25f, 6f));
2019-10-29 01:48:36 +00:00
Action invalidTime = () => CubicBezierCurve2D.Velocity(p0, p1, p2, p3, 1.5f);
invalidTime.Should().Throw<ArgumentException>();
2019-10-28 18:17:14 +00:00
}
[Test]
public void VelocityNormalized()
{
var p0 = new Vector2(-4, -4);
var p1 = new Vector2(-2, 4);
var p2 = new Vector2(2, -4);
var p3 = new Vector2(4, 4);
CubicBezierCurve2D.Velocity(p0, p1, p2, p3, 3, 2, 4).Should().BeEquivalentTo(new Vector2(9, 0));
CubicBezierCurve2D.Velocity(p0, p1, p2, p3, 2, 1, 5).Should().BeEquivalentTo(new Vector2(8.25f, 6f));
CubicBezierCurve2D.Velocity(p0, p1, p2, p3, 11, 2, 14).Should().BeEquivalentTo(new Vector2(8.25f, 6f));
2019-10-29 01:48:36 +00:00
Action invalidTime = () => CubicBezierCurve2D.Velocity(p0, p1, p2, p3, 15, 2, 5);
invalidTime.Should().Throw<ArgumentException>();
2019-10-28 18:17:14 +00:00
}
}
public class CubicBezierCurve2DStructTests
{
[Test]
public void Point()
{
var myCurve = new CubicBezierCurve2D(
new Vector2(-4, -4),
new Vector2(-2, 4),
new Vector2(2, -4),
new Vector2(4, 4)
);
myCurve.Point(0.5f).Should().BeEquivalentTo(new Vector2(0, 0));
myCurve.Point(0.25f).Should().BeEquivalentTo(new Vector2(-2.1875f, -0.5f));
myCurve.Point(0.75f).Should().BeEquivalentTo(new Vector2(2.1875f, 0.5f));
2019-10-29 01:48:36 +00:00
myCurve.Invoking(x => x.Point(1.5f)).Should().Throw<ArgumentException>();
2019-10-28 18:17:14 +00:00
}
[Test]
public void PointNormalized()
{
var myCurve = new CubicBezierCurve2D(
new Vector2(-4, -4),
new Vector2(-2, 4),
new Vector2(2, -4),
new Vector2(4, 4)
);
myCurve.Point(3, 2, 4).Should().BeEquivalentTo(new Vector2(0, 0));
myCurve.Point(2, 1, 5).Should().BeEquivalentTo(new Vector2(-2.1875f, -0.5f));
myCurve.Point(11, 2, 14).Should().BeEquivalentTo(new Vector2(2.1875f, 0.5f));
2019-10-29 01:48:36 +00:00
myCurve.Invoking(x => x.Point(15, 2, 4)).Should().Throw<ArgumentException>();
2019-10-28 18:17:14 +00:00
}
[Test]
public void Velocity()
{
var myCurve = new CubicBezierCurve2D(
new Vector2(-4, -4),
new Vector2(-2, 4),
new Vector2(2, -4),
new Vector2(4, 4)
);
myCurve.Velocity(0.5f).Should().BeEquivalentTo(new Vector2(9, 0));
myCurve.Velocity(0.25f).Should().BeEquivalentTo(new Vector2(8.25f, 6f));
myCurve.Velocity(0.75f).Should().BeEquivalentTo(new Vector2(8.25f, 6f));
2019-10-29 01:48:36 +00:00
myCurve.Invoking(x => x.Velocity(1.5f)).Should().Throw<ArgumentException>();
2019-10-28 18:17:14 +00:00
}
[Test]
public void VelocityNormalized()
{
var myCurve = new CubicBezierCurve2D(
new Vector2(-4, -4),
new Vector2(-2, 4),
new Vector2(2, -4),
new Vector2(4, 4)
);
myCurve.Velocity(3, 2, 4).Should().BeEquivalentTo(new Vector2(9, 0));
myCurve.Velocity(2, 1, 5).Should().BeEquivalentTo(new Vector2(8.25f, 6f));
myCurve.Velocity(11, 2, 14).Should().BeEquivalentTo(new Vector2(8.25f, 6f));
2019-10-29 01:48:36 +00:00
myCurve.Invoking(x => x.Velocity(15, 2, 5)).Should().Throw<ArgumentException>();
2019-10-28 18:17:14 +00:00
}
[Test]
public void Equals()
{
var myCurve = new CubicBezierCurve2D(
new Vector2(-4, -4),
new Vector2(-2, 4),
new Vector2(2, -4),
new Vector2(4, 4)
);
var otherCurve = new CubicBezierCurve2D(
new Vector2(-4, -4),
new Vector2(-2, 4),
new Vector2(2, -4),
new Vector2(4, 4)
);
(myCurve == otherCurve).Should().BeTrue();
}
[Test]
public void NotEquals()
{
var myCurve = new CubicBezierCurve2D(
new Vector2(-9, -4),
new Vector2(-2, 4),
new Vector2(2, -4),
new Vector2(4, 4)
);
var otherCurve = new CubicBezierCurve2D(
new Vector2(-4, -4),
new Vector2(-2, 4),
new Vector2(2, -4),
new Vector2(4, 4)
);
(myCurve != otherCurve).Should().BeTrue();
}
2019-10-28 18:17:14 +00:00
}
2020-02-21 01:37:52 +00:00
}