set up to use quicklisp to load cairo2d, will need to do the same for cl-2d
[CommonLispStat.git] / examples / 20-visual-2d-cairo2.lisp
blob9cfc55f26f7a52b765e64e8cb47af51b9d714e72
1 ;;; -*- mode: lisp -*-
3 ;;; Time-stamp: <2012-10-12 08:54:07 tony>
4 ;;; Creation: <2009-03-12 17:14:56 tony>
5 ;;; File: 20-visual-2d-cairo.lisp
6 ;;; Author: AJ Rossini <blindglobe@gmail.com>
7 ;;; Copyright: (c)2009--2012, AJ Rossini. MIT license.
8 ;;; Purpose: Example visualizations using cl-2d and cl-cairo2
10 ;;; What is this talk of 'release'? Klingons do not make software
11 ;;; 'releases'. Our software 'escapes', leaving a bloody trail of
12 ;;; designers and quality assurance people in its wake.
14 ;;; This organization and structure is new to the 21st Century
15 ;;; version.. Think, "21st Century Schizoid Man".
17 ;; SETUP FOR PLOT EXAMPLE:
18 (in-package :cl-user)
20 (ql:quickload :cl-cairo2)
21 (ql:quickload :cl-2d)
23 (defpackage :cls-2d-user-x11
24 ;; do we need all of these for the plotting? not sure about numlib and cairo2?
25 (:use :cl :cl-cairo2 :cl-2d :cl-numlib :cl-colors :bind :cls)
26 (:shadowing-import-from
27 :lisp-stat call-method call-next-method
29 expt + - * / ** mod rem abs 1+ 1- log exp sqrt sin cos tan
30 asin acos atan sinh cosh tanh asinh acosh atanh float random
31 truncate floor ceiling round minusp zerop plusp evenp oddp
32 < <= = /= >= > > ;; complex
33 conjugate realpart imagpart phase
34 min max logand logior logxor lognot ffloor fceiling
35 ftruncate fround signum cis
36 ;; ?? where did these come from? <= float imagpart
38 ;; (:export objects and data that we want to use post load)
41 (in-package :cls-2d-user-x11)
43 ;; PLOT EXAMPLE
44 #+nil
45 (progn
48 ;; this is how you create an X11 frame. If you supply a
49 ;; background-color to as-frame, each plot will clear the frame with
50 ;; this color.
52 (defparameter *frame1* (as-frame (create-xlib-image-context 300 300)
53 :background-color +white+))
55 ;; or netbook size, picture is similar but on a lower-res display window.
56 (defparameter *frame2* (as-frame (create-xlib-image-context 200 200)
57 :background-color +white+))
59 (plot-function *frame1*
60 #'exp (interval-of 0 2)
61 :x-title "x"
62 :y-title "exp(x)")
64 ;; split the frame, and you can draw on the subframes independently.
65 ;; I do this a lot.
67 (bind ((#2A((f1 f2) (f3 f4))
68 (split-frame *frame2* (percent 50) (percent 50))))
69 (defparameter *f1* f1)
70 (defparameter *f2* f2)
71 (defparameter *f3* f3)
72 (defparameter *f4* f4))
75 (bind ((#2A((f1 f2) (f3 f4))
76 (split-frame *frame2* (percent 75) (percent 25))))
77 (defparameter *f1* f1)
78 (defparameter *f2* f2)
79 (defparameter *f3* f3)
80 (defparameter *f4* f4))
82 (plot-function *f1* #'sin (interval-of 0 2) :x-title "x" :y-title "sin(x)")
83 (plot-function *f2* #'cos (interval-of 0 2) :x-title "x" :y-title "cos(x)")
84 (plot-function *f3* #'tan (interval-of 0 2) :x-title "x" :y-title "tan(x)")
85 (plot-function *f4* #'/ (interval-of 0 2) :x-title "x" :y-title "1/x")
87 (clear *frame1*)
90 (let* ((n 500)
91 (xs (num-sequence :from 0 :to 10 :length n))
92 (ys (map 'vector #'(lambda (x) (+ x 8 (random 4.0))) xs))
93 (weights (replicate #'(lambda () (1+ (random 10))) n 'fixnum))
94 (da (plot-simple *frame1* (interval-of 0 10) (interval-of 10 20)
95 :x-title "x" :y-title "y")))
96 (draw-symbols da xs ys :weights weights))
98 (xlib-image-context-to-png (context *frame1*) "/home/tony/test1.png")
99 (xlib-image-context-to-png (context *frame2*) "/home/tony/test2.png")
103 ;;; EXAMPLE FOR DSC2009
104 (defparameter *frame2* (as-frame (create-xlib-image-context 300 300)
105 :background-color +white+))
107 (bind ((#2A((f1 f2) (f3 f4))
108 (split-frame *frame2* (percent 50) (percent 50))))
109 (defparameter *f1* f1)
110 (defparameter *f2* f2)
111 (defparameter *f3* f3)
112 (defparameter *f4* f4))
113 (plot-function *f1* #'sin (interval-of 0 2) :x-title "x" :y-title "sin(x)")
114 (plot-function *f2* #'cos (interval-of 0 2) :x-title "x" :y-title "cos(x)")
115 (plot-function *f3* #'tan (interval-of 0 2) :x-title "x" :y-title "tan(x)")
117 (num-sequence :from 0 :to 10 :length 30)
119 (let* ((n 500)
120 (xs (num-sequence :from 0 :to 10 :length n))
121 (ys (map 'vector #'(lambda (x) (+ x 8 (random 4.0))) xs))
122 (weights (replicate #'(lambda () (1+ (random 10))) n 'fixnum))
123 (da (plot-simple *f4* (interval-of 0 10) (interval-of 10 20)
124 :x-title "x" :y-title "y")))
125 (draw-symbols da xs ys :weights weights))
126 (xlib-image-context-to-png (context *f1*) "/home/tony/test1.png")
127 (xlib-image-context-to-png (context *frame2*) "/home/tony/test2.png")
128 (destroy (context *frame2*))
130 ;;; End of examples