Use segments to draw 3D axes with tic marks using WPF and C#

draw 3D arrows

This example uses the techniques described in the post Draw improved 3D line segments using WPF and C# to draw 3D axes with tic marks. You could make the tic marks in several ways. You could use small spheres, cubes, line segments, of boxes. This example uses a flat box. (Picture an axis as a pole with the tic marks represented by pizza boxes spaced along the pole.)

The following code shows the AddAxis extension method that adds an axis to a MeshGeometry3D object.

// Make an axis with tic marks.
public static void AddAxis(this MeshGeometry3D mesh,
    Point3D point1, Point3D point2, Vector3D up,
    double tic_diameter, double tic_separation)
    // Make the shaft.
    AddSegment(mesh, point1, point2, 0.05, true);

    // Get a unit vector in the direction of the segment.
    Vector3D v = point2 - point1;
    double length = v.Length;

    // Find the position of the first tic mark.
    Point3D tic_point1 = point1 + v * (tic_separation - 0.025);

    // Make tic marks.
    int num_tics = (int)(length / tic_separation) - 1;
    for (int i = 0; i < num_tics; i++)
        Point3D tic_point2 = tic_point1 + v * 0.05;
        AddSegment(mesh, tic_point1, tic_point2, tic_diameter);
        tic_point1 += v * tic_separation;

The method first calls AddSegment to create the axis's shaft.

Next the code makes a vector with length 1 in the direction of the axis. To the axis's first point, it adds a vector along the axis that has length tic_spacing minus half of the tic mark's thickness (0.005 in this example, but you could make it a parameter). That gives the location of the first tic mark. (This example doesn't draw a tic mark at either of the axis's ends, but you could change that if you like.)

Now the code calculates the number of tic marks it must draw. It finishes by looping through the tic marks to create them. It makes each tic mark 0.05 units thick (again, you could make this a parameter). Between tic marks it moves along the axis by distance tic_spacing.

The following code shows how the main program makes its X axis. It makes the other axes similarly.

// X = Red.
MeshGeometry3D x_axis_mesh = new MeshGeometry3D();
x_axis_mesh.AddAxis(origin, new Point3D(axis_length, 0, 0),
    new Vector3D(0, 1, 0), tic_diameter, 1.0);
DiffuseMaterial x_axis_material = new DiffuseMaterial(Brushes.Red);
XAxis = new GeometryModel3D(x_axis_mesh, x_axis_material);

This code creates a new MeshGeometry3D object. It calls the mesh's AddAxis extension method, creates a red material for it, and uses the mesh and material to create a GeometryModel3D object. Finally it adds the model to the main model group's Children collection.

Download the example to see additional 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, drawing, geometry, graphics, mathematics, wpf, XAML and tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Use segments to draw 3D axes with tic marks using WPF and C#

  1. 阿呆麦迪 says:

    how can i add scale on the axes

Leave a Reply

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