What does the problem mean by (rows-1, columns-1)?
This is tied up with your mysterious m
variable and the order()
function you left undefined. Let’s proceed anyway. We can see from the matrix()
function we’re dealing with a square matrix but let’s not even assume that. Within the sparse_matrix()
function, we can figure out rows and columns by doing:
rows = len(sparse_matrix)
columns = len(sparse_matrix[0])
Along with checking that rows
isn’t zero.
How do I show the sparse matrix on turtle?
Your sparse_matrix()
function isn’t using turtle_object
appropriately — we don’t want to store it, we want to ask it to draw things. And this function probably shouldn’t return anything. I’m guessing it should look something like:
def showMatrix(turtle_object, sparse_matrix):
rows = len(sparse_matrix)
if rows == 0:
return
columns = len(sparse_matrix[0])
turtle_object.penup()
for r in range(rows):
for c in range(columns):
if sparse_matrix[r][c] != 0:
turtle_object.goto(c, r)
turtle_object.dot(dot_size, "red")
Where dot_size
is 1 for now. Wrapping this in some turtle code:
from turtle import Screen, Turtle
# ...
m = 6
screen = Screen()
dot_size = 1
yertle = Turtle(visible=False)
mat = matrix(order(m), 0)
sparse_matrix(mat, order(m / 2), 1)
showMatrix(yertle, mat)
screen.mainloop()
We get an unsatisfactory graph:
As everything is too small and needs to be scaled up.
I’m not sure how to use screen.setworldcoordinates()
Rather than add a scaling factor directly to our graphing code, we can use turtle’s own setworldcoordinates()
to bend the window to our graph limits:
screen.setworldcoordinates(0, 0, order(m), order(m))
dot_size = screen.window_width() / order(m)
This gives us something a little more visually satisfying:
I hope this rough sketch gets you moving in the right direction.