|
3 | 3 | ;; A compatibility module for the old 'plot'. |
4 | 4 |
|
5 | 5 | (require racket/contract racket/class racket/snip racket/draw racket/vector |
6 | | - unstable/latent-contract unstable/latent-contract/defthing |
7 | 6 | ;; Plotting |
8 | 7 | plot/private/common/math |
9 | 8 | plot/private/common/contract |
|
23 | 22 | ;; Miscellaneous |
24 | 23 | plot/private/deprecated/math) |
25 | 24 |
|
26 | | -(provide mix |
27 | | - (activate-contract-out plot-color? |
28 | | - plot plot3d |
29 | | - points vector-field error-bars |
30 | | - line |
31 | | - contour shade |
32 | | - surface) |
33 | | - ;; Miscellaneous |
34 | | - make-vec derivative gradient) |
| 25 | +(provide |
| 26 | + mix |
| 27 | + (contract-out |
| 28 | + [plot-color? (-> any/c boolean?)] |
| 29 | + [plot |
| 30 | + (->* [((is-a?/c 2d-plot-area%) . -> . void?)] |
| 31 | + [#:width real? |
| 32 | + #:height real? |
| 33 | + #:x-min real? |
| 34 | + #:x-max real? |
| 35 | + #:y-min real? |
| 36 | + #:y-max real? |
| 37 | + #:x-label string? |
| 38 | + #:y-label string? |
| 39 | + #:title string? |
| 40 | + #:fgcolor (list/c byte? byte? byte?) |
| 41 | + #:bgcolor (list/c byte? byte? byte?) |
| 42 | + #:lncolor (list/c byte? byte? byte?) |
| 43 | + #:out-file (or/c path-string? output-port? #f)] |
| 44 | + (is-a?/c image-snip%))] |
| 45 | + [plot3d |
| 46 | + (->* [((is-a?/c 3d-plot-area%) . -> . void?)] |
| 47 | + [#:width real? |
| 48 | + #:height real? |
| 49 | + #:x-min real? |
| 50 | + #:x-max real? |
| 51 | + #:y-min real? |
| 52 | + #:y-max real? |
| 53 | + #:z-min real? |
| 54 | + #:z-max real? |
| 55 | + #:alt real? |
| 56 | + #:az real? |
| 57 | + #:x-label string? |
| 58 | + #:y-label string? |
| 59 | + #:z-label string? |
| 60 | + #:title string? |
| 61 | + #:fgcolor (list/c byte? byte? byte?) |
| 62 | + #:bgcolor (list/c byte? byte? byte?) |
| 63 | + #:lncolor (list/c byte? byte? byte?) |
| 64 | + #:out-file (or/c path-string? output-port? #f)] |
| 65 | + (is-a?/c image-snip%))] |
| 66 | + [points |
| 67 | + (->* [(listof (vectorof real?))] |
| 68 | + [#:sym (or/c char? string? exact-integer? symbol?) |
| 69 | + #:color plot-color?] |
| 70 | + ((is-a?/c 2d-plot-area%) . -> . void?))] |
| 71 | + [vector-field |
| 72 | + (->* [((vector/c real? real?) . -> . (vector/c real? real?))] |
| 73 | + [#:samples (and/c exact-integer? (>=/c 2)) |
| 74 | + #:width exact-positive-integer? |
| 75 | + #:color plot-color? |
| 76 | + #:style (one-of/c 'scaled 'normalized 'real)] |
| 77 | + ((is-a?/c 2d-plot-area%) . -> . void?))] |
| 78 | + [error-bars |
| 79 | + (->* [(listof (vector/c real? real? real?))] |
| 80 | + [#:color plot-color?] |
| 81 | + ((is-a?/c 2d-plot-area%) . -> . void?))] |
| 82 | + [line |
| 83 | + (->* [(real? . -> . (or/c real? (vector/c real? real?)))] |
| 84 | + [#:samples (and/c exact-integer? (>=/c 2)) |
| 85 | + #:width (>=/c 0) |
| 86 | + #:color plot-color/c |
| 87 | + #:mode (one-of/c 'standard 'parametric) |
| 88 | + #:mapping (one-of/c 'cartesian 'polar) |
| 89 | + #:t-min real? |
| 90 | + #:t-max real?] |
| 91 | + ((is-a?/c 2d-plot-area%) . -> . void?))] |
| 92 | + [contour |
| 93 | + (->* [(real? real? . -> . real?)] |
| 94 | + [#:samples exact-nonnegative-integer? |
| 95 | + #:width (>=/c 0) |
| 96 | + #:color plot-color/c |
| 97 | + #:levels (or/c (and/c exact-integer? (>=/c 2)) (listof real?))] |
| 98 | + ((is-a?/c 2d-plot-area%) . -> . void?))] |
| 99 | + [shade |
| 100 | + (->* [(real? real? . -> . real?)] |
| 101 | + [#:samples (and/c exact-integer? (>=/c 2)) |
| 102 | + #:levels (or/c (and/c exact-integer? (>=/c 2)) (listof real?))] |
| 103 | + ((is-a?/c 2d-plot-area%) . -> . void?))] |
| 104 | + [surface |
| 105 | + (->* [(real? real? . -> . real?)] |
| 106 | + [#:samples (and/c exact-integer? (>=/c 2)) |
| 107 | + #:width (>=/c 0) |
| 108 | + #:color plot-color/c] |
| 109 | + ((is-a?/c 3d-plot-area%) . -> . void?))] |
| 110 | + ) |
| 111 | + ;; Miscellaneous |
| 112 | + make-vec |
| 113 | + derivative |
| 114 | + gradient) |
35 | 115 |
|
36 | 116 | (define (mix . data) |
37 | 117 | (for/fold ([f (λ (area) (void))]) ([d (in-list data)]) |
|
40 | 120 | (d area) |
41 | 121 | (void)))) |
42 | 122 |
|
43 | | -(defproc (plot-color? [v any/c]) boolean? |
| 123 | +(define (plot-color? v) |
44 | 124 | (and (member v '(white black yellow green aqua pink wheat gray brown blue violet cyan |
45 | 125 | turquoise magenta salmon red)) |
46 | 126 | #t)) |
|
59 | 139 | (define x-axis-data (renderer2d->plot-data (new.x-axis))) |
60 | 140 | (define y-axis-data (renderer2d->plot-data (new.y-axis))) |
61 | 141 |
|
62 | | -(defproc (plot [data ((is-a?/c 2d-plot-area%) . -> . void?)] |
63 | | - [#:width width real? 400] [#:height height real? 400] |
64 | | - [#:x-min x-min real? -5] [#:x-max x-max real? 5] |
65 | | - [#:y-min y-min real? -5] [#:y-max y-max real? 5] |
66 | | - [#:x-label x-label string? "X axis"] [#:y-label y-label string? "Y axis"] |
67 | | - [#:title title string? ""] |
68 | | - [#:fgcolor fgcolor (list/c byte? byte? byte?) '(0 0 0)] |
69 | | - [#:bgcolor bgcolor (list/c byte? byte? byte?) '(255 255 255)] |
70 | | - [#:lncolor lncolor (list/c byte? byte? byte?) '(255 0 0)] |
71 | | - [#:out-file out-file (or/c path-string? output-port? #f) #f] |
72 | | - ) (is-a?/c image-snip%) |
| 142 | +(define (plot data |
| 143 | + #:width [width 400] |
| 144 | + #:height [height 400] |
| 145 | + #:x-min [x-min -5] |
| 146 | + #:x-max [x-max 5] |
| 147 | + #:y-min [y-min -5] |
| 148 | + #:y-max [y-max 5] |
| 149 | + #:x-label [x-label "X axis"] |
| 150 | + #:y-label [y-label "Y axis"] |
| 151 | + #:title [title ""] |
| 152 | + #:fgcolor [fgcolor '(0 0 0)] |
| 153 | + #:bgcolor [bgcolor '(255 255 255)] |
| 154 | + #:lncolor [lncolor '(255 0 0)] |
| 155 | + #:out-file [out-file #f]) |
73 | 156 | (define x-ticks (ticks-generate (new.plot-x-ticks) x-min x-max)) |
74 | 157 | (define y-ticks (ticks-generate (new.plot-y-ticks) y-min y-max)) |
75 | 158 | (define bounds-rect (vector (ivl x-min x-max) (ivl y-min y-max))) |
|
96 | 179 |
|
97 | 180 | (make-object image-snip% bm))) |
98 | 181 |
|
99 | | -(defproc (plot3d [data ((is-a?/c 3d-plot-area%) . -> . void?)] |
100 | | - [#:width width real? 400] [#:height height real? 400] |
101 | | - [#:x-min x-min real? -5] [#:x-max x-max real? 5] |
102 | | - [#:y-min y-min real? -5] [#:y-max y-max real? 5] |
103 | | - [#:z-min z-min real? -5] [#:z-max z-max real? 5] |
104 | | - [#:alt alt real? 30] |
105 | | - [#:az az real? 45] |
106 | | - [#:x-label x-label string? "X axis"] |
107 | | - [#:y-label y-label string? "Y axis"] |
108 | | - [#:z-label z-label string? "Z axis"] |
109 | | - [#:title title string? ""] |
110 | | - [#:fgcolor fgcolor (list/c byte? byte? byte?) '(0 0 0)] |
111 | | - [#:bgcolor bgcolor (list/c byte? byte? byte?) '(255 255 255)] |
112 | | - [#:lncolor lncolor (list/c byte? byte? byte?) '(255 0 0)] |
113 | | - [#:out-file out-file (or/c path-string? output-port? #f) #f] |
114 | | - ) (is-a?/c image-snip%) |
| 182 | +(define (plot3d data |
| 183 | + #:width [width 400] |
| 184 | + #:height [height 400] |
| 185 | + #:x-min [x-min -5] |
| 186 | + #:x-max [x-max 5] |
| 187 | + #:y-min [y-min -5] |
| 188 | + #:y-max [y-max 5] |
| 189 | + #:z-min [z-min -5] |
| 190 | + #:z-max [z-max 5] |
| 191 | + #:alt [alt 30] |
| 192 | + #:az [az 45] |
| 193 | + #:x-label [x-label "X axis"] |
| 194 | + #:y-label [y-label "Y axis"] |
| 195 | + #:z-label [z-label "Z axis"] |
| 196 | + #:title [title ""] |
| 197 | + #:fgcolor [fgcolor '(0 0 0)] |
| 198 | + #:bgcolor [bgcolor '(255 255 255)] |
| 199 | + #:lncolor [lncolor '(255 0 0)] |
| 200 | + #:out-file [out-file #f]) |
115 | 201 | (define x-ticks (ticks-generate (new.plot-x-ticks) x-min x-max)) |
116 | 202 | (define y-ticks (ticks-generate (new.plot-y-ticks) y-min y-max)) |
117 | 203 | (define z-ticks (ticks-generate (new.plot-z-ticks) z-min z-max)) |
|
143 | 229 | ;; =================================================================================================== |
144 | 230 | ;; Functions that generate "plot data" |
145 | 231 |
|
146 | | -(defproc (points [vecs (listof (vectorof real?))] |
147 | | - [#:sym sym (or/c char? string? exact-integer? symbol?) 'square] |
148 | | - [#:color color plot-color? 'black] |
149 | | - ) ((is-a?/c 2d-plot-area%) . -> . void?) |
| 232 | +(define (points vecs #:sym [sym 'square] #:color [color 'black]) |
150 | 233 | (renderer2d->plot-data (new.points (map (λ (v) (vector-take v 2)) vecs) |
151 | 234 | #:sym sym #:size 6 #:color color))) |
152 | 235 |
|
153 | | -(defproc (vector-field [f ((vector/c real? real?) . -> . (vector/c real? real?))] |
154 | | - [#:samples samples (and/c exact-integer? (>=/c 2)) 20] |
155 | | - [#:width width exact-positive-integer? 1] |
156 | | - [#:color color plot-color? 'red] |
157 | | - [#:style style (one-of/c 'scaled 'normalized 'real) 'scaled] |
158 | | - ) ((is-a?/c 2d-plot-area%) . -> . void?) |
| 236 | +(define (vector-field f |
| 237 | + #:samples [samples 20] |
| 238 | + #:width [width 1] |
| 239 | + #:color [color 'red] |
| 240 | + #:style [style 'scaled]) |
159 | 241 | (define scale (case style |
160 | 242 | [(scaled) 'auto] |
161 | 243 | [(normalized) 'normalized] |
162 | 244 | [(real) 1.0])) |
163 | 245 | (renderer2d->plot-data |
164 | 246 | (new.vector-field f #:samples samples #:line-width width #:color color #:scale scale))) |
165 | 247 |
|
166 | | -(defproc (error-bars [vecs (listof (vector/c real? real? real?))] |
167 | | - [#:color color plot-color? 'black] |
168 | | - ) ((is-a?/c 2d-plot-area%) . -> . void?) |
| 248 | +(define (error-bars vecs #:color [color 'black]) |
169 | 249 | (renderer2d->plot-data (new.error-bars vecs #:color color #:alpha 1 #:width 4))) |
170 | 250 |
|
171 | | -(defproc (line [f (real? . -> . (or/c real? (vector/c real? real?)))] |
172 | | - [#:samples samples (and/c exact-integer? (>=/c 2)) 150] |
173 | | - [#:width width (>=/c 0) 1] |
174 | | - [#:color color plot-color/c 'red] |
175 | | - [#:mode mode (one-of/c 'standard 'parametric) 'standard] |
176 | | - [#:mapping mapping (one-of/c 'cartesian 'polar) 'cartesian] |
177 | | - [#:t-min t-min real? -5] [#:t-max t-max real? 5] |
178 | | - ) ((is-a?/c 2d-plot-area%) . -> . void?) |
| 251 | +(define (line f |
| 252 | + #:samples [samples 150] |
| 253 | + #:width [width 1] |
| 254 | + #:color [color 'red] |
| 255 | + #:mode [mode 'standard] |
| 256 | + #:mapping [mapping 'cartesian] |
| 257 | + #:t-min [t-min -5] |
| 258 | + #:t-max [t-max 5]) |
179 | 259 | (renderer2d->plot-data (line-renderer f samples width color mode mapping t-min t-max))) |
180 | 260 |
|
181 | | -(defproc (contour [f (real? real? . -> . real?)] |
182 | | - [#:samples samples exact-nonnegative-integer? 50] |
183 | | - [#:width width (>=/c 0) 1] |
184 | | - [#:color color plot-color/c 'black] |
185 | | - [#:levels levels (or/c (and/c exact-integer? (>=/c 2)) (listof real?)) 10] |
186 | | - ) ((is-a?/c 2d-plot-area%) . -> . void?) |
| 261 | +(define (contour f |
| 262 | + #:samples [samples 50] |
| 263 | + #:width [width 1] |
| 264 | + #:color [color 'black] |
| 265 | + #:levels [levels 10]) |
187 | 266 | (renderer2d->plot-data (contour-renderer f samples width color levels))) |
188 | 267 |
|
189 | | -(defproc (shade [f (real? real? . -> . real?)] |
190 | | - [#:samples samples (and/c exact-integer? (>=/c 2)) 50] |
191 | | - [#:levels levels (or/c (and/c exact-integer? (>=/c 2)) (listof real?)) 10] |
192 | | - ) ((is-a?/c 2d-plot-area%) . -> . void?) |
| 268 | +(define (shade f #:samples [samples 50] #:levels [levels 10]) |
193 | 269 | (renderer2d->plot-data (shade-renderer f samples levels))) |
194 | 270 |
|
195 | | -(defproc (surface [f (real? real? . -> . real?)] |
196 | | - [#:samples samples (and/c exact-integer? (>=/c 2)) 50] |
197 | | - [#:width width (>=/c 0) 1] |
198 | | - [#:color color plot-color/c 'black] |
199 | | - ) ((is-a?/c 3d-plot-area%) . -> . void?) |
| 271 | +(define (surface f #:samples [samples 50] #:width [width 1] #:color [color 'black]) |
200 | 272 | (renderer3d->plot-data (surface-renderer f samples width color))) |
0 commit comments