This example shows how to draw cones in WPF and C#. The program uses a method very similar to the one used by the example Draw cylinders using WPF and C#.

The picture on the right shows the approach used by the previous example to draw a cylinder. It starts with the endpoint (green). It then adds a vector perpendicular to the axes to get the point `p1` on the cylinder’s base. Next it adds the vector `axis` to that point to get the point `p2` on the other end of the cylinder. The program generates the cylinder by connecting the points properly.

To create a truncated cone, this example use the slightly different approach shown on the left. It adds vector `v1` to the first end point (green) to get the point `p1`. It then adds a different vector `v2` to the other end point (red) to get the corresponding point on the other end of the cone.

To generate the vectors `v1`, the program finds two vectors perpendicular to the axis in the plane of the cone’s base. It then multiples those vectors my the cosine and sine of an angle `theta` as `theta` runs from 0 to 2π. It generates the vectors `v2` similarly.

The `AddCone` method has the following signature:

`private void AddCone(MeshGeometry3D mesh, Point3D end_point,
Vector3D axis, double radius1, double radius2, int num_sides)`

Its parameters are:

`mesh` – The `mesh` object that should contain the new cone.
`end_point` – The center of one of the cone’s faces. (Green in the pictures above.)
`axis` – The cone’s axis.
`radius1` – The radius of the cone’s first face.
`radius2` – The radius of the cone’s second face.
`num_sides` – The number of polygons that should be used around the cone’s sides.

To generate a complete cone instead of a truncated cone (like the blue cone in the picture at the top of the post), just set `radius1` or `radius2` to 0.

For additional details, download the example program and see the previous example.

##
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.

Pingback: Draw smooth cones using WPF and C# - C# HelperC# Helper