Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Wrapping paper, anyone ?

0 views
Skip to first unread message

simon

unread,
Dec 16, 2009, 12:46:26 AM12/16/09
to
#!/usr/bin/env python

from math import *

from random import *

import cairo
from cairo import Context

Black = (0, 0, 0)
White = (1, 1, 1)

def rand():
return random()*2 - 1

def rotate(theta, x, y):
x, y = x*cos(theta)-y*sin(theta), x*sin(theta)+y*cos(theta)
return x, y

def star(ctx, n=5, r0=0.4):

for c in range(2):
ctx.set_source_rgba(*(White, Black)[c])
x, y = 0, 1
ctx.move_to(x, y)
theta = 2*pi / n
for i in range(n):
x1, y1 = rotate(theta/2, x, y)
ctx.line_to(r0*x1, r0*y1)
x, y = rotate(theta/2, x1, y1)
ctx.line_to(x, y)
ctx.close_path()
(ctx.fill, ctx.stroke)[c]()


class ScribeCall(object):
def __init__(self, scribe, name):
self.scribe = scribe
self.name = name

def __call__(self, *args, **kw):
self.args = args
self.kw = kw
self.scribe.calls.append(self)


class Scribe(object):
def __init__(self):
self.calls = []

__getattr__ = ScribeCall
def run(self, ctx):
for call in self.calls:
#print "ctx.%s(%s)" % (call.name, ', '.join(str(x) for x
in call.args))
getattr(ctx, call.name)(*call.args, **call.kw)


def orbit(dx, dy, w, h):
x = -2*w
while x < 2*w:
y = -2*h
while y < 2*h:
yield x, y
y += dy
x += dx


def main_stars(w=800, h=1000):

seed(0)

surface = cairo.PSSurface('stars.ps', w, h)
ctx = Context(surface)

ctx.set_source_rgba(*White)
ctx.rectangle(0, 0, w, h)
ctx.fill()

ctx.set_line_width(1./30)

scribe = Scribe()

for i in range(10):
scribe.save()

scribe.translate(w*random(), h*random())
scribe.scale(20, 20)
scribe.rotate(random() * 2 * pi)

r = exp(random())
scribe.scale(r, r)
star(scribe, 5)
scribe.scale(1./r, 1./r)

scribe.translate(rand(), rand())
scribe.restore()

for x, y in orbit(120, 240, w, h):
ctx.save()
ctx.translate(x, y)
scribe.run(ctx)
ctx.restore()


if __name__=="__main__":
main_stars()

Peter Otten

unread,
Dec 16, 2009, 5:00:24 AM12/16/09
to
simon wrote:

Nice :)

--- stars.py 2009-12-16 10:52:49.553505036 +0100
+++ stars_fixed.py 2009-12-16 10:53:32.545786454 +0100
@@ -48,7 +48,9 @@
def __init__(self):
self.calls = []

- __getattr__ = ScribeCall
+ def __getattr__(self, name):
+ return ScribeCall(self, name)
+


def run(self, ctx):
for call in self.calls:
#print "ctx.%s(%s)" % (call.name, ', '.join(str(x) for x in
call.args))

Peter

r0g

unread,
Dec 16, 2009, 8:36:32 AM12/16/09
to


Nice :) Now all I need is an A0 printer, maybe Santa will bring me one!

Roger.

simon

unread,
Dec 16, 2009, 9:54:30 AM12/16/09
to

Oh.. I'm on py2.5.. does this not work for you ?

simon

unread,
Dec 16, 2009, 9:56:15 AM12/16/09
to

I found I could wrap a CD in A4 paper... for bigger things, i might
stick some A4 paper together.

Wow. Who is getting the A0 gift ?


Peter Otten

unread,
Dec 16, 2009, 10:18:35 AM12/16/09
to
simon wrote:

You mean 2.4? Here's a little demo:

$ cat scribecall.py


class ScribeCall(object):
def __init__(self, scribe, name):

print "init", scribe, name
def __call__(self, *args, **kw):
print "call", args, kw

class Scribe(object):
__getattr__ = ScribeCall

if __name__ == "__main__":
scribe = Scribe()
scribe.yadda(42)

$ python2.4 scribecall.py
init <__main__.Scribe object at 0x7fc87b9a1450> yadda
call (42,) {}

$ python2.5 scribecall.py
Traceback (most recent call last):
File "scribecall.py", line 12, in <module>
scribe.yadda(42)
TypeError: __init__() takes exactly 3 arguments (2 given)

$ python2.5 -V
Python 2.5.4

$ python2.6 scribecall.py
Traceback (most recent call last):
File "scribecall.py", line 12, in <module>
scribe.yadda(42)
TypeError: __init__() takes exactly 3 arguments (2 given)

Peter

r0g

unread,
Dec 16, 2009, 10:27:49 AM12/16/09
to

I was thinking of selling it, this is BSD licensed right! ;D

R.

bartc

unread,
Dec 16, 2009, 11:35:49 AM12/16/09
to

"simon" <pianom...@gmail.com> wrote in message
news:a50b1c21-287b-498d...@k9g2000vbl.googlegroups.com...

> #!/usr/bin/env python
>
> from math import *
>
> from random import *
>
> import cairo
> from cairo import Context

What's cairo?


r0g

unread,
Dec 16, 2009, 12:12:01 PM12/16/09
to


A vector graphics library.

http://en.wikipedia.org/wiki/Cairo_(graphics)

Roger.

simon

unread,
Dec 17, 2009, 1:04:34 AM12/17/09
to

Oh wow, it works on python 2.5.2 !

Simon.

0 new messages