using System;
using UnityEngine;
using NUnit.Framework;
namespace Framework.Geometry.Lines.Fitting {
/// Verifies that the 2D line fitter is working correctly
[TestFixture]
public static class Line2FitterTest {
///
/// Verifies that attempting to fit a line to no points results in a default line
///
[Test]
public static void NoPointsResultInDefaultLine() {
var noPoints = new Vector2[0];
Line2 fittedLine = Line2Fitter.Fit(noPoints);
Assert.That(fittedLine.Offset, Is.EqualTo(Vector2.zero));
Assert.That(fittedLine.Direction, Is.EqualTo(Vector2.right));
}
///
/// Verifies that fitting a line to a single point produces a horizontal line
///
[Test]
public static void SinglePointResultsInHorizontalLine() {
var singlePoint = new Vector2[] { new Vector2(1.2f, 3.4f) };
Line2 fittedLine = Line2Fitter.Fit(singlePoint);
Assert.That(fittedLine.Offset, Is.EqualTo(singlePoint[0]));
Assert.That(fittedLine.Direction, Is.EqualTo(Vector2.right));
}
///
/// Verifies that fitting a bunch of points all on top of each other produces
/// a horizontal line
///
[Test]
public static void ClumpedPointsResultInHorizontalLine() {
var clumpedPoints = new Vector2[] {
new Vector2(1.2f, 3.4f),
new Vector2(1.2f, 3.4f),
new Vector2(1.2f, 3.4f),
new Vector2(1.2f, 3.4f)
};
Line2 fittedLine = Line2Fitter.Fit(clumpedPoints);
Assert.That(fittedLine.Offset, Is.EqualTo(clumpedPoints[0]));
Assert.That(fittedLine.Direction, Is.EqualTo(Vector2.right));
}
///
/// Verifies that fitting a line to a series in a flat row builds a horizontal line
///
[Test]
public static void FlatHorizontalLineCanBeFitted() {
var horizontalPoints = new Vector2[] {
new Vector2(4.0f, 5.0f),
new Vector2(9.0f, 5.0f),
new Vector2(14.0f, 5.0f),
new Vector2(21.0f, 5.0f)
};
Line2 fittedLine = Line2Fitter.Fit(horizontalPoints);
Assert.That(fittedLine.Direction, Is.EqualTo(Vector2.right));
Assert.That(fittedLine.Offset, Is.EqualTo(new Vector2(0.0f, 5.0f)));
}
///
/// Verifies that fitting a line to a series in a flat column builds a vertical line
///
[Test]
public static void FlatVerticalLineCanBeFitted() {
var verticalPoints = new Vector2[] {
new Vector2(5.0f, 4.0f),
new Vector2(5.0f, 9.0f),
new Vector2(5.0f, 14.0f),
new Vector2(5.0f, 21.0f)
};
Line2 fittedLine = Line2Fitter.Fit(verticalPoints);
Assert.That(fittedLine.Direction, Is.EqualTo(Vector2.up));
Assert.That(fittedLine.Offset, Is.EqualTo(new Vector2(5.0f, 0.0f)));
}
}
} // namespace Framework.Geometry.Lines.Fitting