Advertisement
Vermiculus

Grid

Jul 23rd, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from Tkinter import *
  2.  
  3. def butts(Bx1, By1, Bx2, By2, x, y, dotSize):
  4.     x-=1;y-=1
  5.     WINDOW_WIDTH = 800
  6.     WINDOW_HEIGHT = 600
  7.     def fxrange(min,max,delta=1):
  8.         r = min
  9.         while r <= max:
  10.             yield r
  11.             r += delta
  12.     def drawGrid(c, x1, y1, x2, y2, x, y):
  13.         """Draws a complete grid (x by y) on Canvas c within the bounding box."""
  14.         # calculate the spacing between each line (vertical and horizontal)
  15.         sv = float((x2 - x1)) / float(x)
  16.         sh = float((y2 - y1)) / float(y)
  17.        
  18.         # Draw the lines
  19.         for i in fxrange(x1, x2, sv):
  20.             c.create_line(i, y1, i, y2)
  21.         for i in fxrange(y1, y2, sh):
  22.             c.create_line(x1, i, x2, i)
  23.     def drawDots(c, x1, y1, x2, y2, x, y, size=dotSize):
  24.         """Draws a complete grid (x by y) of dots on Canvas c within the bounding box."""
  25.         # calculate the spacing between each line (vertical and horizontal)
  26.         sx = float(x2 - x1) / float(x)
  27.         sy = float(y2 - y1) / float(y)
  28.        
  29.         for tx in fxrange(x1, x2, sx):
  30.             for ty in fxrange(y1, y2, sy):
  31.                 # calculate the bounding box of the circle
  32.                 ts = float(size) / 2
  33.                 box_x1 = tx - ts
  34.                 box_y1 = ty - ts
  35.                 box_x2 = tx + ts
  36.                 box_y2 = ty + ts
  37.                
  38.                 # draw the circle
  39.                 c.create_oval(box_x1, box_y1, box_x2, box_y2, fill="black")
  40.     master = Tk()
  41.     w = Canvas(master, width=WINDOW_WIDTH, height=WINDOW_HEIGHT)
  42.     w.pack()
  43.     drawGrid(w, Bx1, By1, Bx2, By2, x, y)
  44.     drawDots(w, Bx1, By1, Bx2, By2, x, y)
  45.     mainloop()
  46.  
  47. butts(10, 10, 600, 600, 50, 50, 7)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement