[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: Draw improved 3D line segments using WPF and C#

[WPF 3d] improved 3D line segments

This example extends the example Draw interlocked tetrahedrons in a cage of "line segments" using WPF and C# by using improved 3D line segments. The previous example draws skinny rectangular prisms to represent line segments. If you make the prisms thick enough, you can see that they don't line up well at their corners. (See the picture on the left side.)

This example uses the following AddSegment method to extend the prisms slightly so segments that share an end point overlap by the size of their thickness. That makes them line up nicely. (At least when their prism sides are parallel. See the picture on the right side.) The new code is shown in blue.

private void AddSegment(MeshGeometry3D mesh, Point3D point1, Point3D point2, Vector3D up, bool extend) { const double thickness = 0.25; // Get the segment's vector. Vector3D v = point2 - point1; if (extend) { // Increase the segment's length on both ends // by thickness / 2. Vector3D n = ScaleVector(v, thickness / 2.0); point1 -= n; point2 += n; } // Get the scaled up vector. Vector3D n1 = ScaleVector(up, thickness / 2.0); // Get another scaled perpendicular vector. Vector3D n2 = Vector3D.CrossProduct(v, n1); n2 = ScaleVector(n2, thickness / 2.0); // Make a skinny box. // p1pm means point1 PLUS n1 MINUS n2. Point3D p1pp = point1 + n1 + n2; Point3D p1mp = point1 - n1 + n2; Point3D p1pm = point1 + n1 - n2; Point3D p1mm = point1 - n1 - n2; Point3D p2pp = point2 + n1 + n2; Point3D p2mp = point2 - n1 + n2; Point3D p2pm = point2 + n1 - n2; Point3D p2mm = point2 - n1 - n2; // Sides. AddTriangle(mesh, p1pp, p1mp, p2mp); AddTriangle(mesh, p1pp, p2mp, p2pp); AddTriangle(mesh, p1pp, p2pp, p2pm); AddTriangle(mesh, p1pp, p2pm, p1pm); AddTriangle(mesh, p1pm, p2pm, p2mm); AddTriangle(mesh, p1pm, p2mm, p1mm); AddTriangle(mesh, p1mm, p2mm, p2mp); AddTriangle(mesh, p1mm, p2mp, p1mp); // Ends. AddTriangle(mesh, p1pp, p1pm, p1mm); AddTriangle(mesh, p1pp, p1mm, p1mp); AddTriangle(mesh, p2pp, p2mp, p2mm); AddTriangle(mesh, p2pp, p2mm, p2pm); }

If the extend parameter is true, the program creates a vector in the direction of the line segment that has length equal to half of the prism's width. It then moves the segment's end points that distance away from each other to extend the segment the correct amount.

The rest of the method is similar to the code used in the previous example.

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

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