graphgif/graphgif.scm

66 lines
1.6 KiB
Scheme

(define-module (graphgif))
(use-modules (cairo))
;;;;;;;;;;;;;;;;;;;
;; Basic Drawing ;;
;;;;;;;;;;;;;;;;;;;
(define-public pi 3.14159)
(define-public tau (* 2 pi))
(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)
(cairo-set-source cr black)
(let ([x (caar node)]
[y (cdar node)]
[edges (cadr node)])
(for-each
(lambda (edge)
(let* ([other (list-ref graph edge)]
[ox (caar other)]
[oy (cdar other)])
(cairo-move-to cr x y)
(cairo-line-to cr ox oy)
(cairo-stroke cr)))
edges))))
(define (node-painter cr)
(lambda (node)
(let ([x (caar node)]
[y (cdar node)]
[color (caddr node)])
(cairo-arc cr x y 4. 0. tau)
(cairo-set-source cr color)
(cairo-fill-preserve cr)
(cairo-set-source cr black)
(cairo-stroke cr))))
(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))
(define paint-nodes (node-painter cr))
;; White background
(cairo-rectangle cr 0 0 400 400)
(cairo-set-source cr white)
(cairo-fill cr)
(for-each paint-edges graph)
(for-each paint-nodes graph)
(cairo-destroy cr)
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
;; End: