Categories
Drawing Adinkra Symbols using Python

Dame Dame

Dame Dame means “chequered or checkered” as in the pattern on a chess or draught board. It is the symbol of intelligence, ingenuity, and strategy.

We will use a grid of 5 pixels to trace out the image. The image of this is shown below:

Analyzing the Symbol

Dame Dame consists of a filled square surrounded by an outer circle. The four sides of the square are connected to the circle by a shape that looks like a tong.

The Plan to Draw the Symbol

To draw Dame Dame, we first draw the filled square. Next we draw the outer circle.

The tong like shape connecting the square to the circle is drawn for all the four sides of the square.

The Algorithm to Draw the Symbol

The algorithm to draw Dame Dame is given below:

  1. Increase the pen size to 20 pixels
  2. Move the turtle to the position (-60, 60)
  3. Draw the inner filled square as a square of length 120 pixels
  4. Lift up the pen
  5. Move the pen to the position (0, -170)
  6. Draw a circle of radius 170
  7. Lift up the pen
  8. Set the heading of the pen to 0
  9. Move the pen to the position (60, 20)
  10. Draw the right handle
  11. Lift up the pen
  12. Set the heading of the pen to 0
  13. Draw the line joining the right handle to the circle
  14. Lift up the pen
  15. Set the heading of the pen to 90
  16. Move the pen to the position (-20, 60)
  17. Draw the top handle
  18. Lift up the pen
  19. Set the heading of the pen to 90
  20. Draw the line joining the top handle to the circle
  21. Lift up the pen
  22. Set the heading of the pen to 180
  23. Move the pen to the position (-60, -20)
  24. Draw the left handle
  25. Lift up the pen
  26. Set the heading of the pen to 180
  27. Draw the line joining the left handle to the circle
  28. Lift up the pen
  29. Set the heading of the pen to 270
  30. Move the pen to the position (20, -60)
  31. Draw the bottom handle
  32. Lift up the pen
  33. Set the heading of the pen to 270
  34. Draw the line joining the bottom handle to the circle

From the above steps, you will notice that steps (7-13), (14-20), (21-27) and (28-34) are similar. As a result, they would make a great candidate for a function.

Using Turtle Graphics

We will use the template.py file and rename it to damedame.py. The first task will be do increase the pen size of the turtle.

The code to do this is shown below:

turtle.pensize(20)

Next we draw the filled square. The code to do this is shown below:

turtle.setposition(-60, 60)
turtle.pendown()
turtle.begin_fill()
turtle.forward(120)
turtle.right(90)
turtle.forward(120)
turtle.right(90)
turtle.forward(120)
turtle.right(90)
turtle.forward(120)
turtle.right(90)
turtle.end_fill()

The generated image is shown below:

Next we draw the outer circle. The code to do this is shown below:

turtle.penup()
turtle.setposition(0, -170)
turtle.pendown()
turtle.circle(170)

The generated image is shown below:

Next we draw the right handle. The code to do this is shown below:

turtle.penup()
turtle.setheading(0)
turtle.setposition(60, 20)
turtle.pendown()
turtle.forward(50)
turtle.right(90)
turtle.forward(40)
turtle.right(90)
turtle.forward(50)

The generated image is shown below:

Now we draw the line that connects the right handle to the circle. The code to do this is shown below:

turtle.penup()
turtle.setheading(0)
turtle.setposition(110, 0)
turtle.pendown()
turtle.forward(50)

The generated image is shown below:

Next we draw the top handle. The code to do this is shown below:

turtle.penup()
turtle.setheading(90)
turtle.setposition(-20, 60)
turtle.pendown()
turtle.forward(50)
turtle.right(90)
turtle.forward(40)
turtle.right(90)
turtle.forward(50)

The generated image is shown below:

Now we draw the line that connects the top handle with the circle. The code to do this is shown below:

turtle.penup()
turtle.setheading(90)
turtle.setposition(0, 110)
turtle.pendown()
turtle.forward(50)

The generated image is shown below:

Next we draw the left handle. The code to do this is shown below:

turtle.penup()
turtle.setheading(180)
turtle.setposition(-60, -20)
turtle.pendown()
turtle.forward(50)
turtle.right(90)
turtle.forward(40)
turtle.right(90)
turtle.forward(50)

The generated image is shown below:

Now we draw the line that connects the left handle to the circle. The code to do this is shown below:

turtle.penup()
turtle.setheading(180)
turtle.setposition(-110, 0)
turtle.pendown()
turtle.forward(50)

The generated image is shown below:

Next we draw the bottom handle. The code to do this is shown below:

turtle.penup()
turtle.setheading(270)
turtle.setposition(20, -60)
turtle.pendown()
turtle.forward(50)
turtle.right(90)
turtle.forward(40)
turtle.right(90)
turtle.forward(50)

The generated image is shown below:

Now we draw the line that connects the bottom handle to the circle. The code to do this is shown below:

Complete Code

"""
Project Name: Drawing Adinkra Symbols using Python
Symbol Name: Dame Dame
Developer Name: Truston Ailende
Email Address: trustonailende@gmail.com
"""
import turtle
import math
 
# Square
def drawSquare(length):
    turtle.penup()
    turtle.setposition(-length/2.0, length/2.0)
    turtle.pendown()
    for i in range(0, 4):
        turtle.forward(length)
        turtle.right(90)
    turtle.penup()
    turtle.home()
 
# Horizontal lines
def drawHorizontalLine(length, division):
    pixelSpace = int(length / division)
    half = int(length / 2)
    for j in range((-half + pixelSpace), half, pixelSpace):
        turtle.penup()
        turtle.setposition(-half, j)
        turtle.pendown()
        turtle.forward(length)
    turtle.penup()
    turtle.home()
 
# Vertical lines
def drawVerticalLine(length, division):
    pixelSpace = int(length / division)
    half = int(length / 2)
    turtle.right(90)
    for k in range((-half + pixelSpace), half, pixelSpace):
        turtle.penup()
        turtle.setposition(k, half)
        turtle.pendown()
        turtle.forward(length)
    turtle.penup()
    turtle.home()
 
# Draw the grid
turtle.speed(1000000)
drawSquare(400)
drawHorizontalLine(400, 40)
drawVerticalLine(400, 40)
 
# Change the colour mode
turtle.colormode(255)
 
# Change the pencolor to red
turtle.pencolor(255, 0, 0)
 
# Draw the horizontal centre line
turtle.setposition(-200, 0)
turtle.pendown()
turtle.forward(400)
turtle.penup()
 
# Draw the vertical centre line
turtle.setposition(0, 200)
turtle.setheading(270)
turtle.pendown()
turtle.forward(400)
 
# Reset all the properties
turtle.home()
turtle.pencolor(0, 0, 0)
 
# Place code here
# Set the pensize to 20
turtle.pensize(20)

# Draw the filled center square
turtle.setposition(-60, 60)
turtle.pendown()
turtle.begin_fill()
turtle.forward(120)
turtle.right(90)
turtle.forward(120)
turtle.right(90)
turtle.forward(120)
turtle.right(90)
turtle.forward(120)
turtle.right(90)
turtle.end_fill()

# Draw the outer circle
turtle.penup()
turtle.setposition(0, -170)
turtle.pendown()
turtle.circle(170)

# Draw the right handle
turtle.penup()
turtle.setheading(0)
turtle.setposition(60, 20)
turtle.pendown()
turtle.forward(50)
turtle.right(90)
turtle.forward(40)
turtle.right(90)
turtle.forward(50)

# Draw the line joining the right handle to the circle
turtle.penup()
turtle.setheading(0)
turtle.setposition(110, 0)
turtle.pendown()
turtle.forward(50)

# Draw the top handle
turtle.penup()
turtle.setheading(90)
turtle.setposition(-20, 60)
turtle.pendown()
turtle.forward(50)
turtle.right(90)
turtle.forward(40)
turtle.right(90)
turtle.forward(50)

# Draw the line joining the top handle to the circle
turtle.penup()
turtle.setheading(90)
turtle.setposition(0, 110)
turtle.pendown()
turtle.forward(50)

# Draw the left handle
turtle.penup()
turtle.setheading(180)
turtle.setposition(-60, -20)
turtle.pendown()
turtle.forward(50)
turtle.right(90)
turtle.forward(40)
turtle.right(90)
turtle.forward(50)

# Draw the line joining the left handle to the circle
turtle.penup()
turtle.setheading(180)
turtle.setposition(-110, 0)
turtle.pendown()
turtle.forward(50)

# Draw the bottom handle
turtle.penup()
turtle.setheading(270)
turtle.setposition(20, -60)
turtle.pendown()
turtle.forward(50)
turtle.right(90)
turtle.forward(40)
turtle.right(90)
turtle.forward(50)

# Draw the line joining the bottom handle to the circle
turtle.penup()
turtle.setheading(270)
turtle.setposition(0, -110)
turtle.pendown()
turtle.forward(50)

# End the program
turtle.done()

Where it Can be Found

I searched online but I was unable to find any reference to anyone using the Dame Dame symbol.

Summary

At the end of this post, we have successfully used the Python Turtle environment to draw the Dame Dame symbol.

The code for this series is available on GitHub. Please feel free to check it out.

Next time, we will look at the Epa symbol.

Support the Series

Using the Adinkra symbols, I created the Adinkra Notebooks Collection.

You can support this series by buying one of them.

Categories
Drawing Adinkra Symbols using Python

Akoma Ntoaso

Akoma Ntoaso is the symbol for understanding and agreement.

We will use the 5 pixel grid to trace out this image. The image of this is shown below:

Analyzing the Symbol

