Platonic Solids Part 3: The cube

[cube]



[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 and look at the code for details.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.

This entry was posted in algorithms, graphics, mathematics, wpf and tagged , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

5 Responses to Platonic Solids Part 3: The cube

  1. hazim says:

    Hello Mr.
    I would like you help me in code program for signature image hash alg. and check image after signature if image original or not.

  2. José Pessoa says:

    Good Morning Lord RodStephens, Alright? Hà Long come looking for a cube 3D example with code and it worked, because thanks to God found her, only I need to be in Delphi (but in reality use Lazarus), and, as I saw that you also write articles Delphi, I urge you that if it is not asking too much Please make available a code of that hub only in Delphi, so I can continue my work, Please, and From now Thank you and God Bless .. .

    • RodStephens says:

      Sorry but I haven’t written in Delphi in a long time. I don’t know how its 3D libraries work. If you google for “Delphi 3D” you will find some libraries and other resources that may help.

Leave a Reply

Your email address will not be published. Required fields are marked *