+1 917 8105386 [email protected]

Creating an algorithm

  Creating an algorithm for a program involves creativity and the ability to decompose a problem into smaller parts. In.Write code which draws patterned rectangles to create a quilt from the 4 squares below. HH HT TH TT These patterned tiles are know as Truchet tiles. In 1704, Father Sebastien Truchet, a French priest and scientist invented this set of tiles and studied the patterns which could be created. You will write a function to draw each of the tiles and then you will make a quilt from a random set of tiles. You will use Python with the ezgraphics library to the tiles. Getting started You can complete your coursework on the CoCalc platform or on your local computer or both. Your solution will be You are provided with some starter code in the file quilt.py which contains a main function and several incomplete functions that you will finish for the coursework. In this coursework you will complete the code according to the instructions in the brief. The instructions in the rest of the brief will take you through writing and testing each patch in turn and then printing the quilt. This is good programming practice: decomposing the functionality and writing and testing new parts of the code in turn. main() The main function, main(), is the starting point for the program. It is complete, you do not need to edit the code in main(). 1. Tile HH def draw_HH(canvas, x, y, width, height): 1. This function draws one HH patch on the canvas. Parameters x, y are the coordinates of the top-left of the patch and width and height are the dimensions of the patch. 2. You must draw a rectangle on the canvas with the origin x,y and the width and height specified Use the canvas method setOutline() with the parameter “black” to achieve this (you need the quotes). 3. Draw the orange triangle, use the method: drawPoly(). The documentation for drawPoly() can be found on the ezgraphics website. 4. There are two constants for the outline colour and fill colour at the top of the program. 5. When you are ready to test the draw_HH() function. Run your program in the terminal: 6. You might need to type ‘python3’ instead of ‘python’. Your output should look like the quilt below. Each patch will have a black outline and the filled triangle has a black outline: Figure 1 - HH 7. The code in main() will call draw_HH() twice, once for the patch in the top-left and once for the patch in the bottom-right. You do not need to edit the code in main(). python solution_quilt.py -HH Version 1. 01/03/2021 2. Tile HT def draw_HT(canvas, x, y, width, height): 1. This function draws one HT patch on the canvas. Parameters x, y are the coordinates of the top-left of the patch and width and height are the dimensions of the patch. The instructions are the same as those for the first patch. 2. When you are ready to test the draw_HT() function. Run your program in the terminal: 3. You might need to type ‘python3’ instead of ‘python’. Your output should look like the quilt below. Each patch will have a black outline and the filled triangle has a black outline: Figure 2 – HT 3. Tile TH def draw_TH(canvas, x, y, width, height): 1. This function draws one TH patch on the canvas. Parameters x, y are the coordinates of the top-left of the patch and width and height are the dimensions of the patch. Use the instructions for the first patch. 2. When you are ready to test the draw_TH() function. Run your program in the terminal: python solution_quilt.py -HT python solution_quilt.py -TH Version 1. 01/03/2021 Figure 3 - TH 4. Tile TT def draw_TT(canvas, x, y, width, height): 1. This function draws one TT patch on the canvas. Parameters x, y are the coordinates of the top-left of the patch and width and height are the dimensions of the patch. Use the instructions for the first patch. 2. When you are ready to test the draw_TT() function. Run your program in the terminal: Figure 4 - TT python quilt.py -TT Version 1. 01/03/2021 5. draw_quilt() – with one block pattern Now you are going to put your functions together to draw a quilt. First we’ll draw a quilt with just one of our blocks. def draw_quilt(canvas, width, height, n): x, y is the coordinate of the top-left corner of the quilt, width and height are the width and height of the quilt. The quilt will be made from n x n patches. 1. You should calculate the height of each patch by dividing the height by n and the width of each patch by dividing the width by n. Use the // operator. 2. You will need a for loop to go over each row and an inner loop to go over each column in the row and then at each position you should draw a patch of one of your designs by calling the appropriate function. 3. Call your program using the -quilt option: 4. Experiment. Change the code to use the different quilt patterns, use n to change the number of tiles. HH pattern, n = 5 TT pattern, n = 10 6. Generate truchet string def generate_truchet(width, depth) 1. In this function you will show that you can use string operations to generate a string containing the symbols for the quilt. The number of tiles in the quilt is width multiplied by depth. 2. Use the random.choice() function from the Python random library to generate either an ‘H’ or a ‘T’ character. 3. This function must return a string with the right number of Truchet symbols. For a 2x2 quilt, a possible string is: TTHTTTHT python quilt.py -quilt 5 Version 1. 01/03/2021 4. draw_quilt() – use Truchet symbols Now add more functionality to draw_quilt(). 1. Call your function generate_truchet() to create a string of symbols. 2. Use the symbols in the string to select the tiles that you draw. You will need to find the right slice in the string for each row and column in the quilt. 3. Depending on the current symbol in the slice, you will need to call the appropriate tile function. 4. To create a 10 x 10 tile quilt, call: 5. Here is an example of the output for a where the The command line options 600 and 400 determine the number of pixels in the width and height of the quilt. Here is some example output: Testing: You are responsible for testing your program carefully. Make sure that you have thought about all the things that can go wrong and test your program to ensure that you know it works correctly in all circumstances. python quilt.py -quilt 10 600 400 Version 1. 01/03/2021

Ready To Get Started?

GET STARTED TODAY