1. Introduction
Stick Figure is a minimalist graphic that is usually composed of simple lines and circles, but can vividly express the character's posture and movements. Stickman is not only widely used in animation, comics and graffiti, but can also be used as a teaching and research tool in the fields of graphics, artificial intelligence and other fields. This article aims to introduce how to use Python to implement the design and drawing of Stickman. Through programming, readers can understand the basic principles and implementation methods behind Stickman.
2. Preparation
Before you start implementing Stickman, you need to make sure that you have the Python environment installed and are familiar with basic Python programming knowledge. In addition, in order to draw the graph, we will usematplotlib
Library, a powerful drawing library for generating a variety of static, dynamic and interactive charts.
You can install it by following the commandmatplotlib
:
pip install matplotlib
3. Basic theoretical knowledge
The drawing of stickman mainly relies on the drawing and transformation of geometric figures. Specifically, we need:
(1)Define joints: Stickman's joints include the head, shoulders, elbows, wrists, hips, knees and ankles. These joints can be regarded as points in two-dimensional or three-dimensional space.
(2)Draw segments: Draw the line segments connecting the joints according to the position of the joint, which form the stickman's bones.
(3)Add a circle: Add a circle to joints such as the head to indicate joints.
(4)Transformation and animation: By changing the position of the joint, you can realize stickman's movements and animation effects.
4. Detailed steps
Below, we will introduce how to use Python andmatplotlib
Draw Stickman.
(1)Import library
First, we need to importmatplotlib
In the librarypyplot
Module:
import as plt import numpy as np
(2)Define joint position
For simplicity, we first define the joint position of the stickman on a two-dimensional plane. Here is an example of a simple stickman standing posture:
# Define joint positionhead = [0, 1] torso = [0, 0] left_shoulder = [-0.5, 0] left_elbow = [-1, -0.5] left_hand = [-1, -1] right_shoulder = [0.5, 0] right_elbow = [1, -0.5] right_hand = [1, -1] left_hip = [-0.5, -0.5] left_knee = [-1, -1.5] left_foot = [-1, -2] right_hip = [0.5, -0.5] right_knee = [1, -1.5] right_foot = [1, -2] # Store joint positions in a dictionaryjoints = { 'head': head, 'torso': torso, 'left_shoulder': left_shoulder, 'left_elbow': left_elbow, 'left_hand': left_hand, 'right_shoulder': right_shoulder, 'right_elbow': right_elbow, 'right_hand': right_hand, 'left_hip': left_hip, 'left_knee': left_knee, 'left_foot': left_foot, 'right_hip': right_hip, 'right_knee': right_knee, 'right_foot': right_foot }
(3)Drawing Stickman
Next, we write a function to draw stickman based on joint position:
def draw_stick_figure(joints, ax): # Draw the body body_parts = [ ('torso', 'head'), ('torso', 'left_shoulder'), ('left_shoulder', 'left_elbow'), ('left_elbow', 'left_hand'), ('torso', 'right_shoulder'), ('right_shoulder', 'right_elbow'), ('right_elbow', 'right_hand'), ('torso', 'left_hip'), ('left_hip', 'left_knee'), ('left_knee', 'left_foot'), ('torso', 'right_hip'), ('right_hip', 'right_knee'), ('right_knee', 'right_foot') ] for start, end in body_parts: start_pos = (joints[start]) end_pos = (joints[end]) ([start_pos[0], end_pos[0]], [start_pos[1], end_pos[1]], 'k-') # Draw the head circle = (joints['head'], 0.1, color='black', fill=True) ax.add_patch(circle) # Draw hands (optional) circle = (joints['left_hand'], 0.05, color='black', fill=True) ax.add_patch(circle) circle = (joints['right_hand'], 0.05, color='black', fill=True) ax.add_patch(circle) # Draw the foot (optional) circle = (joints['left_foot'], 0.05, color='black', fill=True) ax.add_patch(circle) circle = (joints['right_foot'], 0.05, color='black', fill=True) ax.add_patch(circle)
(4)Draw and display the graphics
Finally, we create a graph object, call the drawing function, and display the result:
def main(): fig, ax = () ax.set_aspect('equal') ('off') # Close the axis draw_stick_figure(joints, ax) () if __name__ == "__main__": main()
5. FAQ
(1) Stickman looks distorted or misscaled: This is usually caused by unreasonable definition of joint position or incorrect segment connection. Check that the joint position and connection order are correct.
(2) The graphics are incomplete: Make sure to set ax.set_aspect('equal') so that the graphics are displayed in equal proportions.
(3) How to add animation effects: You can use matplotlib's FuncAnimation class to achieve animation effects by constantly updating joint positions.
6. Results and Case Sharing
Through the above steps, you have successfully drawn a simple stickman. Next, we can try more complex poses and animation effects. For example, by changing the joint position, stickman jumps, walks and other actions.
Here is a simple animation example showing the process of stickman moving from left to right:
import as animation def update_position(frame, joints): # Here we simply move the stickman to the right translation = 0.1 * frame for key in (): joints[key][0] += translation return joints def animate(frame): global joints_anim joints_anim = update_position(frame, joints_anim) () ax.set_aspect('equal') ('off') draw_stick_figure(joints_anim, ax) def main_animation(): fig, ax = () global joints_anim joints_anim = {key: () for key, value in ()} # Copy the initial joint position ani = (fig, animate, frames=100, interval=100) () if __name__ == "__main__": main_animation()
7. Case code example
Here is a complete code example, including all steps and comments:
import as plt import numpy as np import as animation # Define joint positionjoints = { 'head': [0, 1], 'torso': [0, 0], 'left_shoulder': [-0.5, 0], 'left_elbow': [-1, -0.5], 'left_hand': [-1, -1], 'right_shoulder': [0.5, 0], 'right_elbow': [1, -0.5], 'right_hand': [1, -1], 'left_hip': [-0.5, -0.5], 'left_knee': [-1, -1.5], 'left_foot': [-1, -2], 'right_hip': [0.5, -0.5], 'right_knee': [1, -1.5], 'right_foot': [1, -2] } # Convert joint positions to numpy arrays for math operationsjoints = {key: (value) for key, value in ()} # Drawing Stickman's functiondef draw_stick_figure(joints, ax): # Clear the previous drawing () # Set the scale and limits of the axis ax.set_aspect('equal') ax.set_xlim(-2, 2) ax.set_ylim(-2.5, 1.5) # Define body parts and corresponding colors (optional) body_parts = [ ('torso', 'head', 'black'), ('torso', 'left_shoulder', 'black'), ('left_shoulder', 'left_elbow', 'black'), ('left_elbow', 'left_hand', 'black'), ('torso', 'right_shoulder', 'black'), ('right_shoulder', 'right_elbow', 'black'), ('right_elbow', 'right_hand', 'black'), ('torso', 'left_hip', 'black'), ('left_hip', 'left_knee', 'black'), ('left_knee', 'left_foot', 'black'), ('torso', 'right_hip', 'black'), ('right_hip', 'right_knee', 'black'), ('right_knee', 'right_foot', 'black') ] # Draw the various parts of the stickman for part in body_parts: start_joint, end_joint, color = part[0], part[1], part[2] if len(part) > 2 else 'black' ([joints[start_joint][0], joints[end_joint][0]], [joints[start_joint][1], joints[end_joint][1]], color=color, linewidth=2) # Show grid (optional) (True) # Create graphics and axesfig, ax = () # Initialize function (for animation)def init(): draw_stick_figure(joints, ax) return [] # Return to the empty list because we don't have an artist object that needs to be updated # Animation update functiondef update(frame): # Here you can add logic to make stickman move or change posture # For example, simply rotate your arms or legs # But for simplicity, we don't change joint positions here draw_stick_figure(joints, ax) return [] # Also return to empty list # Create animationani = (fig, update, frames=100, init_func=init, blit=True, interval=100) # Show graphics()
Please note the following points:
(1) I converted the joint position into a numpy array to perform math operations when needed (although not used in this simple example).
(2) In the draw_stick_figure function, I added code to set the axis scale and limit, as well as an optional grid display.
(3) In the body_parts list, I added the color parameters, but in this example, I used black by default. You can change the color as needed.
(4) In the update function, I did not change the joint position, so the stickman stayed still in the animation. You can add logic to change the stickman's posture or position as needed.
(5) I used FuncAnimation to create animations and set the interval between 100 frames and 100 milliseconds. You can adjust these parameters as needed.
Running this code will show a window containing the stationary stickman and, due to the animation's settings, it will be repainted every 100 milliseconds (although it looks like it is still, as the joint position has not changed, and interested readers can try to change the joint position).
The above is the detailed content of the design and implementation of Stickman in Python. For more information about the implementation of Stickman in Python, please pay attention to my other related articles!