Tutorial 5: Adding a Leader Line
Anchored leader lines have a number of applications, but they're especially useful for text annotations. In this tutorial, we'll use the ShapeKit class to build a simple text annotation with an anchored leader line that points to a vertex on a cube. Before we begin, be sure that you've set the namespace declaration in your code ("using namespace HPS;" in C++ or "using HPS;" in C#).
Let's start off by setting up a basic scene (including colors, font size, handedness, and camera position) and creating a portfolio where we'll store definitions that our segments will access throughout this program.
Next we'll insert a basic cube and position it at the point (0, 1, 0) in World space. This is the object that the leader line will be pointing to. (The code for creating a cube is outside the scope of this discussion, but you can find a sample of the InsertCube function in the Appendices below.)
Our textbox will be in the shape of a rectangle, so we'll create a rectangle by defining each of the four vertices with a ShapePoint. A rectangle is, of course, a polygon, so we'll add these vertices to a PolygonShapeElement that will serve as the background element on top of which our annotation text will be positioned.
Next, we'll create an AnchorShapeElement for our leader line and set the anchor point to the top right of our textbox rectangle. We'll also create an intermediate point where the leader line will bend (see the image at the beginning of this tutorial for reference).
A ShapeKit object will contain our rectangle shape along with the anchor and intermediate points of our leader line. We'll add the ShapeKit to our portfolio, insert our text into the rectangle segment, and define the coordinates where the leader line will point.
Our leader line will be a solid line, so we'll define it using a SolidLinePatternElement and add it to a LinePatternParallelKit, which we can later add to our portfolio and access in our segments.
The arrowhead of our leader line is a simple triangle glyph. Define the glyph in the portfolio and give it a name. Then create a GlyphLinePatternElement to set the attributes of the glyph.
Add the line pattern parallel kit to a line pattern kit using the SetParallels function. Then, we'll define the line pattern in our portfolio, where it can be accessed by our rectangle segment key.
Your scene is now complete and should match the image at the start of this tutorial. This is just a simple demonstration of the annotation and leader line features. Feel free to expand the program by adding multiple leader lines and intermediate points or by trying out different shapes for your annotations.
Full source code for this program is available here, including the program described above and a function for creating a cube from a Shell object.