Saturday, October 6, 2007

Starting with Vecto

A couple of days ago I read about a library for Common Lisp called Vecto that allows the creation of graphics by describing them using Lisp functions.

Before trying this library, I noticed that it could be installed by using ASDF-Install. ASDF is like a packaging system for Common Lisp libraries and ASDF-Install is a program that let's you locate and install thouse libraries with its dependencies.

I'm using Steel Bank Common Lisp which already has ASDF and ASDF-Install integrated with it.

In order to install Vecto the only thing that I did was:

* (require :asdf)

* (require :asdf-install)
* (asdf-install:install :Vecto)

After answering a couple of questions It downloaded and installed Vecto with all its dependencies.

The library has nice documentation. Here's a little example I created for this post.

(require :vecto)
(defpackage #:vecto-test
(:use #:cl #:vecto))

(in-package #:vecto-test)

(defun triangle (s c)
(set-rgb-stroke (car c)
(cadr c)
(caddr c))
(set-line-width 10)
(set-line-join :round)
(move-to 0 0)
(line-to (- s) 0)
(line-to 0 (- s))
(line-to s 0)

(defun gen-values (from to step)
(if (<= from to)
(cons from
(gen-values (+ from step) to step))

(defun tria (file)
(with-canvas (:width 300 :height 300)
(mapcar #'(lambda (x)
(translate 150 150)
(rotate (cadr x))
(triangle (car x) (list (random 1.0)
(random 1.0)
(random 1.0)))))
(mapcar #'(lambda (length angle) (list length angle))
(gen-values 10 200 30)
(gen-values 0.0 3.14 0.4)))
(save-png file)))

(tria "vecto-test-image.png")

This code generates the following image: