Categories
Drawing Adinkra Symbols using Python

Aban

Aban means “fortress”. It is the symbol of strength and authority.

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

Aban on a Grid

Analysing the Symbol

The symbol is a composite symbol made up of 4 symbols that look like tongs.

A single tong is symmetrical in its quadrant. This property means that we draw a tong and use the steps to draw others.

The Plan to Draw the Symbol

We shall move the pen to the position (0, 80). Once we do this, we shall set the heading of the pen to 45 degrees thus drawing the tong in the upper right quadrant.

Once the drawing of the first tong is completed, the position of the turtle will place it on the x axis on the right.

At that point, we simply set the heading of the pen to 315 degrees and draw the second tong. When the second tong is drawn, the turtle will be on the negative y axis.

At this point, we simply set the heading of the pen to 225 degrees and draw the third tong. When the third tong is drawn, the turtle will be on the negative x axis.

At this point, we simple set the heading of the pen to 135 degrees and draw the fourth tong thus completing our shape.

Algorithm to Draw the Symbol

The algorithm to draw the Aban symbol is shown below:

  1. Lift up the pen
  2. Set the heading to 45 degrees
  3. Move the pen to the position (0, 80)
  4. Set the pen size to half the width of the shape
  5. Place the pen down
  6. Move forward to draw the first line
  7. Turn right by 90 degrees
  8. Move forward to draw half of the first line
  9. Turn left by 90 degrees
  10. Move forward to draw the protruding line at a distance of 5/8 of the length
  11. Lift up the pen
  12. Reverse the turtle by moving back the same distance as you moved forward
  13. Turn right by 90 degrees
  14. Place the pen down
  15. Draw the second half of the line
  16. Turn right by 90 degrees
  17. Move forward to draw the third line
  18. Change the heading to 315 and repeat steps 6 – 17
  19. Change the heading to 225 and repeat steps 6 – 17
  20. Change the heading to 135 and repeat steps 6 – 17

Using Turtle Graphics

We will use the template.py file and rename it to aban.py.

The code for steps 1 to 3 is given below:

turtle.penup()
turtle.setheading(45)
turtle.setposition(0, 80)

In step 4 of the algorithm to draw Aban, we have to find the width of the shape. We will also need to find the length of the shape.

To find the width of the line, we need to find the distance between two parallel points it passes through and this are: (120, 0) and (40, 80).

To find the length of a side of the Aban symbol, we take the point (0, 80) as our starting point and get the coordinates of the centre point (80, 160) which is where a line drawing the symbol would stop and turn.

We have to introduce a function coordinateDistance to give us the distance between the two coordinates. The function definition is shown below:

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

To find the width and length, we use the expressions below:

width = coordinateDistance(120, 0, 40, 80)
length = coordinateDistance(0, 80, 80, 160)

The code for steps 4 and 5 is shown below:

turtle.pensize(width / 2)
turtle.pendown()

Since steps 6 to 17 will be repeated 3 more times, they are a good candidate for a function. We shall call this function drawTong.

Steps 6 to 17 can be automated using the drawTong function. We will first write out the commands for the steps to check then we will later integrate them into the drawTong function.

The code for steps 6 to 10 is given below:

turtle.forward(length)
turtle.right(90)
turtle.forward(length / 2)
turtle.left(90)
turtle.forward(length * 5 / 8)

The generated image is shown below:

Half Tong
Half Tong

The code to lift the pen is straightforward enough. To reverse the shape, we need to move backward the same number of steps as forward.

The code to do this is shown below:

turtle.penup()
turtle.backward(length * 5 / 8)
turtle.right(90)
turtle.pendown()
turtle.forward(length / 2)
turtle.right(90)
turtle.forward(length)

The generated image is shown below:

Full Tong
Full Tong

Now that we have drawn one part of the symbol, we can use the code to create the drawTong function so that we don’t have to repeat ourselves.

The code for the function is shown below:

def drawTong():
turtle.forward(length)
turtle.right(90)
turtle.forward(length / 2)
turtle.left(90)
turtle.forward(length * 5 / 8)
turtle.penup()
turtle.backward(length * 5 / 8)
turtle.right(90)
turtle.pendown()
turtle.forward(length / 2)
turtle.right(90)
turtle.forward(length)

To draw the shape completely, we need to call this function and alternate with the setheading angle that is representative of the quadrant the function will draw in. The code to do this is shown below:

drawTong()
turtle.setheading(315)
drawTong()
turtle.setheading(225)
drawTong()
turtle.setheading(135)
drawTong()

The generated image is shown below:

Completed Symbol
Completed Symbol

Complete Code