Akoma Ntoaso consists of a circle with lines radiating out from it which connect to semi-circles that are perpendicular to those lines.

The Plan to Draw Symbol

To draw this symbol, we will draw the circle first then move from one quadrant to the other and complete the shape.

We will start from the upper right quadrant and draw the semi-circle and the connecting line then move clockwise until the entire symbol is completed.

Algorithm to Draw the Symbol

The algorithm to draw the Akoma Ntoaso symbol is shown below:

  1. Move the turtle to the position (0, -70)
  2. Draw a filled circle of radius 70 pixel
  3. Draw a line in the upper left quadrant from (-165, 30) to (-30, 165)
  4. Draw a semi-circle in the upper left quadrant and fill it
  5. Draw a line from the center of the circle to the upper left semi-circle
  6. Draw a line in the upper right quadrant from (30, 165) to (165, 30)
  7. Draw a semi-circle in the upper right quadrant and fill it
  8. Draw a line from the center of the circle to the upper right semi-circle
  9. Draw a line in the lower right quadrant from (165, -30) to (30, -165)
  10. Draw a semi-circle in the lower right quadrant and fill it
  11. Draw a line from the center of the circle to the lower right semi-circle
  12. Draw a line in the lower left quadrant from (-30, -165) to (-165, -30)
  13. Draw a semi-circle in the lower left quadrant and fill it
  14. Draw a line from the center of the circle to the lower left semi-circle

Using Turtle Graphics

We will use the template.py file and rename it to akoma_ntoaso.py. However, we have to add some code to draw diagonal lines that will cut across the grid.

The code to do this is given below:

def coordinateDistance(x1, y1, x2, y2):
dx = x1 – x2
dy = y1 – y2
D = math.sqrt((dx * dx) + (dy * dy))
return D

turtle.penup()
turtle.setposition(-200, 200)
turtle.setheading(315)
diagonalLength = coordinateDistance(-200, 200, 200, -200)
turtle.pendown()
turtle.forward(diagonalLength)

turtle.penup()
turtle.setposition(-200, -200)
turtle.setheading(45)
diagonalLength = coordinateDistance(-200, -200, 200, 200)
turtle.pendown()
turtle.forward(diagonalLength)

turtle.pencolor(0, 0, 0)

turtle.setheading(0)
turtle.penup()
turtle.home()

The function coordinateDistance calculates the distance between two extreme ends of the grid.

Now we can start drawing the symbol.

The code for steps 1 and 2 is given below:

turtle.penup()
turtle.setposition(0, -70)
turtle.pendown()
turtle.begin_fill()
turtle.circle(70)
turtle.end_fill()

The generated image is show below:

For step 3 we draw a line from (-165, 30) to (-30, 165). To do this, we move the turtle to the position (-165, 30) and we set the heading to 45 degrees.

Next we find the distance between the two points we want our line to extend to and move the turtle that distance.

The code to do this is shown below:

turtle.penup()
turtle.setposition(-165, 30)
turtle.setheading(45)
diagonalLength = coordinateDistance(-165, 30, -30, 165)
turtle.pendown()
turtle.forward(diagonalLength)

The generated image is shown below:

For step 4 of the algorithm, we draw a semi-circle. The code to do this is given below:

turtle.penup()
turtle.setposition(-30, 165)
turtle.pendown()
turtle.setheading(135)
turtle.begin_fill()
turtle.circle(diagonalLength/2, 180)
turtle.end_fill()

The generated image is shown below:

Step 5 of the algorithm is easy. We return the turtle back to the center of the grid and draw a line connecting the circle to the semi-circle.

The code to do this is shown below:

turtle.penup()
turtle.home()
turtle.setheading(135)
turtle.pendown()
turtle.pensize(30)
turtle.forward(diagonalLength * 0.75)

The generated image is shown below:

Now that one part of the symbol has been drawn, you will notice that steps 6 to 8, 9 to 11 and 12 to 14 are basically a repeat of steps 3 to 5.

This means that we could use a single function to draw the symbol. I will leave this as an exercise for you.

For step 6, we move the pen to the position (30, 165) and we draw a line to position (165, 30).

The code to do this is shown below:

turtle.penup()
turtle.home()
turtle.pensize(1)
turtle.setposition(30, 165)
turtle.setheading(-45)
diagonalLength = coordinateDistance(30, 165, 165, 30)
turtle.pendown()
turtle.forward(diagonalLength)

The generated image is shown below:

Next we proceed to draw the semi-circle in the upper right quadrant. The code to do this is shown below:

turtle.penup()
turtle.setposition(165, 30)
turtle.pendown()
turtle.setheading(45)
turtle.begin_fill()
turtle.circle(diagonalLength/2, 180)
turtle.end_fill()

The generated image is shown below:

Now we draw the line to join the semi-circle to the circle.

The code to do this is shown below:

