[reportlab-users] Sunday pleasures: 12 queens animated

Dinu Gherman reportlab-users@reportlab.com
Mon, 4 Nov 2002 11:05:09 +0100


--Apple-Mail-9-115051332
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed

Andy Robinson:

> Yes it does, a very nice effect!  So, I guess Acrobat Reader gets it 
> right
> at least on Windows but not all viewers are this good.  Are you using 
> XPdf,
> or Acrobat Reader on OSX?

AR(gh) 5.1 on OSX... I've attached another script generating a
10 fps PDF file. To run it you must first copy Python "almost"
standard library Queens generator file Lib/test/test_generators.py
to the same directory and for Python < 2.3 add the following line
at the top of it: from __future__ import generators.

Unfortunately, it seems this test stuff is not directly available
for importing unless you make some exotically configured installa-
tion or do some manual tricks... I don't understand why this lives
in the test area only and not in tools or so... Before I was using
a generator-less algorithm, which seems to be three times slower.

Also, the now attached script doesn't filter any solutions.

Dinu

--Apple-Mail-9-115051332
Content-Disposition: attachment;
	filename=8queens92PDF.py
Content-Transfer-Encoding: 7bit
Content-Type: application/text;
	x-mac-creator=522A6368;
	x-unix-mode=0644;
	x-mac-type=54455854;
	name="8queens92PDF.py"

#!/usr/bin/env python

from test_generators import Queens
## from addons import *


def create_solutions(n):
    sols = []
    q = Queens(n)
    for sol in q.solve():
        sol = sol[:]
        sols.append(sol)
    return sols
    

def make_PDF():
    "Create a PDF file with all solutions to the N-queens problem."
    
    from reportlab.lib import colors
    from reportlab.pdfgen.canvas import Canvas

    a = 100
    n = 8

    # setup canvas    
    c = Canvas('8queens92.pdf')
    c.setPageSize((n*a, n*a))
    c.showFullScreen0()
    c.setPageDuration(0.1)

    # create list of solutions    
    sols = create_solutions(n)
    ## sols = remove_mirror_solutions(sols)
    print "#solutions:", len(sols)

    # add one page per solution to the canvas    
    na = n*a
    for sol in sols:
        # draw grid
        c.setFillColor(colors.gray)
        for i in range(0, na, a):
            c.line(0, i, na, i)
            c.line(i, 0, i, na)
        # draw queens
        c.setFillColor(colors.red)
        for i in range(len(sol)):
            c.rect(i*a, sol[i]*a, a, a, fill=1)
        c.showPage()
    
    c.save()


if __name__ == '__main__':
    make_PDF()

--Apple-Mail-9-115051332
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed


--
Dinu C. Gherman
......................................................................
"Experience is one thing you can't get for nothing." (Oscar Wilde)

--Apple-Mail-9-115051332--