[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Platonic Solids Part 3: The cube

[Platonic Solids Part 3: The cube]


[Platonic Solids Part 3: The cube] A platonic cube has six square faces. The cube's symmetry makes it easy to find the coordinates of its vertices. For a cube with edge lengths of 1, the vertex coordinates are (±0.5, ±0.5, ±0.5) as shown in Figure 1.

The example program uses the following code to generate the cube's vertices.

// Return the vertices for a cube. private Point3D[] MakeVertices() { List points = new List(); points.Add(new Point3D(0.5, 0.5, 0.5)); points.Add(new Point3D(0.5, 0.5, -0.5)); points.Add(new Point3D(-0.5, 0.5, -0.5)); points.Add(new Point3D(-0.5, 0.5, 0.5)); points.Add(new Point3D(0.5, -0.5, 0.5)); points.Add(new Point3D(0.5, -0.5, -0.5)); points.Add(new Point3D(-0.5, -0.5, -0.5)); points.Add(new Point3D(-0.5, -0.5, 0.5)); // Verify that the edges have length 1. VerifyEdgeLengths(1, points.ToArray()); return points.ToArray(); }

This code creates the vertices and adds them to a list. It calls the following VerifyEdgeLengths method to verify that the edges all have length 1 and then returns the list of points converted into an array.

// Verify that the edges have the same length. private void VerifyEdgeLengths(double length, Point3D[] points) { VerifyEdgeLength(length, points[0], points[1]); VerifyEdgeLength(length, points[1], points[2]); VerifyEdgeLength(length, points[2], points[3]); VerifyEdgeLength(length, points[3], points[0]); VerifyEdgeLength(length, points[4], points[5]); VerifyEdgeLength(length, points[5], points[6]); VerifyEdgeLength(length, points[6], points[7]); VerifyEdgeLength(length, points[7], points[4]); for (int i = 0; i < 4; i++) VerifyEdgeLength(length, points[i], points[i + 4]); }

The VerifyEdgeLengths method calls the following VerifyEdgeLength method for each of the cube's edges.

private void VerifyEdgeLength(double length, Point3D p0, Point3D p1) { Vector3D vector = p1 - p0; Debug.Assert(Math.Abs(length - vector.Length) < 0.00001, "Edge " + p0.ToString() + " --> " + p1.ToString() + " does not have length " + length); }

The VerifyEdgeLength method subtracts the edge's vertices to get a vector between them. It then asserts that the vector has the desired length.

The rest of the program's code deals with using WPF to display the cube's faces, its edges, and the axes.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.