Graphics DSL
Learning Processing is a book that teaches graphics programming. The target audience is digital artists with no prior experience in programming. But this could also include professional programmers who wish to refresh their graphics programming skills, teachers of preliminary programming courses or people who are just curious about multimedia programming. It uses the Processing language as the programming medium. To work through the examples and exercises I decided to use Spark-Scheme - for two reasons:
- It has excellent 2D/3D capabilities.
- Being a Lisp, it can be easily molded into a DSL for specifying digital images.
So I started implementing this new graphics language on top of Spark. Right now it has one special form, define-display, which allows the artist to issue simple graphics commands to the Spark-Scheme interpreter and immediately view the result on screen. Let us look at a simple display definition and its output:
(define-display basic-shapes (background 'white) (stroke 'black) (fill 'dark-yellow) (rect 50 40 75 100) (fill 'dark-red) (ellipse 150 140 100 100)) (basic-shapes) ;; Displays the following image on screen.
define-display adds a procedure to the environment which translate the commands to Spark's lower-level graphics calls. This procedure can optionally receive the title, position and size of the window in which the image is rendered. The following example makes use of these arguments:
(define-display line-across-rect (background 150) (stroke 'red) (line 0 0 100 100) (stroke 'white) (no-fill) (rect 25 25 50 50)) (line-across-rect "Line-Rect" 100 100 100 100)
Here is the solution to exercise 1.4:
(define-display squares
(with (a 100)
(background 'white)
(stroke 'black)
(fill 'dark-red)
(rect 0 0 a a)
(fill (rgb 205 183 158))
(rect a a a a)))
(squares "Squares" 100 100 200 200)