简体   繁体   中英

Visualize a sparse matrix using Python Turtle graphics

I'm stuck on this problem.

def showMatrix(turtle_object, sparse_matrix):

The showMatrix() function will visualize the matrix contents using a grid of dots. Each grid location will correspond to a single matrix location (row, column). The presence of a "dot" indicates a non-zero entry.

First, you need to set the display coordinates to match the matrix extent using the screen.setworldcoordinates() method. In other words, the lower left corner of the display will become coordinate (0,0) and the upper right corner will be (rows-1, columns-1). Changing the screen coordinates in this way simplifies the mapping of matrix indices to screen coordinates by matching the "grid" and matrix coordinates.

Using the turtle .goto and .dot methods, plot a red dot for each matrix.

This is the work I've done so far:

def matrix(n, init):
    matrix = []
    for i in range(n):
        row = []
        for j in range(n):
            row.append(init)
        matrix.append(row)
    return matrix



def sparse_matrix(matrix,n,value):
    import random
    ctr = 0
    while ctr < n:
        row = random.randint(0,order(m)-1)
        col = random.randint(0,order(m)-1)
        if matrix[row][col] != value:
            matrix[row][col] = value
            ctr += 1
    return matrix

def showMatrix(turtle_object, sparse_matrix):
    for i in len(m):
        for j in len(m):
            if sparse_matrix[i][j] != 0:
                sparse_matrix[i][j] = turtle_object
    return sparse_matrix

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM