2. • Q.Define the length of the sides of the cells. All other dimensions for the drawing canvas are calculated relative to this value.
• cell_side = 60
• Ans=
• The length of the sides of the cells is 60 units. This means that each cell in a grid or matrix would have a side length of 60 units.
• Any other dimensions or measurements related to the drawing canvas, such as the width and height of the canvas or the spacing between cells,
• would be calculated relative to this value of 60.
•
• For example, if the grid is 10 cells wide, the width of the canvas would be 10 times the length of one cell side, or 600 units.
• Q.Define the overall size of our "flat top" hexagonal grid
• grid_width = 9 # hexagonal cells
• grid_height = 9 # hexagonal cells
• Ans=
• The overall size of our "flat top" hexagonal grid is 9 hexagonal cells wide by 9 hexagonal cells high.
• This means that the grid consists of a total of 81 hexagonal cells arranged in a 9 by 9 pattern.
• The width and height of the grid are defined by the variables "grid_width" and "grid_height", respectively, with each variable set to a value of 9.
• The specific units of measurement for the grid are not specified in this definition,
• so it is assumed that they are the same as the units used to define the length of the sides of the cells
• (i.e., the units used to define "cell_side" in the previous question).
3. • #-----Module Description - Drawing Canvas Configuration--------------#
• #
• # This module contains functions needed for Assignment 1 in QUT's
• # teaching unit IFB104 "Building IT Systems". You should put
• # a copy of this file in the same folder as your solution to the
• # assignment. The necessary elements will then be imported
• # into your program automatically.
• #
• # NB: Do NOT make any changes to this module and do NOT submit a
• # copy of this file with your solution. Changes made to this
• # module will have no effect when your assignment is graded because
• # the markers will use their own copy of the file. If your program
• # relies on changes made to this file it will fail to work when
• # assessed.
• #
• #--------------------------------------------------------------------#
4. • #-----Preamble-------------------------------------------------------#
• #
• # This section defines constants and imports functions used for
• # creating the drawing canvas.
• #
• # Import standard Python functions needed to support this module.
• from turtle import *
• from math import *
• # Define the length of the sides of the cells. All other dimensions
• # for the drawing canvas are calculated relative to this value.
• cell_side = 60 # pixels
• # Define the overall size of our "flat top" hexagonal grid
• grid_width = 9 # hexagonal cells
• grid_height = 9 # hexagonal cells
5. • To ensure that the grid has a cell at the centre, the arithmetic
• # below supports only certain grid sizes
• assert [grid_width, grid_height] in
• [[5, 5], [9, 5], [13, 5],
• [7, 7], [11, 7], [15, 7],
• [5, 9], [9, 9], [13, 9],
• [7, 11], [11, 11], [15, 11],
• [5, 13], [9, 13], [13, 13]], 'Invalid grid dimensions‘
ANS:
• This code asserts that the grid width and height must have specific values in order to ensure that the grid has a cell at the
center. These specific values are listed in the nested list, which contains all valid combinations of grid width and height that
will result in a grid with a cell at the center. If the grid width and height are not in this list, the code will raise an assertion
error with the message "Invalid grid dimensions".
• For example, a 6x6 grid is not in the list, so the assertion would fail and raise an error. This is because a 6x6 grid does not
have a center cell, since the center would fall between four cells.
• Overall, this code is meant to ensure that the grid being used has a center cell, which may be important for certain
calculations or applications.
6. 2ND ANS
This code is a Python assertion statement that checks if the dimensions of a grid are valid,
based on a predefined set of allowed sizes. The assertion statement is composed of two parts:
1.The first part is a boolean expression that checks if the dimensions of the grid, represented by the
variables
2. grid_width and grid_height, are present in a list of allowed sizes. This list contains 15 tuples,
3.each representing a pair of integers that correspond to a valid grid size.
2.The second part is a string that is passed as a second argument to the assertion statement.
3. This string is an error message that will be raised if the boolean expression evaluates to False. In this
case, the error message is "Invalid grid dimensions".
If the dimensions of the grid are not in the list of allowed sizes, the assertion statement will raise an
AssertionError with the error message "Invalid grid dimensions“
. If the dimensions are valid, the assertion statement will do nothing and the program will continue to
execute.
Overall, this code is used to enforce a restriction on the grid dimensions to ensure that the grid has a
center cell.
The allowed sizes are carefully chosen to ensure that the center cell is present in the grid.
7. Q.
• Derive constant values used in the main program that sets up
• # the drawing canvas.
• cell_width = cell_side * 2 # cell width for a grid with "flat top"
• # orientation
• cell_height = round(2 * (cell_side * sin(radians(60)))) # cell height and
• # distance from neighbours
• horiz_spacing = round(3 / 4 * cell_width) # distance between grid cells
• # horizontally
• vert_spacing = cell_height / 2 # distance between grid cells
• # vertically
• x_margins = horiz_spacing * 5.5 # the total size of the margins left
• # and right of the grid
• y_margins = vert_spacing * 4 # the total size of the margins below
• # and above the grid
• window_height = int(((grid_height + 1) * vert_spacing) + y_margins) # the drawing
• # canvas' height
• window_width = int((grid_width * horiz_spacing) + x_margins) # the drawing
• # canvas' width
• coord_font = ('Arial', cell_side // 3, 'normal') # text font for labels
• # on grid axes
• label_font = ('Arial', cell_side // 2, 'normal') # text font for
• # instructions
8. ANS:i)
The constant values used in the main program are derived based on the value of cell_side,
which is assumed to be defined earlier in the program. Here are the steps to derive each
constant:
•cell_width is calculated by multiplying cell_side by 2,
•since each cell in the grid has a width equal to twice the length of its side. This assumes a "flat
top" orientation for the cells.
•cell_height is calculated by multiplying cell_side by the sine of 60 degrees (converted to
radians), and then multiplying by 2.
• This formula calculates the height of each cell and the distance between each cell and its
neighbors in a hexagonal grid with "flat top" orientation.
•horiz_spacing is calculated by multiplying 3/4 of cell_width by a rounding of the value to the
nearest integer.
• This formula calculates the horizontal distance between cells in a hexagonal grid with "flat top"
orientation.
9. ii)
•vert_spacing is calculated by dividing cell_height by 2.
•This formula calculates the vertical distance between cells in a hexagonal grid with "flat top" orientation.
•x_margins is calculated by multiplying 5.5 times horiz_spacing.
•This formula calculates the total size of the margins to the left and right of the grid.
•y_margins is calculated by multiplying 4 times vert_spacing.
•This formula calculates the total size of the margins above and below the grid.
•window_height is calculated by adding 1 to grid_height,
• multiplying the result by vert_spacing, adding y_margins, and then rounding the result to the nearest integer.
•This formula calculates the total height of the drawing canvas, which is the height of the grid plus the margins above
and below it.
•window_width is calculated by multiplying grid_width by
•horiz_spacing, adding x_margins, and then rounding the result to the nearest integer
•. This formula calculates the total width of the drawing canvas, which is the width of the grid plus the margins to the
left and right of it.
•coord_font and label_font are both tuples that contain font specifications for the labels on the grid axes and the
instructions, respectively.
•The first element of each tuple specifies the font family, the second element specifies the font size (derived by integer
division of cell_side by 3 or 2),
• and the third element specifies the font weight (set to "normal").
10. Q.
• The functions in this section are called by the assignment template
• # to manage the drawing canvas used by your program.
• #
• # Set up the canvas and draw the background for the overall image
• #
• def create_drawing_canvas(canvas_title = "Put your solution's title here",
• bg_colour = 'light grey',
• line_colour = 'slate grey',
• draw_grid = True,
• write_instructions = True):
•
• # Set up the drawing canvas with enough space for the grid and
• # margins
• setup(window_width, window_height)
• bgcolor(bg_colour)
• title(canvas_title)
11. ANS:i)
This function create_drawing_canvas sets up the drawing canvas with the specified parameters.
•canvas_title: A string representing the title of the canvas.
•bg_colour: A string representing the background color of the canvas.
•line_colour: A string representing the color of the grid lines.
•draw_grid: A boolean indicating whether to draw the grid lines or not.
•write_instructions: A boolean indicating whether to write instructions on the canvas or not.
12. ii)
The function sets up the drawing canvas with the specified width and height
using the setup function from the turtle module.
It also sets the background color of the canvas using the bgcolor function
from the turtle module and sets the title of the canvas using
the title function from the turtle module.
If draw_grid is True, the function draws the grid lines by calling the
draw_grid_lines function,
which is defined later in the code. If write_instructions is True, the function
writes instructions on the canvas by calling
the write_instructions_on_canvas function, which is also defined later in
the code.
Finally, the function returns the turtle object used for drawing on the canvas.
13. Q.
• # Draw the canvas as quickly as possible
• tracer(False)
14. ANS:
The tracer(False) function is a method of the turtle module that turns off
the animation of the turtle drawing.
By default, the turtle animation is turned on, which can slow down the
drawing process.
Turning off the animation with tracer(False) allows the drawing to
happen more quickly and efficiently.
Once the drawing is complete, the animation can be turned back on with
tracer(True) if desired.
15. Q.
• # Get ready to draw the grid
• penup()
• color(line_colour)
• width(2)
16. ANS:
The penup() function is a method of the turtle module that lifts the
pen off the drawing canvas.
This allows the turtle to move without drawing a line.
The color(line_colour) function sets the color of the turtle's pen to
the line_colour specified in the function parameters.
This color will be used for drawing the grid lines.
The width(2) function sets the width of the turtle's pen to 2 pixels.
This determines the thickness of the lines that the turtle will draw
when the pen is down.
17. Q.
• # Determine the left-bottom coords of the grid (defined
• # as the left-most point of the bottom-left cell)
• left_edge = -(((grid_width // 2) * horiz_spacing) + (0.5 *
cell_width))
• bottom_edge = -((grid_height - 1) // 2) * vert_spacing
• goto(left_edge, bottom_edge)
18. ANS:
The left_edge variable is calculated as the x-coordinate of the left-most point of
the bottom-left cell of the grid.
It is determined by taking the horizontal spacing between cells
(horiz_spacing) multiplied by half the width of
the grid (grid_width // 2) and subtracting half the width of a cell (0.5 *
cell_width) to get the left-most x-coordinate.
The bottom_edge variable is calculated as the y-coordinate of the bottom-left
cell of the grid.
It is determined by taking the vertical spacing between cells (vert_spacing)
multiplied by half
the height of the grid minus one ((grid_height - 1) // 2) to get the y-coordinate
of the bottom-most point.
The goto(left_edge, bottom_edge) function moves the turtle to the left-most
point of the bottom-left cell of the grid.
19. Q.
• # Optionally draw the grid
• if draw_grid:
• # Draw the cells row by row
• for rows in range(ceil(grid_height / 2)):
20. ANS:
The if draw_grid: statement checks whether the draw_grid
parameter was set to
True when the create_drawing_canvas function was called.
If it was, the grid will be drawn. If not, the for loop that draws the
grid will be skipped.
The for loop iterates over the rows of the grid, from 0 to
ceil(grid_height / 2) - 1. ceil(grid_height / 2)
gives the number of full rows in the grid (i.e., rows with 2 cells),
and
range generates a sequence of integers from 0 to that number
minus 1.
Each iteration of the for loop draws one full row of cells, from left
to right.
21. Q.
• # Draw upper half of row
• goto(left_edge, bottom_edge + (rows * cell_height))
• pendown()
• setheading(0) # face east
• for angle in ([60, -60, -60, 60] * ceil(grid_width / 2))[:-1]:
• left(angle)
• forward(cell_side)
• penup()
22. ANS:i)
The code above draws the upper half of a row of cells.
The goto command moves the turtle to the bottom-left corner of the first cell in
the current row.
The rows variable indicates which row is currently being drawn, and is used to
determine the vertical position of the cell.
The pendown command lowers the pen to start drawing the cell.
The setheading(0) command points the turtle to the east, in preparation for
drawing the first side of the hexagon.
The for loop draws the six sides of the hexagon for each cell in the current
row.
It does this by iterating over a list containing four angles: 60 degrees (for the
east side), -60 degrees (for the northeast side),
-60 degrees (for the northwest side), and 60 degrees (for the west side). The
ceil(grid_width /
23. ii)
multiplier determines how many times this list should be repeated
to draw all the cells in the row.
The [:-1] slice at the end of the list removes the final angle (which
is 60 degrees) to avoid drawing an extra line at the end of the row.
Finally, the penup command lifts the pen to prepare for moving to
the next cell in the row.
24. Q.
• # Draw lower half of row
• goto(left_edge, bottom_edge + (rows * cell_height))
• pendown()
• setheading(0) # face east
• for angle in ([-60, 60, 60, -60] * ceil(grid_width / 2))[:-1]:
• left(angle)
• forward(cell_side)
• penup()
25. ANS:i)
The create_drawing_canvas function is a part of a program that sets up a
drawing canvas to be used for the rest of the program.
It takes in several parameters such as the title of the canvas, the background
color, and the color of the lines to be drawn.
It also has two optional parameters that control whether or not to draw the grid
and write instructions.
The tracer(False) function is used to turn off animation and speed up the
drawing process.
The penup() function lifts the pen off the canvas, allowing the turtle to move
without drawing a line.
The color() function is used to set the color of the pen.
The width() function sets the width of the lines to be drawn.
26. ii)
The goto() function is used to move the turtle to a specific location on the
canvas.
The ceil() function is used to round up a number to the nearest integer.
The pendown() function lowers the pen onto the canvas, allowing the turtle
to draw a line.
The setheading() function sets the direction the turtle is facing.
The left() function rotates the turtle to the left by a specified angle.
The forward() function moves the turtle forward by a specified distance.
The penup() function is used again to lift the pen off the canvas after a line
has been drawn.
28. ANS:
This code section labels the x-axis of the grid by placing letters
from A to Z to indicate each column of the grid.
It first sets the penup() state to lift the pen off the drawing surface,
then calculates a y_offset value to determine how far above
the grid to place the labels. The goto() function is then called to
move to the appropriate position for each label, and the write()
function is used to display the letter at the current position with the
given alignment and font. The chr()
function is used to convert the integer column index to its
corresponding uppercase letter.
30. ANS:i)
The create_drawing_canvasfunction is used to set up the drawing canvas with the specified parameters.
The parameters include:
•canvas_title: the title of the canvas
•bg_colour: the background color of the canvas
•line_colour: the color of the grid lines
•draw_grid: a boolean flag indicating whether or not to draw the grid
•write_instructions: a boolean flag indicating whether or not to write the instructions
The function uses the setupfunction to set the size of the canvas to window_width
and window_height, which are constant values determined based on the dimensions of the grid and the size of the cells
. It sets the background color of the canvas to bg_colourusing the bgcolorfunction and sets the title of the canvas to
canvas_titleusing the titlefunction.
31. ii)
The function then sets the tracer to False to draw the canvas as quickly as
possible.
It sets the pen color to line_colour and the pen width to 2 using the color
and width functions, respectively.
It then determines the left-bottom coordinates of the grid, defined as the left-
most point of the bottom-left cell,
using the grid dimensions and the horiz_spacing, vert_spacing,
and cell_width and cell_height constant values.
If draw_grid is True, the function draws the cells row by row using nested
loops.
It first draws the upper half of each cell in the row, then the lower half of each
cell in the row. It labels the x and y axes using
the coord_font font and the write function.
32. Q.
• # Mark the four "special" cells (assuming a grid
• # of size at least 9 x 9)
• home()
• dot(cell_side // 4) # middle
• home()
• forward(3 * horiz_spacing)
• left(90)
• forward(vert_spacing)
• dot(cell_side // 4) # right
• home()
• left(180)
• forward(horiz_spacing)
• right(90)
• forward(3 * vert_spacing)
• dot(cell_side // 4) # left, top
• home()
• left(180)
• forward(4 * horiz_spacing)
• left(90)
• forward(2 * vert_spacing)
• dot(cell_side // 4) # left, bottom
33. ANS:
The dot() function in this code draws a dot at the current
turtle position.
The size of the dot is set to be cell_side // 4, which means
that it will be a quarter of the size of a cell.
The turtle moves to different positions using the home(),
forward(), left(), and right()
functions to position the dots in the four "special" cells of
the grid. These cells are assumed to exist in a grid of size
at least 9x9.
34. Q.
• # Optionally write instructions for the programmer
• if write_instructions:
• # Write to the left of the grid
• goto(-((grid_width / 1.8) * horiz_spacing), -(vert_spacing // 0.7))
• write('Replacenthis withnhalf ofnyournsymbol’snvariants',
• align = 'right', font = label_font)
• # Write to the right of the grid
• goto((grid_width / 1.7) * horiz_spacing, -(vert_spacing // 0.7))
• write('Replacenthis withnthe restnof yournsymbol’snvariants',
• align = 'left', font = label_font)
35. ANS:i)
The create_drawing_canvas function is a helper function that sets up the
drawing canvas for the program.
The function takes several parameters such as canvas_title, bg_colour,
line_colour, draw_grid,
and write_instructions which can be used to customize the canvas.
The function first sets up the drawing canvas with the given canvas_title and
bg_colour.
It then draws a grid on the canvas using the given line_colour and draw_grid
parameters.
The grid is drawn by iterating over each cell in the grid and drawing the cell
boundaries using turtle graphics.
36. ii)
The function also marks four special cells in the grid assuming that
the grid is at least 9x9 in size.
Finally, if the write_instructions parameter is set to True,
the function writes some instructions for the programmer on how to
use the canvas.
These instructions are written to the left and right of the grid using
turtle graphics.
Overall, the create_drawing_canvas function provides a convenient
way to set up the drawing canvas and
customize it according to the program's needs.
37. Q.
• # Write above the grid
• goto(0, 2 * vert_spacing * (grid_height / 3.2))
• write('Replace this with your final message',
• align = 'center', font = label_font)
38. ANS:i)
It seems like the create_drawing_canvas function is a helper function for
drawing a canvas with a grid and some special cells.
The function takes some optional arguments to customize the appearance of
the canvas such as the canvas title, background color, line color, and
whether to draw a grid or not.
The function first sets up the canvas by calling the setup function with the
window width and height. Then, it sets the background color and title.
The tracer function is called to speed up the drawing process by turning off
animation.
39. ii)
Next, the function sets up the pen color, width, and moves the pen
to the starting position for the grid.
The grid is drawn row by row, and the function draws the upper and
lower halves of each cell using a set of angles to draw the sides of
the hexagon.
After the grid is drawn, the function labels the x and y axes using
the write function.
It also marks four special cells with dots. Finally, if the
write_instructions flag is set,
the function writes some instructions for the programmer above and
to the left and right of the grid, and a final message is written above
the grid.
41. ANS:i)
The create_drawing_canvas function sets up the drawing canvas with
specified parameters such as the canvas title,
background color, line color, and whether to draw a grid or not. It also defines
the size of each cell in the grid and the size of the canvas.
The function starts by setting up the canvas using the setup function with the
specified window width and height,
and setting the background color using the bgcolor function. It also sets the
canvas title using the title function.
Next, it sets the turtle pen's color, width, and position to prepare for drawing
the grid.
The left edge and bottom edge of the grid are then calculated based on the
specified grid width, grid height, cell width, and cell height.
42. ii)
The function then optionally draws the grid by iterating over the
rows and drawing each row of cells.
The cells are drawn by alternating between two halves of the cell
shape.
The x and y axis labels are then drawn using the write function.
Finally, the function optionally marks four special cells on the grid
and writes instructions for the programmer and a final message
above the grid.
The pen settings are then reset to black, width 1, and home
position, and the drawing canvas is displayed with the
tracer(True) function.
43. O.
• # End the program and release the drawing canvas back to the
• # host operating system
• #
• def release_drawing_canvas(signature,
• text_colour = 'slate grey'):
•
• # Ensure any student drawing still in progress is displayed
• # completely
• tracer(True)
44. ANS:
The release_drawing_canvas() function ends the turtle graphics drawing
session and releases
the canvas back to the host operating system. It takes in two arguments,
signature and text_colour,
with signature being a string representing the name of the student or author of
the program, and text_colour being a string representing
the color of the signature text.
The function starts by calling the tracer() function with an argument of True to
ensure that any student drawing still in progress is displayed completely.
It then sets the turtle's pen color to text_colour and writes the signature at the
bottom of the canvas using the write() function.
Finally, it calls the done() function to end the turtle graphics session and
release the canvas back to the operating system.
45. Q.
• # Sign the canvas with the student's name
• signature_font = ('Comic Sans MS', cell_side // 3, 'bold')
• color(text_colour)
• penup()
• goto((horiz_spacing * grid_width) / 2,
• -(((grid_height - 1) // 2) + 2.4) * vert_spacing)
• write('Visualisation by ' + signature,
• align = 'right', font = signature_font)
•
• # Hide the cursor and release the window to the host
• # operating system
• hideturtle()
• done()
46. ANS:
This code defines a function called release_drawing_canvas that is used to release
the drawing canvas back to the host operating system, after the student has finished their
drawing.
The function takes two arguments: signature, which is a string containing the name of the
student who created the drawing,
and text_colour, which is an optional argument that specifies the color of the text used to
display the student's name.
The function first ensures that any student drawing still in progress is displayed completely by
setting
the tracer to True. It then signs the canvas with the student's name, using a custom font, and
hides the cursor.
Finally, it releases the window to the host operating system using the done function.
Overall, this function is used to clean up and finalize the drawing created by the student, and
prepare the drawing canvas for further use.