summaryrefslogtreecommitdiff
path: root/posts/2018-09-01-guile-picture-language.md
blob: 3fcac5156e5583686cdd8cbe2b10b4749a9c1b7a (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
title: A simple picture language for GNU Guile
date: 2018-09-01 23:00
tags: planet-fsfe-en, free software, guile
---

One thing that I really love about Racket is its [picture
language](https://docs.racket-lang.org/pict/), which allows you to
play with geometric shapes in an interactive session in Dr Racket.
The shapes are displayed right there in the REPL, just like numbers or
strings.  Instead of writing a programme that prints "hello world" or
that computes the Fibonacci numbers, one could write a programme that
composes differently rotated, coloured shapes and prints those
instead.

I use [GNU Guile](https://gnu.org/software/guile) for my own projects,
and sadly we don't have an equivalent of Racket's picture language or
the Dr Racket editor environment.  So I made something: a [simple
picture language for GNU
Guile](https://git.elephly.net/software/guile-picture-language.git).
It provides simple primitive procedures to generate shapes, to
manipulate them, and to compose them.

Download the single Guile module containing the implementation:

    mkdir ~/pict
    wget https://elephly.net/downies/pict.scm

To actually see these shapes as you play with them, you need to use a
graphical instance of [GNU Emacs](https://gnu.org/software/emacs) with
[Geiser](http://www.nongnu.org/geiser/).

Start geiser in Emacs and load the module:

    M-x run-guile
    (add-to-load-path (string-append (getenv "HOME") "/pict"))
    ,use (pict)

Let’s play!

    (circle 100)
    
If you see a pretty circle: hooray!  Let’s play some more:

    (colorize (circle 100) "red")
    (disk 80)
    (rectangle 50 100)

Let's compose and manipulate some shapes!

    ,use (srfi srfi-1)
    ,use (srfi srfi-26)
    (apply hc-append
           (map (cut circle <>)
                (iota 10 2 4)))

    (apply cc-superimpose
           (map (cut circle <>)
                (iota 10 2 4)))

    (apply hc-append
           (map (cut rotate (rectangle 10 30) <>)
                (iota 36 0 10)))

    (apply cc-superimpose
           (map (cut rotate (triangle 100 300) <>)
                (iota 36 0 10)))

There are many more procedures for primitive shapes and for
manipulations.  Almost all procedures in pict.scm have docstrings, so
feel free to explore the code to find fun things to play with!

PS: I realize that it's silly to have a blog post about a picture
language without any pictures.  Instead of thinking about this now,
get the module and make some pretty pictures yourself!