"""
Project Name: Drawing Adinkra Symbols using Python
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
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
turtle.penup()
turtle.setheading(45)
turtle.setposition(0, 80)

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

width = coordinateDistance(120, 0, 40, 80)
length = coordinateDistance(0, 80, 80, 160)

turtle.pensize(width/2)
turtle.pendown()

def drawTong():
    turtle.forward(length)
    turtle.right(90)
    turtle.forward(length / 2)
    turtle.left(90)
    turtle.forward(length * 5 / 8)
    turtle.penup()
    turtle.backward(length * 5 / 8)
    turtle.right(90)
    turtle.pendown()
    turtle.forward(length / 2)
    turtle.right(90)
    turtle.forward(length)

drawTong()
turtle.setheading(315)
drawTong()
turtle.setheading(225)
drawTong()
turtle.setheading(135)
drawTong()

# End the program
turtle.done()

Where Can It Be Found?

A brief Google search led me to the following places where you can get branded Aban products:

  1. Aban Zanzibar Trinity Rings on Simbasante
  2. Socks by Adinkra Republic
  3. Protection Adinkra Earrings by Ahima
  4. Aban Gold Pendant on Amazon
  5. Aban Wooden Stamp on Etsy

Summary

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

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

Next time, we will look at the Abusua Pa Adinkra symbol

Support this Series

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

You can support this series by buying one of them.

Categories
Python Programming

Programming Languages

Computer programming languages allow us to give instructions to a computer in a language the computer understands.

The diagram above shows how programming languages hide details from the user.

At the base of the diagram, we have the computer hardware.

The hardware refers to the part of the computer that we can see and touch.

To control a computer, we need to use a machine language. Each computer has it’s own machine language and programmers found it hectic to have to learn because it was in binary.

As time when on, the Assembly Language was developed. Assembly Language provided a symbolic representation of the machine code needed to program a given CPU architecture.

The real breakthrough in computer programming would start with the arrival of high level languages.

High level languages were closer to English and in addition to making the task of computer programming easier, they also opened the field to a lot more people.

The top four from that period in computing history are C, BASIC, FORTRAN and COBOL.

Applications running this languages are still in use today.

We will be learning the Python programming language.

According to the TIOBE Index as at January 2020, the Python programming language is the 3rd most popular programming language in the world.

Each language has it’s strength and weakness but for absolute beginners, I will give my reasons why recommend learning the Python programming language first.

Simplicity

When you are starting out on the journey of learning to code, you don’t want to deal with problems that occur while setting up your computer.

While learning Java at the University, one of the biggest issues we would run into was the setting of classpath.

The issue of classpath would stop many beginners dead in their tracks.

In Python, once you have installed the interpreter, you are ready to start programming.

In addition, the language is simple and readable. The commands in the language look like English.

Versatility

Python is a general purpose language that caters to every use case that you can imagine.

It might not beat a specialized language in its domain but it will allow you get started in any domain.

Large Community

The true secret weapon of the Python programming language is the community.

The versatility of the Python programming language is due to the libraries developed by the community.

Conclusion

As technology become more integrated with everyday life, learning how to program will become important in being a member of the society.

For absolute beginners, the Python programming language is great for starting because it is simple and easy to use.

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
Drawing Adinkra Symbols using Python

Adinkrahene

Adinkrahene is the symbol for leadership and charisma. This symbol is reportedly the inspiration for the design of the other symbols. Its simple yet abstract design, consisting of three concentric circles emphasizes the importance of ideas and abstract concepts.

In the last section, I introduced the grid. The grid will allow us create any symbol we want to because we can analyse and replicate the symbol of our choice.

We will draw a grid of 5 pixel squares on the image of the Adinkra symbol of our choice and replicate that on our grid. The squares on our grid are 10 pixels each. The goal of our program would be to create a grid that is two times the size of the original grid.

Drawing the 5 pixel squares on the Adinkrahene symbol will look as shown below:

Adinkrahene on a Grid
Adinkrahene on a Grid

Analysing the Symbol

The first circle is 8 squares away from the centre of the circle. So multiplying by 5 would give us 40 pixels.

The second circle is 13 pixels away from the centre of the circle. So multiplying by 5 would give us 65 pixels.

The third circle is 18 pixels away from the centre of the circle. So multiplying by 5 would give us 90 pixels.

The outer circle doesn’t touch the edge of the grid. It leaves a space of 1 square. The thickness of the circles are all 10 pixels.

The Plan to Draw the Symbol

To draw the Adinkahene symbol, we have to compensate for scaling up the shape to twice its size.

As a result of this, we shall increase the size of our pen to 20 pixels.

The first circle will have a radius of 80 pixels. We will move the pen to the location (0, -80) and draw a circle of 80 pixels.

The second circle will have a radius of 130 pixels. We will move the pen to the location (0, -130) and draw a circle of 130 pixels.

The third circle will have a radius of 180 pixels. We will move the pen to the location (0, -180) and draw a circle of radius 180 pixels.

Algorithm to Draw the Symbol

The algorithm to draw the Adinkrahene symbol is shown below:

  1. Lift up the pen
  2. Increase the pensize to 20 pixels
  3. Move the turtle to the position (0, -80)
  4. Place the pen down
  5. Draw a circle that is 80 pixels in radius
  6. Lift up the pen
  7. Move the turtle to the position (0, -130)
  8. Place the pen down
  9. Draw a circle that is 130 pixels in radius
  10. Lift up the pen
  11. Move the turtle to the position (0, -180)
  12. Place the pen down
  13. Draw a circle that is 180 pixels in radius

Using Turtle Graphics

We will use the template.py file and rename it to adinkrahene.py.

The first step in drawing the Adinkrahene symbol is to draw the innermost circle and work our way out.

The first task is to lift up the pen. The code to do this is shown below:

turtle.penup()

Next we will increase our pensize to 20. The code to do this is shown below:

turtle.pensize(20)

The origin of the turtle is at (0, 0). To move the turtle to the position (0, -80), we use the command shown below:

turtle.setposition(0, -80)

At this point, place the pen down and draw a circle of radius 80 pixels. The code to do this is shown below:

turtle.pendown()
turtle.circle(80)

Our Python Turtle window will look like shown below:

Inner concentric circle of Adinkrahene
Inner Concentric Circle

Steps 6 to 9 are the steps for drawing the second circle. The code is similar to what you have already seen before and is shown below:

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

When we run the code, our second circle now appears as shown below:

Middle Circle of Adikrahene
Middle Concentric Circle

Steps 10 to 13 will draw the third circle. The code to do this is similar to what you have already seen before and is shown below:

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

When you run the code, the image is as shown below:

Adinkrahene
Final Image

Complete Code

The code for drawing the symbol is shown below:

"""
Project Name: Drawing Adinkra Symbols using Python
Symbol Name: Adinkrahene
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
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
# Lift up the pen
turtle.penup()

