This is the first part of a study on how shortest path algorithms work. This study is divided into three parts.

    1. Draw an empty board
    1. Add obstacles randomly
    1. Implement the shortest path algorithm

# 1 - Dependencies

Before we start writing code, we need to install the python library Pillow.

pip install Pillow

To draw the board, we need to import Image and ImageDraw classes from the Pillow library.

from PIL import Image, ImageDraw

*PIL is short for Python Image Library

# 3 - Classes

Now that we have our dependencies installed, let’s start defining the Node class.

The Node class will hold each position of the board.

class Node:
	    Each node on the board
	    # size of each node

	    def __init__(self, x, y, fill='#FFFFFF', outline='#000000'):
	        # define node position
	        self.x = x
	        self.y = y

	        # define node colors
	        self.fill = fill
	        self.outline = outline

	    def draw(self, base):
	        # define the start of the image
	        left = self.x * self.SIZE
	        top = self.y * self.SIZE

	        # define the end of the image
	        right = left + self.SIZE
	        bottom = top + self.SIZE

	        # shape
	        shape = [(left, top), (right, bottom)]

	        node = ImageDraw.Draw(base)   
	        node.rectangle(shape, fill=self.fill, outline=self.outline) 
view raw hosted with ❤ by GitHub

Inside the draw method, we use the ImageDraw class to create the image of the node.

The most important part is the first parameter of the rectangle method. This parameter is responsible for the positions of the pixels that the class will use to draw our node.

The shape is an array with two positions. In the first position, we define where is the top/left edge of the rectangle and in the second, the bottom/right edge.

With these definitions, the method will draw:

  • One vertical line from the left/top edge to the relative left/bottom edge;
  • One vertical line from the right/bottom edge to the relative right/top edge;
  • One horizontal line from the left/top edge to the relative right/top edge;
  • One horizontal line from the right/bottom edge to the relative left/bottom edge.

In the end, we have a rectangle.

Now we need to create the Board class, to hold the matrix of Nodes, and draw the final image.

#python #pillow #algorithms #computer-science #shortest-path #algorithms

Shortest Path Algorithm
1.45 GEEK