Draw a simple robot arm with a hand in C#

The example Draw a simple robot arm in C# shows how to use transformations to make it (relatively) easy to draw a robot arm. Each piece of the arm is transformed due to the pieces closer to the arm's base at the shoulder. See that example for the basic explanation.

This example adds a hand that opens and closes. The following code shows how the program draws its arm with the new code highlighted in blue.

// Draw the robot arm.
private void DrawRobotArm(Graphics gr)
{
const int UpperArmLength = 75;
const int LowerArmLength = 50;
const int WristLength = 20;
const int HandWidth = 48;
const int FingerLength = 30;

gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.Clear(picCanvas.BackColor);

// For each stage in the arm, draw and then
// *prepend* the new transformation to
// represent the next arm in the sequence.

// Translate to center of form.
float cx = picCanvas.ClientSize.Width / 2;
float cy = picCanvas.ClientSize.Height / 2;
gr.TranslateTransform(cx, cy);

// Draw the shoulder centered at the origin.
gr.DrawEllipse(Pens.Red, -4, -4, 9, 9);

// Rotate at the shoulder.
// (Negative to make the angle increase counter-clockwise).
gr.RotateTransform(-scrJoint1.Value, MatrixOrder.Prepend);

// Draw the first arm.
gr.DrawRectangle(Pens.Blue, 0, -1, UpperArmLength, 3);

// Translate to the end of the first arm.
gr.TranslateTransform(UpperArmLength, 0, MatrixOrder.Prepend);

// Draw the elbow.
gr.DrawEllipse(Pens.Red, -4, -4, 9, 9);

// Rotate at the elbow.
gr.RotateTransform(-scrJoint2.Value, MatrixOrder.Prepend);

// Draw the second arm.
gr.DrawRectangle(Pens.Blue, 0, -1, LowerArmLength, 3);

// Translate to the end of the second arm.
gr.TranslateTransform(LowerArmLength, 0, MatrixOrder.Prepend);

// Draw the wrist.
gr.DrawEllipse(Pens.Red, -4, -4, 9, 9);

// Rotate at the wrist.
gr.RotateTransform(-scrJoint3.Value, MatrixOrder.Prepend);

// Draw the third arm.
gr.DrawRectangle(Pens.Blue, 0, -1, WristLength, 3);

// Translate to the end of the third arm.
gr.TranslateTransform(WristLength, 0, MatrixOrder.Prepend);

// Draw the hand.
gr.DrawRectangle(Pens.Black, 0, -HandWidth / 2, 4, HandWidth);
gr.DrawRectangle(Pens.Black, 4, -scrHand.Value - 4, FingerLength, 4);
gr.DrawRectangle(Pens.Black, 4, scrHand.Value, FingerLength, 4);

}

There are only two additions to this code beyond what was used in the previous example. First, the code adds a translation to move to the end of the final arm segment. That positions future drawing at the end of the arm where the hand should be.

The second change is simply the code that draws the hand.

Adding more segments to a robot arm is just as easy. Add a translation and rotation to move to the end of the current segment, and then draw the new section.

  

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.