// Sort the vertices in anticlockwise order when looking towards the origin. private static List <Vertex> SortVertices(IEnumerable <Vertex> vertices) { var vertexList = vertices.ToList(); var center = vertexList.Aggregate(Vector.Zeros(3), (c, v) => c + v.Position).Normalize(); var view = -center; var comparer = new AnticlockwiseComparer(vertexList.First().Position, view); var sortedVertices = vertexList.OrderBy(vertex => vertex.Position, comparer).ToList(); return(sortedVertices); }
//TODO: Does this actually need to be given the list of vertices? Can't that be inferred from the edges? /// <summary> /// Build a lookup from the given vertices to the edges that contain them. /// </summary> /// <param name="vertices"></param> /// <param name="edges"></param> /// <returns></returns> public static Dictionary <Vertex, List <Edge> > VertexToEdgeDictionary(List <Vertex> vertices, List <Edge> edges) { var vertexToEdges = vertices.ToDictionary(vertex => vertex, vertex => new List <Edge>()); foreach (var edge in edges) { vertexToEdges[edge.A].Add(edge); vertexToEdges[edge.B].Add(edge); } foreach (var vertex in vertices) { var comparer = new AnticlockwiseComparer(vertex.Position, -vertex.Position); var sortedEdges = vertexToEdges[vertex].OrderBy(edge => edge.SphericalCenter(), comparer); vertexToEdges[vertex] = sortedEdges.ToList(); } return(vertexToEdges); }
public void Compare_ForClockwiseVectorsAroundAPoint_ShouldReturn1 (double azimuth0, double azimuth1) { // Fixture setup var center = VectorUtilities.NewVector(1, 0, 0); var view = VectorUtilities.NewVector(0, 0, -1); var comparer = new AnticlockwiseComparer(center, view); var lesserAzimuth = azimuth0 % (2 * Math.PI); var greaterAzimuth = azimuth1 % (2 * Math.PI - lesserAzimuth) + lesserAzimuth; var lessAnticlockwise = VectorUtilities.NewVector(Math.PI / 2, lesserAzimuth); var moreAnticlockwise = VectorUtilities.NewVector(Math.PI / 2, greaterAzimuth); var expected = 1; // Exercise system var actual = comparer.Compare(moreAnticlockwise, lessAnticlockwise); // Verify outcome TestUtilities.WriteExpectedAndActual(expected, actual); Assert.Equal(expected, actual); // Teardown }