turtle.penup()
turtle.home()
turtle.setheading(45)
turtle.pendown()
turtle.pensize(30)
turtle.forward(diagonalLength * 0.75)

The generated image is shown below:

Next we draw a line in the lower right quadrant. The code to do this is shown below:

turtle.penup()
turtle.home()
turtle.pensize(1)
turtle.setposition(165, -30)
turtle.setheading(-135)
turtle.pendown()
turtle.forward(diagonalLength)

The generated image is shown below:

We now draw the semi-circle. The code to do this is shown below:

turtle.penup()
turtle.setposition(30, -165)
turtle.pendown()
turtle.setheading(-45)
turtle.begin_fill()
turtle.circle(diagonalLength/2, 180)
turtle.end_fill()

The generated image is shown below:

Now we connect the semi-circle to the center circle. The code to do this is shown below:

turtle.penup()
turtle.home()
turtle.setheading(-45)
turtle.pendown()
turtle.pensize(30)
turtle.forward(diagonalLength * 0.75)

Next we draw a line in the lower left quadrant. The code to do this is shown below:

turtle.penup()
turtle.home()
turtle.pensize(1)
turtle.setposition(-30, -165)
turtle.setheading(135)
turtle.pendown()
turtle.forward(diagonalLength)

We now draw the semi-circle. The code to do this is shown below:

turtle.penup()
turtle.setposition(-165, -30)
turtle.pendown()
turtle.setheading(225)
turtle.begin_fill()
turtle.circle(diagonalLength/2, 180)
turtle.end_fill()

The generated image is shown below:

Finally, we draw the line connecting the semi-circle to the center circle. The code to do this is shown below:

turtle.penup()
turtle.home()
turtle.setheading(225)
turtle.pendown()
turtle.pensize(30)
turtle.forward(diagonalLength * 0.75)

The generated image is shown below:

Complete Code

"""
Project Name: Drawing Adinkra Symbols using Python
Symbol Name: Akoma Ntoaso
Developer Name: Truston Ailende
Email Address: trustonailende@gmail.com
"""
import turtle
import math
 
# Square
def drawSquare(length):
    turtle.penup()
    turtle.setposition(-length/2.0, length/2.0)
    turtle.pendown()
    for i in range(0, 4):
        turtle.forward(length)
        turtle.right(90)
    turtle.penup()
    turtle.home()
 
# Horizontal lines
def drawHorizontalLine(length, division):
    pixelSpace = int(length / division)
    half = int(length / 2)
    for j in range((-half + pixelSpace), half, pixelSpace):
        turtle.penup()
        turtle.setposition(-half, j)
        turtle.pendown()
        turtle.forward(length)
    turtle.penup()
    turtle.home()
 
# Vertical lines
def drawVerticalLine(length, division):
    pixelSpace = int(length / division)
    half = int(length / 2)
    turtle.right(90)
    for k in range((-half + pixelSpace), half, pixelSpace):
        turtle.penup()
        turtle.setposition(k, half)
        turtle.pendown()
        turtle.forward(length)
    turtle.penup()
    turtle.home()
 
# Draw the grid
turtle.speed(1000000)
drawSquare(400)
drawHorizontalLine(400, 40)
drawVerticalLine(400, 40)
 
# Change the colour mode
turtle.colormode(255)
 
# Change the pencolor to red
turtle.pencolor(255, 0, 0)
 
# Draw the horizontal centre line
turtle.setposition(-200, 0)
turtle.pendown()
turtle.forward(400)
turtle.penup()
 
# Draw the vertical centre line
turtle.setposition(0, 200)
turtle.setheading(270)
turtle.pendown()
turtle.forward(400)
 
# Reset all the properties
turtle.home()
 
# Place code here
def coordinateDistance(x1, y1, x2, y2):
    dx = x1 - x2
    dy = y1 - y2
    D = math.sqrt((dx * dx) + (dy * dy))
    return D

# Draw the top left diagonal
turtle.penup()
turtle.setposition(-200, 200)
turtle.setheading(315)
diagonalLength = coordinateDistance(-200, 200, 200, -200)
turtle.pendown()
turtle.forward(diagonalLength)

# Draw the bottom left diagonal
turtle.penup()
turtle.setposition(-200, -200)
turtle.setheading(45)
diagonalLength = coordinateDistance(-200, -200, 200, 200)
turtle.pendown()
turtle.forward(diagonalLength)

# Reset the colour back to black
turtle.pencolor(0, 0, 0)

# Reset the heading to 0
turtle.setheading(0)
turtle.penup()
turtle.home()

# Draw the center circle
turtle.penup()
turtle.setposition(0, -70)
turtle.pendown()
turtle.begin_fill()
turtle.circle(70)
turtle.end_fill()

# Draw a line in the upper left quadrant
turtle.penup()
turtle.setposition(-165, 30)
turtle.setheading(45)
diagonalLength = coordinateDistance(-165, 30, -30, 165)
turtle.pendown()
turtle.forward(diagonalLength)

