diff --git a/demo.scm b/demo.scm new file mode 100644 index 0000000..da281ef --- /dev/null +++ b/demo.scm @@ -0,0 +1,14 @@ +(use-modules (graphgif)) + +(define my-graph + `(((10 . 10) (1) ,white) + ((30 . 20) () ,red))) + +(define more-complex-graph + `(((10 . 10) () ,white) + ((40 . 10) (0) ,white) + ((25 . 25) (0 1) ,white) + ((10 . 40) (0 2 4) ,white) + ((40 . 40) (1 2 3) ,white))) + +(write-graph-to-file more-complex-graph (cadr (command-line))) diff --git a/graphgif.scm b/graphgif.scm index 7a9c36f..6e64969 100644 --- a/graphgif.scm +++ b/graphgif.scm @@ -1,28 +1,17 @@ -#!/usr/bin/guile -s -!# +(define-module (graphgif)) + (use-modules (cairo)) ;;;;;;;;;;;;;;;;;;; ;; Basic Drawing ;; ;;;;;;;;;;;;;;;;;;; -(define pi 3.14159) -(define tau (* 2 pi)) +(define-public pi 3.14159) +(define-public tau (* 2 pi)) -(define black (cairo-pattern-create-rgb 0 0 0)) -(define white (cairo-pattern-create-rgb 1 1 1)) -(define red (cairo-pattern-create-rgb 1 0 0)) - -(define my-graph - `(((10 . 10) (1) ,white) - ((30 . 20) () ,red))) - -(define more-complex-graph - `(((10 . 10) () ,white) - ((40 . 10) (0) ,white) - ((25 . 25) (0 1) ,white) - ((10 . 40) (0 2 4) ,white) - ((40 . 40) (1 2 3) ,white))) +(define-public black (cairo-pattern-create-rgb 0 0 0)) +(define-public white (cairo-pattern-create-rgb 1 1 1)) +(define-public red (cairo-pattern-create-rgb 1 0 0)) (define (edge-painter cr graph) (lambda (node) @@ -51,7 +40,7 @@ (cairo-set-source cr black) (cairo-stroke cr)))) -(define (draw-abstract-graph graph) +(define-public (draw-abstract-graph graph) (let* ([surface (cairo-image-surface-create 'argb32 400 400)] [cr (cairo-create surface)]) (define paint-edges (edge-painter cr graph)) @@ -65,9 +54,11 @@ (cairo-destroy cr) surface)) -(define my-surface (draw-abstract-graph more-complex-graph)) -(cairo-surface-write-to-png my-surface (cadr (command-line))) -(cairo-surface-destroy my-surface) +(define-public (write-graph-to-file graph filename) + (define my-surface (draw-abstract-graph graph)) + (cairo-surface-write-to-png my-surface filename) + (cairo-surface-destroy my-surface)) + ;; Local Variables: ;; geiser-scheme-implementation: guile diff --git a/run.sh b/run.sh index 3a7f1d6..4a93048 100755 --- a/run.sh +++ b/run.sh @@ -4,6 +4,6 @@ GRAPH_FILE=/tmp/graph.png set -e -guile graphgif.scm $GRAPH_FILE +guile -L . demo.scm $GRAPH_FILE feh $GRAPH_FILE rm $GRAPH_FILE