Draw three interlocked boxes using XAML and C#

interlocked boxes

This example uses techniques similar to those in the example Draw interlocked tetrahedrons using XAML and C# to draw three interlocked boxes. It draws three cubes and scales them so they have different side lengths and so they overlap.

The following code shows how the program defines a single cube centered at the origin and with vertex coordinates (±1, ±1, ±1).

<MeshGeometry3D 
    Positions="
      -1,-1,-1   1,-1,-1   1,-1, 1  -1,-1, 1
      -1,-1, 1   1,-1, 1   1, 1, 1  -1, 1, 1
       1,-1, 1   1,-1,-1   1, 1,-1   1, 1, 1
       1, 1, 1   1, 1,-1  -1, 1,-1  -1, 1, 1
      -1,-1, 1  -1, 1, 1  -1, 1,-1  -1,-1,-1 
      -1,-1,-1  -1, 1,-1   1, 1,-1   1,-1,-1
      "
    TriangleIndices="
       0  1  2     2  3  0
       4  5  6     6  7  4
       8  9 10    10 11  8
      12 13 14    14 15 12
      16 17 18    18 19 16
      20 21 22    22 23 20
      " />

The example repeats this code three times and then uses transformations to stretch the cubes into the boxes that it needs. The following code shows how the program scales the red box.

<GeometryModel3D.Transform>
    <ScaleTransform3D ScaleX="1" ScaleY="2" ScaleZ="3" />
</GeometryModel3D.Transform>

The other cubes are scaled similarly but with different scale factors in the X, Y, and Z directions.


Download Example   Follow me on Twitter   RSS feed   Donate


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

2 Responses to Draw three interlocked boxes using XAML and C#

  1. Use static resources to draw three interlocked boxes using WPF and XAML

    The example Draw three interlocked boxes using WPF and XAML draws three cubes and then scales them to make them interlock. One problem with this technique is that it requires you to include three identical copies of the data that defines the cubes. That means you need to write, debug, and maintain the code in three places. The following code shows how the program defines its red box. The code in blue is duplicated for all three boxes.

  2. Pingback: Use static resources to draw three interlocked boxes

Leave a Reply

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