netlist: Fix warning
[geda-gaf.git] / doc / guile-example.py
blob2c46c58841fd2b4ba8e4bee220e612fecb74dc44
1 # Guile example: A Logo-like tortoise drawer
2 # Copyright (C) David Drysdale, Daniel Kraft
3 # Copyright (C) 2013-2020 Roland Lutz
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software Foundation,
17 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # This example shows how to embed Guile as a script interpreter into a
20 # Python application using the xorn.guile module. It defines a set of
21 # functions for controlling a drawing tortoise, makes them available
22 # by calling xorn.guile.define which creates a top-level Scheme
23 # binding for each function, and then runs the example script file
24 # `guile-example.scm' using xorn.guile.load.
26 # In order to run this example, the built Xorn packages need to be in
27 # your PYTHONPATH. This means you have to either
28 # - build Xorn and add the directory `built-packages' inside the build
29 # tree to your PYTHONPATH,
30 # - build and install Xorn and add PREFIX/lib/python2.7/site-packages
31 # to your PYTHONPATH, or
32 # - build and install Xorn to a default prefix (usually /usr/local).
34 import math, subprocess, sys
35 import xorn.guile
37 WIDTH = 10
38 HEIGHT = 10
40 pipe = None
42 x = 0.
43 y = 0.
44 direction = 0.
45 pendown = True
47 def draw_line(x1, y1, x2, y2):
48 pipe.write("plot [0:1] %f + %f * t, %f + %f * t notitle\n" % (
49 x1, x2 - x1, y1, y2 - y1))
50 pipe.flush()
52 def tortoise_reset():
53 global x, y, direction, pendown
54 x = y = 0.
55 direction = 0.
56 pendown = True
58 pipe.write("clear\n")
59 pipe.flush()
61 def tortoise_pendown():
62 global pendown
63 result = pendown
64 pendown = True
65 return result
67 def tortoise_penup():
68 global pendown
69 result = pendown
70 pendown = False
71 return result
73 def tortoise_turn(degrees):
74 global direction
75 direction += math.pi / 180. * degrees
76 return direction * 180. / math.pi
78 def tortoise_move(length):
79 global x, y
80 newX = x + length * math.cos(direction)
81 newY = y + length * math.sin(direction)
83 if pendown:
84 draw_line(x, y, newX, newY)
86 x = newX
87 y = newY
88 return x, y
90 if __name__ == '__main__':
91 p = subprocess.Popen(['gnuplot'], stdin = subprocess.PIPE)
92 pipe = p.stdin
94 pipe.write("set multiplot\n")
95 pipe.write("set parametric\n")
96 pipe.write("set xrange [-%d:%d]\n" % (WIDTH, WIDTH))
97 pipe.write("set yrange [-%d:%d]\n" % (HEIGHT, HEIGHT))
98 pipe.write("set size ratio -1\n")
99 pipe.write("unset xtics\n")
100 pipe.write("unset ytics\n")
101 pipe.flush()
103 xorn.guile.define('tortoise-reset', tortoise_reset)
104 xorn.guile.define('tortoise-penup', tortoise_penup)
105 xorn.guile.define('tortoise-pendown', tortoise_pendown)
106 xorn.guile.define('tortoise-turn', tortoise_turn)
107 xorn.guile.define('tortoise-move', tortoise_move)
109 xorn.guile.load('guile-example.scm')
111 sys.stdin.readline()