Example pinboard.py

In this image we define a set of points equally spaced along the x axis, and another set of points equally spaced alobg the y axis. We join each point on the x axis to a corresponding point on the y axis using a straight line.

Code

import sympl.imagefile

length = 500 step = 10

def draw(ctx):

  points1 = [(n, 0) for n in range(0, length+step, step)]
  points2 = [(0, n) for n in range(0, length+step, step)]
  for p1, p2 in zip(points1, reversed(points2)):
      ctx.move_to(*p1)
      ctx.line_to(*p2)
      ctx.set_source_rgb(0, 0.5, 0)
      ctx.set_line_width(1)
      ctx.stroke()

sympl.imagefile.save(length, length, draw, “pinboard.png”) </code> In the draw function, we use a list comprehension to define the points on the y axis (points1) and the x axis (points 2).

In the loop, we join each point in points1 to a point in points2. Notice that we go through the points2 list in reverse order.