# Draw a semi-circle in the upper left quadrant
turtle.penup()
turtle.setposition(-30, 165)
turtle.pendown()
turtle.setheading(135)
turtle.begin_fill()
turtle.circle(diagonalLength/2, 180)
turtle.end_fill()

# Draw a line from the center of the circle to the upper left semi-circle
turtle.penup()
turtle.home()
turtle.setheading(135)
turtle.pendown()
turtle.pensize(30)
turtle.forward(diagonalLength * 0.75)

# Draw a line in the upper right quadrant
turtle.penup()
turtle.home()
turtle.pensize(1)
turtle.setposition(30, 165)
turtle.setheading(-45)
diagonalLength = coordinateDistance(30, 165, 165, 30)
turtle.pendown()
turtle.forward(diagonalLength)

# Draw a semi-circle in the upper right quadrant
turtle.penup()
turtle.setposition(165, 30)
turtle.pendown()
turtle.setheading(45)
turtle.begin_fill()
turtle.circle(diagonalLength/2, 180)
turtle.end_fill()

# Draw a line from the center of the circle to the upper right semi-circle
turtle.penup()
turtle.home()
turtle.setheading(45)
turtle.pendown()
turtle.pensize(30)
turtle.forward(diagonalLength * 0.75)

# Draw a line in the lower right quadrant
turtle.penup()
turtle.home()
turtle.pensize(1)
turtle.setposition(165, -30)
turtle.setheading(-135)
turtle.pendown()
turtle.forward(diagonalLength)

# Draw a semi-circle in the lower right quadrant
turtle.penup()
turtle.setposition(30, -165)
turtle.pendown()
turtle.setheading(-45)
turtle.begin_fill()
turtle.circle(diagonalLength/2, 180)
turtle.end_fill()

# Draw a line from the center of the circle to the lower right quadrant
turtle.penup()
turtle.home()
turtle.setheading(-45)
turtle.pendown()
turtle.pensize(30)
turtle.forward(diagonalLength * 0.75)

# Draw a line in the lower left quadrant
turtle.penup()
turtle.home()
turtle.pensize(1)
turtle.setposition(-30, -165)
turtle.setheading(135)
turtle.pendown()
turtle.forward(diagonalLength)

# Draw a semi-circle in the lower left quadrant
turtle.penup()
turtle.setposition(-165, -30)
turtle.pendown()
turtle.setheading(225)
turtle.begin_fill()
turtle.circle(diagonalLength/2, 180)
turtle.end_fill()

# Draw a line from the center of the circle to the lower left quadrant
turtle.penup()
turtle.home()
turtle.setheading(225)
turtle.pendown()
turtle.pensize(30)
turtle.forward(diagonalLength * 0.75)

# End the program
turtle.done()

Where Can it Be Found?

A search of Google for organizations using this symbol yielded the Ghana Stock Exchange. It is the logo of the organization.

Summary

At the end of this post, we have successfully used the Python Turtle environment to draw the Akoma Ntoaso symbol.

The code for this series is available on GitHub. Please feel free to check it out.

Next time, we will look at the Dame Dame symbol.

Support the Series

Using the Adinkra symbols, I created the Adinkra Notebooks Collection.

You can support this series by buying one of them.

Categories
Python Programming

Learn Python in One Week (May Edition)

It’s Never too Late to Learn a New Skill! Learn to Code

Anyone can learn to code regardless of age, degree or gender.

Always wanted to become a coder? Here is a chance for you to fulfill your dream.

Join Truston Ailende for one week of instensive Python programming for absolute beginners.

If you are ready to learn how to code, click the link below:

https://bit.ly/33ImVXL

Categories
Python Turtle

How to Draw a Bicycle using Python Turtle

This tutorial covers how to draw a bicycle using Python Turtle. To draw with code in the Python programming language we can use the Python Turtle module which comes by default with the Python interpreter.

Drawing a bicycle using the Python Turtle module involves the following steps:

  1. Get an image of the bicycle you want to draw
  2. Get a grid for the image of the bicycle
  3. Combine the grid and the image of the bicycle into a single image
  4. Draw centre lines on the image of the bicycle
  5. Draw the bicycle using Python Turtle

Get an Image

A picture of a bicycle cannot be drawn with code. Rather, an icon should be used.

A suitable source of icon is The Noun Project. The landing page of the site is shown below:

The Noun Project Landing Page

Login or Join. This will enable you to download your icon. Type in the kind of image you want as shown below:

Icon Search

Pressing the search icon will bring out search items as shown below:

Search Results

Scroll down until you find a suitable icon then you click on it. This will take you to the screen shown below:

Selected Image

Click on the Get this icon green button. It will display the screen shown below:

Features

Select the Basic Download checkbox. The button will now turn green as shown below:

Basic Download

Click on the Continue button. This will show the screen shown below:

Final Download

Click on the PNG button. It will download an icon of image type PNG.

Get a Grid

To draw the icon, you need to use a grid. The grid allows you to get the coordinates of the points that you want to draw.

A good online tool to us is the Grid Drawing Tool. The landing page is shown below:

Grid Drawing Tool

Clicking on the start button will take you to the screen shown below:

Upload Screen

Click on the Choose file button. Navigate to the location of the image you downloaded on your machine.

The uploaded screen should look as shown below:

Uploaded Image

Clicking the Next button takes the user to the Rotate screen shown below:

Rotate Screen

Click the Next button to go to the Crop Screen which is shown below:

Crop Screen

Click on the Next button to get to the next screen.

Adjust Screen

Click on the Next button to get the grid. This is shown below:

Grid

Adjust the Grid dimensions to 20 and a line style of 1 pixels. Click the apply grid button as shown below:

Final Grid

Click on the Download button to get the grid image.

Combine the Grid and Image

Once you have your grid and image, you need to combine them into a single image.

To do this you can use any image editing software. However, in my case, I shall use Paint.NET.

Open the image in Paint.NET as shown below:

Paint.NET User Interface

Next import the grid as a layer. To do this, go to the Layers menu and click on Import From File… as shown below:

Import From File

When you import the grid, you will notice that the number of layers becomes two as shown below:

Two Layers

Now click on the Magic Wand. This will enable you to make the black areas transparent. The shortcut for the Magic Wand is S.

Click on a square in and press delete. This will give you the result shown below:

Upper Square

Increase the tolerance slightly until it spreads across the layer as shown below:

Increased Tolerance

Now press the delete button on your computer. It will give you the result shown below:

Deleted Spots

Now reduce the tolerance to 50% and delete all the dark spots. The final image will look as shown below:

Cleaned Image

Now we flatten the image. This will reduce the number of layers to one. The way to do this shown below:

Flatten

Press the Save button to save your image. You can now close Paint.NET. The image is now as shown below:

Modified Image

Draw Centre Lines

Drawing the centre lines is a matter of using the paint software on the computer.

Open the image in Microsoft Paint as shown below:

Microsoft Paint

Reduce the size of the image to 50%. This will enable you see the entire image as shown below:

Image Scaled Down

Under shapes in Microsoft Paint, click on line shape and set its colour to red. Also choose the maximum size.

Now draw the centre lines from one end of the image to the other. This will be as shown below:

Centre Lines

Press Ctrl + S to save the image and close Microsoft Paint. The final image will look as shown below:

Modified Image with Centre Lines

Draw the Bicycle

The bicycle will be drawn in stages. We will start with the wheels and draw the rest of the shape.

To start we shall import the Python Turtle module to our code, make use of the delay function and increase the pen size of our turtle.

Our code will look as shown below:

import turtle
turtle.delay(10)
turtle.pensize(20)

The resulting image is shown below:

The turtle is at the centre of the screen

Next we move to the right of the left bicycle and set the heading o the bicycle to 90 degrees. The coordinate of that position is (-3.5, -2). We shall use a scale factor of 25 so the coordinates are (-87.5, -50).

The code to do this is shown below:

# Move the turtle to the right position of the left tyre
turtle.penup()
turtle.setposition(-87.5, -50)
turtle.setheading(90)

The resulting image is shown below:

Move the turtle to Left Tyre

Next we draw the circle. The radius of the circle is 2.5. Scaled by 25, this is 62.5.

The code to draw the circle is shown below:

# Draw the left tyre
turtle.begin_fill()
turtle.circle(87.5)
turtle.end_fill()

The resulting image is shown below:

Left Tyre Drawn

Next we draw the right tyre. The turtle is moved to the right side of the right tyre and then, the circle is drawn. The position we are trying to get to is (8.5, 2). Scaled up by 25, this is (212.5, -50).

The code to do this is shown below:

# Move the turtle to the right position of the right tyre
turtle.penup()
turtle.setposition(212.5, -50)
turtle.setheading(90)

The resulting figure is shown below:

Set Heading for Right Tyre

Now draw the right tyre like you did the left tyre. The code to do this is shown below:

# Draw the right tyre
turtle.begin_fill()
turtle.circle(62.5)
turtle.end_fill()

The generated image is shown below:

Right Tyre Drawn

Now on to drawing the handles. We draw the line from (5.5, 0) to (3, 6). Scaled up by 25, this will be from (137.5, 0) to (75, 150).

The code to do this is shown below:

# Draw the handle
turtle.penup()
turtle.setposition(137.5, 0)
turtle.pendown()
turtle.setposition(75, 150)

The generated image is shown below:

Drawing the Handle Bar

Next we draw the handle. The handle is made up of two parts: a straight part and a semicircle. The straight part will be drawn first.

To draw the straight part, we draw a line from the position (3, 6) to (5, 6). Scaled by 25, this is (75, 150) to (125, 150).

