PASTEBIN
| #1 paste tool since 2002
create new paste
tools
api
archive
real-time
faq
PASTEBIN
create new paste
trending pastes
sign up
login
my alerts
my settings
my profile
Got an iPhone or iPad? We have a brand new Pastebin App for both devices, and it's totally free!
Click here to download the new Pastebin App for iOS
.
Public Pastes
Untitled
2 sec ago
Untitled
5 sec ago
Untitled
5 sec ago
Untitled
C# | 5 sec ago
Untitled
16 sec ago
Untitled
10 sec ago
Untitled
10 sec ago
Quest Script
11 sec ago
New Paste
# this code is an improved version of the l-system # sample code originally distributed with pycairo. # originally Copyright 2003 Jesse Andrews (jdandr2 at uky.edu) # this version Copyright 2009 Seb Potter (iamseb at iamseb.com) # licensed under GPL import logging import cairo # setup logging to just print to stdout. # python's logging module is significantly # better than using print for debugging LOG_FILENAME = '/dev/stdout' logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG) class Lindenmayer: """ A class to represent L-Systems and render them using cairo """ def __init__( self ): self.width = self.height = 500 # use an 800 pixel square image self.prod = {'[':'[','F':'F',']':']','+':'+','-':'-'} # the identity products self.start_pos = (self.width*0.5, self.height) # we translate to the middle bottom of the image to start self.start_angle = 180 # we rotate through 180 degrees to draw upwards - cairo's origin is top-left self.theta = 90 # the rotation angle in degrees self.stack = [] # this will be the stack for storing translation and orientation tuples self.str = 'f' # the starting string, or 'axiom' self.line_length = 5 # how far we move forward on each step self.line_width = 2 # just controls the rendered width of the line self.logger = logging.getLogger("flower.draw.lindenmayer") # use logging to print debugging self.logger.setLevel(logging.DEBUG) def addProd(self, let, prod): """ Add a production to the ordered list of productions to apply to the current string. """ self.prod[let]=prod def iterate(self, iterations=1): """ Iterate over the list of productions and apply them to the string, in a loop. The end result is the final transformed string. """ for i in xrange(iterations): self.str = ''.join([ self.prod[l] for l in self.str]) self.logger.info("String is: %s" % self.str) def line(self, ctx, len): ctx.rel_line_to( 0, len ) def rotate(self, ctx, deg): ctx.rotate( 2*3.141592653589793*deg/360.0 ) def draw(self, colour): """Render the string representation of the L-System""" self.logger.info(colour) # create a cairo drawing context from the provided surface. surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, self.width, self.height) ctx = cairo.Context(surface) # first we'll create a nice white rectangle as our background ctx.rectangle(0, 0, self.width, self.height) ctx.set_source_rgb(1, 1, 1) ctx.fill() # set the line colour, width, and make sure that cairo knows how close # lines should be to join them ctx.set_source_rgb(*colour) ctx.set_line_width(self.line_width) ctx.set_tolerance(0.1) ctx.set_line_join(cairo.LINE_JOIN_BEVEL) # start drawing a path, move to our start point, and rotate to # the starting angle ctx.new_path() ctx.move_to(*self.start_pos) self.logger.debug("Initial position: %s, %s" % ctx.get_current_point()) self.rotate(ctx, self.start_angle) # this is the very simple way we iterate over the final string # and perform a drawing operation for each symbol in the string for c in self.str: if c == 'F': # move forward self.logger.debug("f: draw a line of %s" % self.line_length) self.line(ctx, self.line_length) if c == '+': # rotate clockwise self.logger.debug("+: rotate %s" % self.theta) self.rotate(ctx, self.theta) if c == '-': # rotate anti-clockwise self.logger.debug("-: rotate -%s" % self.theta) self.rotate(ctx, -self.theta) if c == '[': # push the transform and orientation onto the stack m = ctx.get_matrix() p = ctx.get_current_point() self.logger.debug("[: push the matrix %s onto the stack" % m) self.stack.append((p, m)) if c == ']': # restore the transform and orientation from the stack p, m = self.stack.pop() self.logger.debug("]: pop the matrix %s off the stack" % m) ctx.set_matrix(m) ctx.move_to(*p) # now draw the path created as a stroke on the context ctx.stroke() # write this to a png surface.write_to_png("test.png") self.logger.info("Wrote test.png") def main(): colour = (0, 0.3, 0) # setup the initial parameters for this l-system lin = Lindenmayer() lin.start_angle = 205 lin.start_pos = (lin.width*0.25, lin.height) lin.str = 'X' lin.addProd('X', 'F[+X]F[-X]+X') lin.addProd('F', 'FF') lin.theta = 20 # generate the final string lin.iterate(iterations=5) # render the string using pycairo lin.draw(colour) if __name__ == '__main__': main()
Optional Paste Settings
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
HTML 5
Java
JavaScript
Lua
None
Perl
PHP
Python
Rails
-------------
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
ActionScript
ActionScript 3
Ada
ALGOL 68
Apache Log
AppleScript
APT Sources
ASM (NASM)
ASP
autoconf
Autohotkey
AutoIt
Avisynth
Awk
BASCOM AVR
Bash
Basic4GL
BibTeX
Blitz Basic
BNF
BOO
BrainFuck
C
C for Macs
C Intermediate Language
C#
C++
C++ (with QT extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
ChaiScript
Clojure
Clone C
Clone C++
CMake
COBOL
CoffeeScript
ColdFusion
CSS
Cuesheet
D
DCS
Delphi
Delphi Prism (Oxygene)
Diff
DIV
DOS
DOT
E
ECMAScript
Eiffel
Email
EPC
Erlang
F#
Falcon
FO Language
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
Game Maker
GDB
Genero
Genie
GetText
Go
Groovy
GwBasic
Haskell
HicEst
HQ9 Plus
HTML
HTML 5
Icon
IDL
INI file
Inno Script
INTERCAL
IO
J
Java
Java 5
JavaScript
jQuery
KiXtart
Latex
Liberty BASIC
Linden Scripting
Lisp
LLVM
Loco Basic
Logtalk
LOL Code
Lotus Formulas
Lotus Script
LScript
Lua
M68000 Assembler
MagikSF
Make
MapBasic
MatLab
mIRC
MIX Assembler
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MPASM
MXML
MySQL
newLISP
None
NullSoft Installer
Oberon 2
Objeck Programming Langua
Objective C
OCalm Brief
OCaml
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
Pascal
PAWN
PCRE
Per
Perl
Perl 6
PHP
PHP Brief
Pic 16
Pike
Pixel Bender
PL/SQL
PostgreSQL
POV-Ray
Power Shell
PowerBuilder
ProFTPd
Progress
Prolog
Properties
ProvideX
PureBasic
PyCon
Python
q/kdb+
QBasic
R
Rails
REBOL
REG
Robots
RPM Spec
Ruby
Ruby Gnuplot
SAS
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
SQL
SystemVerilog
T-SQL
TCL
Tera Term
thinBasic
TypoScript
Unicon
UnrealScript
Vala
VB.NET
VeriLog
VHDL
VIM
Visual Pro Log
VisualBasic
VisualFoxPro
WhiteSpace
WHOIS
Winbatch
XBasic
XML
Xorg Config
XPP
YAML
Z80 Assembler
ZXBasic
Paste Expiration:
Never
10 Minutes
1 Hour
1 Day
1 Month
Paste Exposure:
Public
Unlisted
Private (members only)
Paste Name / Title:
Hello
Guest
Sign Up
or
Login
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login