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
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
))
53 global x
, y
, direction
, pendown
61 def tortoise_pendown():
73 def tortoise_turn(degrees
):
75 direction
+= math
.pi
/ 180. * degrees
76 return direction
* 180. / math
.pi
78 def tortoise_move(length
):
80 newX
= x
+ length
* math
.cos(direction
)
81 newY
= y
+ length
* math
.sin(direction
)
84 draw_line(x
, y
, newX
, newY
)
90 if __name__
== '__main__':
91 p
= subprocess
.Popen(['gnuplot'], stdin
= subprocess
.PIPE
)
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")
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')