The code to do this is shown below:

# Draw the straight part of the handle
turtle.setheading(0)
turtle.setposition(125, 150)

The generated image is shown below:

Straight Handle

Now we draw the semicircle. The code to do this is shown below:

# Draw the circular part of the handle
turtle.penup()
turtle.setposition(125, 100)
turtle.pendown()
turtle.circle(25, 180)

The generated image is shown below:

Handle Completed

Next is to draw the seat. The icon shows a seat tube and saddle. We draw the seat tube first.

The code to do this is shown below:

# Draw the seat tube
turtle.penup()
turtle.setposition(12.5, -50)
turtle.pendown()
turtle.setposition(-62.5, 137.5)

The generated image is shown below:

Seat Tube

Next we draw the seat. The key to drawing the seat is to set the heading of the turtle to 180 degrees. Next we draw a line from the current location to the position (-100, 137.5).

The code to do this is shown below:

# Draw the saddle
turtle.setheading(180)
turtle.setposition(-100, 137.5)

The generated image is shown below:

Saddle Drawn

To complete the drawing of the bicycle, we will display the diagram of a bicycle which is shown below:

Labelled Bicycle Diagram

The parts that are remaining to be drawn are the top tube, down tube, seat stay and chain stay.

To draw the top tube we draw a line from the position (100, 75) to (-37.5, 75). The code to do this is shown below:

# Draw the top tube
turtle.penup()
turtle.setposition(100, 75)
turtle.pendown()
turtle.setposition(-37.5, 75)

The generated image is shown below:

Top Tube Drawn

To draw the down tube we draw a line from the position (100, 75) to (12.5, -50) . The code to do this is shown below:

# Draw the down tube
turtle.penup()
turtle.setposition(100, 75)
turtle.pendown()
turtle.setposition(12.5, -50)

The generated image is shown below:

Down Tube Drawn

To draw the seat stay we draw a line from the position (-37.5, 75) to (-150, -50). The code to do this is shown below:

# Draw the seat stay
turtle.penup()
turtle.setposition(-37.5, 75)
turtle.pendown()
turtle.setposition(-150, -50)

The generated image is shown below:

Seat Stay Drawn

To draw the chain stay we draw a from position (12.5, -50) to (-150, -50). The code to do this is shown below:

# Draw the chain stay
turtle.penup()
turtle.setposition(12.5, -50)
turtle.pendown()
turtle.setposition(-150, -50)

The generated image is shown below:

Chain Seat Drawn

At the end of this tutorial, we have successfully drawn a bicycle using the Python Turtle module.

The code for this tutorial is available in its entirety as a GitHub Gist.

Learn Python in One Week

If you want a quick and easy introduction to the Python programming language, you should check out my fifth book: Learn Python in One Week on the Amazon store.

Categories
Python Turtle

How to Draw a Car using Python Turtle

Drawing with code in the Python programming language involves using the Python Turtle module. Python Turtle comes by default with the Python interpreter.

This tutorial covers how to draw a car using the Python Turtle module. Drawing the car involves the following steps:

  1. Get an image of the car you want to draw
  2. Get a grid for the picture of the car
  3. Combine the grid and picture of the car into a single image
  4. Draw center lines on the image of the car
  5. Draw the car using the Python Turtle module

Get an Image of the Car

You cannot replicate a photo using Python Turtle so we need to find a suitable icon.

A great source for icons is The Noun Project. Once you visit the site, you simply search for the kind of image you are looking for.

Get a Grid

Drawing the car with Python Turtle would be impossible without a grid. The grid gives you the coordinates of the points of the image that you want to draw.

I couldn’t find a suitable grid maker for my purposes so I used the grid from the Grid Drawing Tool. You customise the grid you want to your requirements and it gives you the image you want.

Combine the Grid

The grid from the Grid Drawing Tool is then taken and made transparent. The grid and image to be drawn are then combined in such a way that the image is on top of the grid.

A simple software for this is Paint.NET. It is a free software for image editing.

Draw Center Lines

The Python Turtle module has it origins at the points (0, 0). So to draw a image, you need its center lines.

This is easily done using the Paint software available on Windows. The image will now look like this:

Car image with center lines

Draw the Car

The car will be drawn in stages. We shall start from the leftmost position and draw the composite shapes that make up the car until we are done.

To start we shall import the Python Turtle module to our code, make use of the delay function and increase the pen size of our turtle.

Our code will look as shown below:

import turtle
turtle.delay(10)
turtle.pensize(20)

The resulting image is show below:

The turtle is displayed at the center of the screen

Next we move the turtle to the extreme left position. On the image with the center lines, this is (-8, -3). We want to draw with a scale factor of 25 so any coordinate we get, we will multiply by 25. The code to do this is shown below:

# Move the turtle to the extreme left position
turtle.penup()
turtle.setposition(-200,-75)

The resulting image is shown below:

