Graph Paper

Back to index

Graph paper can be very useful and graph paper can come with lines which are spaced out at different distances. Check out the grid created by the following sample program by getting it up and running.
#!/usr/bin/python import turtle t=turtle.Pen() t.speed(3) t.screen.bgcolor("green") t.pencolor("black") t.pensize=1 t.shape("turtle") t.seth(0) x=-60 for y in range(-60,61,4): if y%20==0: t.pencolor("red") else: t.pencolor("black") t.penup() t.setpos(x,y) t.pendown() t.forward(120) t.seth(90) y=-60 for x in range(-60,61,4): if x%20==0: t.pencolor("red") else: t.pencolor("black") t.penup() t.setpos(x,y) t.pendown() t.forward(120) t.ht() turtle.mainloop()
Although this program could have been implemented using a single for-loop, it's a little easier to understand using two for-loops. Be that as it may, you should make sure that you understand how the if-else construct is used to decide when lines should be drawn in red or black. In case you don't already know, the % operator is known as the modulus operator. It basically performs division, but instead of returning the quotient, it just returns the remainder.
ASSIGNMENT:

You will create a larger and more complex grid, conforming to the following specification list:

  1. Horizontal Lines: 32
  2. Vertical Lines: 24
  3. Line Spacing: 12 units
  4. Red Lines: Every fourth horizontal line
  5. Blue Lines: Every sixth vertical line
  6. Other Lines: green
  7. Frame: Draw a black frame around the grid using a thicker line
  8. Background: White