using System; using System.Collections.Generic; using System.Linq; using System.Text; using UnitTestSharp; using Lodestone; using Microsoft.Xna.Framework; namespace Lodestone.UnitTests { class PolygonTests : TestFixture { Polygon polygon; Vector2[] points; public void TestConcaveFails() { var points = new Vector2[10]; points[0] = new Vector2(0, -10); points[1] = new Vector2(2, -8); points[2] = new Vector2(4, -7); points[3] = new Vector2(3, -5); points[4] = new Vector2(5, -3); points[5] = new Vector2(3, -1); points[6] = new Vector2(1, -5); points[7] = new Vector2(2, -6); points[8] = new Vector2(1, -8); points[9] = new Vector2(0, -9); CheckThrow(typeof(Exception)); new Polygon(points); } public void TestComplexFails() { var points = new Vector2[10]; points[0] = new Vector2(0, 0); points[1] = new Vector2(2, 0); points[2] = new Vector2(2, 1); points[3] = new Vector2(1, 1); points[4] = new Vector2(1, -2); points[5] = new Vector2(2, -2); points[6] = new Vector2(2, -1); points[7] = new Vector2(-2, -1); points[8] = new Vector2(-2, -2); points[9] = new Vector2(1, 0); CheckThrow(typeof(Exception)); new Polygon(points); } public void TestConvexPasses() { var points = new Vector2[8]; points[0] = new Vector2(0, 0); points[1] = new Vector2(0, 1); points[2] = new Vector2(1, 2); points[3] = new Vector2(2, 2); points[4] = new Vector2(3, 1); points[5] = new Vector2(3, 0); points[6] = new Vector2(2, -1); points[7] = new Vector2(1, -1); new Polygon(points); } public void TestDirectionCorrect() { var points = new Vector2[8]; points[0] = new Vector2(0, 0); points[1] = new Vector2(0, 1); points[2] = new Vector2(1, 2); points[3] = new Vector2(2, 2); points[4] = new Vector2(3, 1); points[5] = new Vector2(3, 0); points[6] = new Vector2(2, -1); points[7] = new Vector2(1, -1); var polygon = new Polygon(points); CheckTrue(polygon.Clockwise); } public void TestProjectToNormal() { var points = new Vector2[8]; points[0] = new Vector2(0, 0); points[1] = new Vector2(0, 1); points[2] = new Vector2(1, 2); points[3] = new Vector2(2, 2); points[4] = new Vector2(3, 1); points[5] = new Vector2(3, 0); points[6] = new Vector2(2, -1); points[7] = new Vector2(1, -1); var polygon = new Polygon(points); Interval expected = new Interval(0, 4); Vector2 vector = new Vector2(1, -1); CheckEqual(expected, polygon.ProjectToNormal(vector)); } public void TestCenterOfMass_Clockwise() { var points = new Vector2[4]; points[0] = new Vector2(0, 0); points[1] = new Vector2(0, 1); points[2] = new Vector2(1, 1); points[3] = new Vector2(1, 0); var polygon = new Polygon(points); CheckEqual(Vector2.Zero, polygon.CenterOfMass); } public void TestCenterOfMass_CCW() { var points = new Vector2[4]; points[3] = new Vector2(0, 0); points[2] = new Vector2(0, 1); points[1] = new Vector2(1, 1); points[0] = new Vector2(1, 0); var polygon = new Polygon(points); CheckEqual(Vector2.Zero, polygon.CenterOfMass); } public void TestArea_Clockwise() { var points = new Vector2[4]; points[0] = new Vector2(0, 0); points[1] = new Vector2(0, 1); points[2] = new Vector2(1, 1); points[3] = new Vector2(1, 0); var polygon = new Polygon(points); Scalar expected = 1; CheckEqual(expected, polygon.Area); } public void TestArea_CCW() { var points = new Vector2[4]; points[3] = new Vector2(0, 0); points[2] = new Vector2(0, 1); points[1] = new Vector2(1, 1); points[0] = new Vector2(1, 0); var polygon = new Polygon(points); var expected = 1; CheckEqual((Scalar)expected, polygon.Area); } public void TestArea() { var points = new Vector2[6]; points[0] = new Vector2(0, 0); points[1] = new Vector2(1, 0); points[2] = new Vector2(2, 1); points[3] = new Vector2(2, 2); points[4] = new Vector2(1, 3); points[5] = new Vector2(0, 3); var polygon = new Polygon(points); CheckEqual((Scalar)5, polygon.Area); } public void TestPerimeter() { points = new Vector2[4]; points[0] = new Vector2(0, 0); points[1] = new Vector2(0, 1); points[2] = new Vector2(1, 1); points[3] = new Vector2(1, 0); polygon = new Polygon(points); Scalar expected = 4; CheckEqual(expected, polygon.Perimeter); } public void TestClone() { var points = new Vector2[8]; points[0] = new Vector2(0, 0); points[1] = new Vector2(0, 1); points[2] = new Vector2(1, 2); points[3] = new Vector2(2, 2); points[4] = new Vector2(3, 1); points[5] = new Vector2(3, 0); points[6] = new Vector2(2, -1); points[7] = new Vector2(1, -1); var polygon = new Polygon(points); var clone = (Polygon)polygon.Clone(); CheckEqual(polygon.Clockwise, clone.Clockwise); CheckEqual(polygon.Perimeter, clone.Perimeter); CheckEqual(polygon.Area, clone.Area); } public void TestClone2() { var points = new Vector2[6]; points[0] = new Vector2(0, 0); points[1] = new Vector2(1, 0); points[2] = new Vector2(2, 1); points[3] = new Vector2(2, 2); points[4] = new Vector2(1, 3); points[5] = new Vector2(0, 3); var polygon = new Polygon(points); Scalar expected = 5; var clone = polygon.Clone(); CheckEqual(expected, polygon.Area); CheckEqual(expected, polygon.Clone().Area); Check(Scalar.FuzzyEquality(expected, polygon.Clone().Area)); } public void TestCache() { var points = new Vector2[6]; points[0] = new Vector2(0, 0); points[1] = new Vector2(1, 0); points[2] = new Vector2(2, 1); points[3] = new Vector2(2, 2); points[4] = new Vector2(1, 3); points[5] = new Vector2(0, 3); var polygon = new Polygon(points); //try and time getting 100 random normals var vector = new Vector2((float)0.103, (float)-1.009); var timer = new System.Diagnostics.Stopwatch(); timer.Reset(); timer.Start(); for (int i = 0; i < 1000; i++) { polygon.ProjectToNormal(vector); } timer.Stop(); //try and time getting 100 common normals vector = polygon.CommonNormals.Keys.ElementAt(1); var timer2 = new System.Diagnostics.Stopwatch(); timer2.Reset(); timer2.Start(); for (int i = 0; i < 1000; i++) { polygon.ProjectToNormal(vector); } timer2.Stop(); //Check the timing CheckTrue(timer.ElapsedTicks > timer2.ElapsedTicks); } public void TestBoundingCircle() { points = new Vector2[4]; points[0] = new Vector2(1, 1); points[1] = new Vector2(-1, 1); points[2] = new Vector2(-1, -1); points[3] = new Vector2(1, -1); polygon = new Polygon(points); CheckEqual(Math.Truncate(100*(2*Math.Sqrt(.5))), Math.Truncate(100*(polygon.BoundingCircle.Radius))); } } }