The turtle has moved to the leftmost position

The goal now is to draw a rectangle of length 400 and width 150. The code to do this is show below:

# Draw the rectangle
turtle.pendown()
turtle.forward(400)
turtle.setheading(90)
turtle.forward(150)
turtle.setheading(180)
turtle.forward(400)
turtle.setheading(-90)
turtle.forward(150)

The resulting image is shown below:

The rectangle has been drawn

Next the left tyre should be drawn. To do this, we move the turtle to the leftmost position of the tyre which is at (-7, -3) scaled up to (-175, -75) and we draw the tyre.

The code to do this is shown below:

# Draw the left tire
turtle.penup()
turtle.setposition(-175,-75)
turtle.pendown()
turtle.forward(50)
turtle.setheading(0)
turtle.forward(75)
turtle.setheading(90)
turtle.forward(50)

The resulting image is shown below:

Left tyre drawn

Next the right tyre is drawn. To do so, we move the turtle to the position (100, -75) and we draw the right tyre.

The code to do this is shown below:

# Draw the right tire
turtle.penup()
turtle.setposition(100,-75)
turtle.pendown()
turtle.setheading(270)
turtle.forward(50)
turtle.setheading(0)
turtle.forward(75)
turtle.setheading(90)
turtle.forward(50)

The generated image is shown below:

Right tyre drawn

Now we intend to draw the uppermost line. The left coordinate of the line is at (-5.5, 4). Scaled up, this is (-137.5, 200). The goal would be to draw a line from that point to the point (137.5, 200).

The code to do this is shown below:

# Draw the uppermost line
turtle.penup()
turtle.setposition(-137.5,200)
turtle.pendown()
turtle.setheading(0)
turtle.forward(275)

The generated image is shown below:

Topmost upper line drawn

Next we draw the second uppermost line. To do so, we move the turtle to the position (-150, 150) and draw a line of length 300 pixels.

The code to do this is shown below:

# Draw the second uppermost line
turtle.penup()
turtle.setposition(-150,150)
turtle.pendown()
turtle.setheading(0)
turtle.forward(300)

The resulting image is shown below:

Second uppermost line drawn

To complete the outline of the car, we need to draw the left and right slanting lines.

To draw the left slanting line, we simply draw a line from the tip of the uppermost line to where the left slanting line should touch the rectangle.

The code to do this is shown below:

# Draw the left slanted line
turtle.penup()
turtle.setposition(-137.5,200)
turtle.pendown()
turtle.setposition(-175,75)

The generated image is shown below:

Left slanting line drawn

To draw the right slanting line, we draw a line from the tip of the topmost line to the point where the right slanting line touches the rectangle.

The code to do this is shown below:

# Draw the right slanted line
turtle.penup()
turtle.setposition(137.5,200)
turtle.pendown()
turtle.setposition(175,75)

The generated image is shown below:

Right slanting line drawn

Now we can draw the car proper. The headlamps will be drawn first. The left headlamp is to be drawn first as a circle.

The code to do this is shown below:

# Draw the left circle
turtle.penup()
turtle.setposition(-125,-25)
turtle.pendown()
turtle.circle(25)

The generated image is shown below:

Left headlamp drawn

Next we draw the right headlamp. The code to do this is shown below:

# Draw the right circle
turtle.penup()
turtle.setposition(125,-25)
turtle.pendown()
turtle.circle(25)

The generated image is shown below:

Right headlamp drawn

Now the wipers are to be drawn. The left wiper will be drawn first. THe code to do this is shown below:

# Draw the left wiper
turtle.penup()
turtle.setposition(-87.5, 75)
turtle.pendown()
turtle.setposition(-50, 112.5)

The generated image is shown below:

Left wiper drawn

Next we draw the right wiper. The code to do this is shown below:

# Draw the right wiper
turtle.penup()
turtle.setposition(62.5, 75)
turtle.pendown()
turtle.setposition(100, 112.5)

The generated image is shown below:

Right wiper drawn

Finally the 4 horizontal lines are drawn. The code to do this is shown below:

# Draw the horizontal lines
turtle.penup()
turtle.setposition(-50, -37.5)
turtle.pendown()
turtle.setposition(-50, 0)
turtle.penup()
turtle.setposition(-15, -37.5)
turtle.pendown()
turtle.setposition(-15, 0)
turtle.penup()
turtle.setposition(15, -37.5)
turtle.pendown()
turtle.setposition(15, 0)
turtle.penup()
turtle.setposition(50, -37.5)
turtle.pendown()
turtle.setposition(50, 0)

The generated image is shown below:

Car completed

At the end of this tutorial, we have successfully drawn a car using the Python Turtle module in the Python programming language.

The code in its entirety is available as a GitHub Gist. You can find it here.

Learn Python in One Week

If you want a quick and easy introduction to the Python programming language, you should check out my fifth book: Learn Python in One Week on the Amazon store.