Home and Learn: Intermediate Programming


Drawing the Legs

Stick Figure App - Head, Arms, Legs

 

In the previous lesson, we wrote the code to draw the head and body for our stick figures. In this lesson, we'll add a pair of legs. Let's start with the right leg.

The start of the legs is obviously the end of the body. You can take the end of the body from the previous code. This is what we had:

x = mouseX + size.HeadRadius
y = mouseY + (size.HeadDiameter + size.BodySize)

We're trying to draw this:

Stick figure measurements

The x positions go from the left of the screen to the right, where the left of the screen is 0. We have the center point for the body as mouseX + size.HeadRadius. This is the start x position for the legs. The start y position is the head diameter plus the body size.

To get the right leg to stretch to the right, we can add 2 of our Base Units to the center line. Then we'll have the X position of the end of the line. For the end Y of the line, you take the mouseY position and just add all the body parts: the head size, the body size, and the leg size. The two positions for the end of the right leg are then this:

x1 = mouseX + (size.HeadRadius + size.BaseUnit * 2)
y1 = mouseY + (size.HeadDiameter + size.BodySize + size.LegSize)

Which means, the code to add to your DrawStickMan routine is this in VB Net:

VB Net code to draw the right leg of a stick figure

And this in C#:

C# code to draw the right leg of a stick figure

Drawing the Left Leg

The code to draw the left leg is just about the same as that for the right leg. There's only one difference, and that's the second X value. We can find the center line again with mouseX + the radius. From the center line we can then go to the left 2 Base Units. (We added two Base Units to get the end of the right leg.)

You can copy and paste the code for the right leg. For the third position parameter, just change the second plus symbol into a minus symbol:

x1 = mouseX + (size.HeadRadius - size.BaseUnit * 2)

Here's what your code window should look like in VB Net:

VB Net code to draw the left leg of a stick figure

And here's the C# code:

C# code to draw the left leg of a stick figure

When you run your program and draw a stick figure, it should look like this:

A stick man with legs

Now that our stick figure has a head, a body, and some legs, let's draw the arms. We'll do that in the next lesson.

Drawing the Arms >>

<< Back to the Intermediate Contents Page


Email us: enquiry at homeandlearn.co.uk