-
-
Notifications
You must be signed in to change notification settings - Fork 37
add color-field 2D renderer #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
98e6589
color field, v1
bdeket f3e96bd
color-field: clean up imports
bdeket 1ff4775
color-field: provide parameters
bdeket 6c88c27
color-field: move test
bdeket 6ecbb12
color-field: add test data
bdeket 1470310
color-field: add history notes and remove code comments
bdeket File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #lang typed/racket/base | ||
|
|
||
| ;; Renderers for points and other point-like things. | ||
|
|
||
| (require typed/racket/class racket/match racket/list | ||
| plot/utils | ||
| "../common/type-doc.rkt" | ||
| "../common/utils.rkt") | ||
|
|
||
| (require/typed | ||
| "../common/untyped-utils.rkt" | ||
| [fix-a-field-fun (All (A) | ||
| (-> Symbol | ||
| (U (-> Real Real A) | ||
| (-> (Vector Real Real) A)) | ||
| (-> Real Real A)))]) | ||
|
|
||
| (provide (all-defined-out)) | ||
|
|
||
| ;; =================================================================================================== | ||
| ;; color-field | ||
| ;; similar to point.rkt/vector-field, but draws a square area with a color | ||
|
|
||
| (: color-field-render-fun | ||
| (-> (-> Real Real Plot-Color) | ||
| Positive-Integer | ||
| Nonnegative-Real | ||
| 2D-Render-Proc)) | ||
| (define ((color-field-render-fun f samples alpha) area) | ||
| (match-define (vector (ivl x-min x-max) (ivl y-min y-max)) (send area get-bounds-rect)) | ||
|
|
||
| (cond | ||
| [(and x-min x-max y-min y-max) | ||
| (define xs (linear-seq x-min x-max (+ samples 1) #:start? #t #:end? #t)) | ||
| (define ys (linear-seq y-min y-max (+ samples 1) #:start? #t #:end? #t)) | ||
|
|
||
| (send area put-alpha alpha) | ||
| (send area put-pen 'black 0 'transparent) | ||
| (for ([x- (in-list xs)] | ||
| [x+ (in-list (cdr xs))]) | ||
| (define x (/ (+ x- x+) 2)) | ||
| (for ([y- (in-list ys)] | ||
| [y+ (in-list (cdr ys))]) | ||
| (define y (/ (+ y- y+) 2)) | ||
| (define c (f x y)) | ||
| (send area put-brush c 'solid) | ||
| (send area put-rect (vector (ivl x- x+) | ||
| (ivl y- y+))))) | ||
| empty] | ||
| [else empty])) | ||
|
|
||
| (:: color-field | ||
| (->* [(U (-> Real Real Plot-Color) | ||
| (-> (Vector Real Real) Plot-Color))] | ||
| [(U Real #f) (U Real #f) | ||
| (U Real #f) (U Real #f) | ||
| #:samples Positive-Integer | ||
| #:alpha Nonnegative-Real] | ||
| renderer2d)) | ||
| (define (color-field f [x-min #f] [x-max #f] [y-min #f] [y-max #f] | ||
| #:samples [samples (color-field-samples)] | ||
| #:alpha [alpha (color-field-alpha)]) | ||
| (define fail/pos (make-raise-argument-error 'vector-field3d f x-min x-max y-min y-max)) | ||
| (define fail/kw (make-raise-keyword-error 'vector-field3d)) | ||
| (cond | ||
| [(and x-min (not (rational? x-min))) (fail/pos "#f or rational" 1)] | ||
| [(and x-max (not (rational? x-max))) (fail/pos "#f or rational" 2)] | ||
| [(and y-min (not (rational? y-min))) (fail/pos "#f or rational" 3)] | ||
| [(and y-max (not (rational? y-max))) (fail/pos "#f or rational" 4)] | ||
| [(or (> alpha 1) (not (rational? alpha))) (fail/kw "real in [0,1]" '#:alpha alpha)] | ||
| [else | ||
| (let ([f ((inst fix-a-field-fun Plot-Color) 'color-field f)]) | ||
| (renderer2d (vector (ivl x-min x-max) (ivl y-min y-max)) #f default-ticks-fun | ||
| (color-field-render-fun | ||
| f samples alpha)))])) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #lang racket | ||
| (require rackunit | ||
| plot | ||
| racket/draw | ||
| racket/runtime-path | ||
| "../helpers.rkt") | ||
|
|
||
| ;; Tests for: https://github.com/racket/plot/pull/66 : color-field | ||
| (define (do-plot-color-field output-fn) | ||
| (output-fn | ||
| (color-field | ||
| (λ (x y) | ||
| (define z (make-rectangular x y)) | ||
| (if (< (magnitude z) 1) | ||
| (cond | ||
| [(< (magnitude z) 0.5) 'red] | ||
| [(< (angle z) 0) 'blue] | ||
| [else 'green]) | ||
| 'black)) | ||
| -2 2 -2 2))) | ||
|
|
||
| (define-runtime-path pr66-color-field-data "./test-data/pr66-1.dat") | ||
|
|
||
| (define pr66-test-suite | ||
| (test-suite | ||
| "PR#66: color-field" | ||
| (test-case "pr66-color-field" | ||
| (check-draw-steps do-plot-color-field pr66-color-field-data)))) | ||
|
|
||
| (module+ test | ||
| (require rackunit/text-ui) | ||
| (run-tests pr66-test-suite)) | ||
|
|
||
| ;; | ||
|
|
||
|
|
||
|
|
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.