# Set the pensize
turtle.pensize(20)

# Draw the first circle
turtle.setposition(0, -80)
turtle.pendown()
turtle.circle(80)

# Draw the second circle
turtle.penup()
turtle.setposition(0, -130)
turtle.pendown()
turtle.circle(130)

# Draw the third circle
turtle.penup()
turtle.setposition(0, -180)
turtle.pendown()
turtle.circle(180)

# End the program
turtle.done()

Where Can It Be Found?

A brief Google search led me to the following places where you can get branded Adinkrahene products:

  1. TShirts, Mugs and Phone Cases on TeePublic
  2. Pendants on Purple Blessing
  3. Socks on Adinkra Republic

Summary

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

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

Support this Series

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

You can support this series by buying one of them.

Categories
Python Programming

Why We Program

A computer is a machine that processes information under the control of a program.

By seeing the computer as a machine, you can then come to appreciate it like any device around you.

What however makes the computer special is the fact that you can program it.

What is Programming?

Computer programming is a way of giving computers instructions about what they should do in order to complete a set task.

A computer program will have the ability to take input data from a device such as a keyboard, process calculations, and make decisions based on certain conditions.

The practice of programming is one of teaching the computer to do something.

You must know how to do it yourself before you can teach a computer to do it for you.

Programming Languages

The instructions to execute the task that you want the computer to do for your are given to it in a programming language.

A programming language is a language that the computer understands.

Using a programming language, you are then able to transfer the logic of how you solved a problem to the computer.

To learn a programming language, you should be able to do the following with it:

  1. Take inputs
  2. Produce outputs
  3. Declare variables
  4. Use mathematical expressions
  5. Use conditional statements such as IF/ELSE
  6. Using looping statements such as WHILE
  7. Write functions

The Purpose of Programming

The purpose of programming is to automate processes.

When we have a problem that we want to solve, we list out the steps to solve that problem.

The sequence of steps we create is the algorithm.

The algorithm embodies all the logic we need to solve a problem.

With a well designed algorithm, we are confident that if we follow the steps of the algorithm, the result will always be correct.

We then take this algorithm and express it in a programming language as a program.

Once we have the program, the computer will handle the task for us.

We can then free up the time for our task until something goes wrong or we need to update the program’s logic.

This is the essence of programming.

Conclusion

Programming is a task that deals with the automation of the solution to a problem.

It is difficult hence the need to understand why it is needed before you do it.

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 for Financial Mathematics

Simple Interest

The concept of Simple Interest is one of the first things anyone learns in Financial Mathematics.

It isn’t really hard to program.

The formula I am using is given below:

Simple Interest Formula

The code to program this is shown below:

# Print out the purpose of the program
print("This program computes the compound amount using simple interest")
print("Reference: An Undergraduate Introduction to Financial Mathematics (Page 2)\n")

# Ask the user for the initial deposit
principal = float(input("Please enter the initial deposit: "))

# Ask the user for the rate as a percentage
rate = float(input("Please enter the rate: "))

# Ask the user for the number of years
years = float(input("Please enter the number of years: "))

# Compute the compounded amount
rate = rate / 100
compounded_amount = principal * ((1 + rate)**years)
print("\nThe compounded amount is:", round(compounded_amount, 2))

You can also find the code on GitHub.