|
| 1 | +#lang typed/racket/base |
| 2 | + |
| 3 | +;; Renderers for points and other point-like things. |
| 4 | + |
| 5 | +(require typed/racket/class racket/match racket/list |
| 6 | + plot/utils |
| 7 | + "../common/type-doc.rkt" |
| 8 | + "../common/utils.rkt") |
| 9 | + |
| 10 | +(require/typed |
| 11 | + "../common/untyped-utils.rkt" |
| 12 | + [fix-a-field-fun (All (A) |
| 13 | + (-> Symbol |
| 14 | + (U (-> Real Real A) |
| 15 | + (-> (Vector Real Real) A)) |
| 16 | + (-> Real Real A)))]) |
| 17 | + |
| 18 | +(provide (all-defined-out)) |
| 19 | + |
| 20 | +;; =================================================================================================== |
| 21 | +;; color-field |
| 22 | +;; similar to point.rkt/vector-field, but draws a square area with a color |
| 23 | + |
| 24 | +(: color-field-render-fun |
| 25 | + (-> (-> Real Real Plot-Color) |
| 26 | + Positive-Integer |
| 27 | + Nonnegative-Real |
| 28 | + 2D-Render-Proc)) |
| 29 | +(define ((color-field-render-fun f samples alpha) area) |
| 30 | + (match-define (vector (ivl x-min x-max) (ivl y-min y-max)) (send area get-bounds-rect)) |
| 31 | + |
| 32 | + (cond |
| 33 | + [(and x-min x-max y-min y-max) |
| 34 | + (define xs (linear-seq x-min x-max (+ samples 1) #:start? #t #:end? #t)) |
| 35 | + (define ys (linear-seq y-min y-max (+ samples 1) #:start? #t #:end? #t)) |
| 36 | + |
| 37 | + (send area put-alpha alpha) |
| 38 | + (send area put-pen 'black 0 'transparent) |
| 39 | + (for ([x- (in-list xs)] |
| 40 | + [x+ (in-list (cdr xs))]) |
| 41 | + (define x (/ (+ x- x+) 2)) |
| 42 | + (for ([y- (in-list ys)] |
| 43 | + [y+ (in-list (cdr ys))]) |
| 44 | + (define y (/ (+ y- y+) 2)) |
| 45 | + (define c (f x y)) |
| 46 | + (send area put-brush c 'solid) |
| 47 | + (send area put-rect (vector (ivl x- x+) |
| 48 | + (ivl y- y+))))) |
| 49 | + empty] |
| 50 | + [else empty])) |
| 51 | + |
| 52 | +(:: color-field |
| 53 | + (->* [(U (-> Real Real Plot-Color) |
| 54 | + (-> (Vector Real Real) Plot-Color))] |
| 55 | + [(U Real #f) (U Real #f) |
| 56 | + (U Real #f) (U Real #f) |
| 57 | + #:samples Positive-Integer |
| 58 | + #:alpha Nonnegative-Real] |
| 59 | + renderer2d)) |
| 60 | +(define (color-field f [x-min #f] [x-max #f] [y-min #f] [y-max #f] |
| 61 | + #:samples [samples (color-field-samples)] |
| 62 | + #:alpha [alpha (color-field-alpha)]) |
| 63 | + (define fail/pos (make-raise-argument-error 'vector-field3d f x-min x-max y-min y-max)) |
| 64 | + (define fail/kw (make-raise-keyword-error 'vector-field3d)) |
| 65 | + (cond |
| 66 | + [(and x-min (not (rational? x-min))) (fail/pos "#f or rational" 1)] |
| 67 | + [(and x-max (not (rational? x-max))) (fail/pos "#f or rational" 2)] |
| 68 | + [(and y-min (not (rational? y-min))) (fail/pos "#f or rational" 3)] |
| 69 | + [(and y-max (not (rational? y-max))) (fail/pos "#f or rational" 4)] |
| 70 | + [(or (> alpha 1) (not (rational? alpha))) (fail/kw "real in [0,1]" '#:alpha alpha)] |
| 71 | + [else |
| 72 | + (let ([f ((inst fix-a-field-fun Plot-Color) 'color-field f)]) |
| 73 | + (renderer2d (vector (ivl x-min x-max) (ivl y-min y-max)) #f default-ticks-fun |
| 74 | + (color-field-render-fun |
| 75 | + f samples alpha)))])) |
| 76 | + |
0 commit comments