From cd293eb3791f62c36df1f0ae888572716a859c8c Mon Sep 17 00:00:00 2001 From: Neil Toronto Date: Sat, 5 Apr 2014 20:04:29 -0600 Subject: [PATCH] Allow plots with 0-length axes Closes PR 14053 --- .../plot-lib/plot/private/common/format.rkt | 2 +- .../plot/private/no-gui/plot2d-utils.rkt | 21 ++++++++++++------- .../plot/private/no-gui/plot3d-utils.rkt | 21 ++++++++++++------- .../plot-test/plot/tests/plot2d-tests.rkt | 7 +++---- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/pkgs/plot-pkgs/plot-lib/plot/private/common/format.rkt b/pkgs/plot-pkgs/plot-lib/plot/private/common/format.rkt index b4f83ea536..251a5d87a4 100644 --- a/pkgs/plot-pkgs/plot-lib/plot/private/common/format.rkt +++ b/pkgs/plot-pkgs/plot-lib/plot/private/common/format.rkt @@ -79,7 +79,7 @@ (format "~a.~a×10~a" fst rst (integer->superscript (- (add1 i))))])]))) (define (zero-string n) - (list->string (build-list n (λ _ #\0)))) + (make-string n #\0)) (defproc (real->plot-label [x real?] [digits exact-integer?] [scientific? boolean? #t]) any (cond diff --git a/pkgs/plot-pkgs/plot-lib/plot/private/no-gui/plot2d-utils.rkt b/pkgs/plot-pkgs/plot-lib/plot/private/no-gui/plot2d-utils.rkt index e0935693ef..d6bce12bc0 100644 --- a/pkgs/plot-pkgs/plot-lib/plot/private/no-gui/plot2d-utils.rkt +++ b/pkgs/plot-pkgs/plot-lib/plot/private/no-gui/plot2d-utils.rkt @@ -16,13 +16,20 @@ (define (get-bounds-rect renderer-list x-min x-max y-min y-max) (define given-bounds-rect (vector (ivl x-min x-max) (ivl y-min y-max))) - (define plot-bounds-rect (bounds-fixpoint renderer-list given-bounds-rect)) - (when (or (not (rect-rational? plot-bounds-rect)) - (rect-zero-area? plot-bounds-rect)) - (match-define (vector x-ivl y-ivl) plot-bounds-rect) - (error 'plot "could not determine sensible plot bounds; got x ∈ ~a, y ∈ ~a" - (ivl->plot-label x-ivl) (ivl->plot-label y-ivl))) - (rect-inexact->exact plot-bounds-rect)) + (let* ([plot-bounds-rect (bounds-fixpoint renderer-list given-bounds-rect)] + [plot-bounds-rect + (cond [(not (rect-rational? plot-bounds-rect)) + (match-define (vector x-ivl y-ivl) plot-bounds-rect) + (error 'plot "could not determine sensible plot bounds; got x ∈ ~a, y ∈ ~a" + (ivl->plot-label x-ivl) (ivl->plot-label y-ivl))] + [(rect-zero-area? plot-bounds-rect) + (for/vector ([i (in-vector plot-bounds-rect)]) + (match-define (ivl a b) i) + (cond [(= a b) (if (zero? a) (ivl -1 1) (ivl (* a (- 1 1e-2)) (* b (+ 1 1e-2))))] + [else i]))] + [else + plot-bounds-rect])]) + (rect-inexact->exact plot-bounds-rect))) (define (get-ticks renderer-list bounds-rect) (define-values (all-x-ticks all-x-far-ticks all-y-ticks all-y-far-ticks) diff --git a/pkgs/plot-pkgs/plot-lib/plot/private/no-gui/plot3d-utils.rkt b/pkgs/plot-pkgs/plot-lib/plot/private/no-gui/plot3d-utils.rkt index 585ea9459f..4436de1d26 100644 --- a/pkgs/plot-pkgs/plot-lib/plot/private/no-gui/plot3d-utils.rkt +++ b/pkgs/plot-pkgs/plot-lib/plot/private/no-gui/plot3d-utils.rkt @@ -16,13 +16,20 @@ (define (get-bounds-rect renderer-list x-min x-max y-min y-max z-min z-max) (define given-bounds-rect (vector (ivl x-min x-max) (ivl y-min y-max) (ivl z-min z-max))) - (define plot-bounds-rect (bounds-fixpoint renderer-list given-bounds-rect)) - (when (or (not (rect-rational? plot-bounds-rect)) - (rect-zero-area? plot-bounds-rect)) - (match-define (vector x-ivl y-ivl z-ivl) plot-bounds-rect) - (error 'plot "could not determine sensible plot bounds; got x ∈ ~a, y ∈ ~a, z ∈ ~a" - (ivl->plot-label x-ivl) (ivl->plot-label y-ivl) (ivl->plot-label z-ivl))) - (rect-inexact->exact plot-bounds-rect)) + (let* ([plot-bounds-rect (bounds-fixpoint renderer-list given-bounds-rect)] + [plot-bounds-rect + (cond [(not (rect-rational? plot-bounds-rect)) + (match-define (vector x-ivl y-ivl z-ivl) plot-bounds-rect) + (error 'plot "could not determine sensible plot bounds; got x ∈ ~a, y ∈ ~a, z ∈ ~a" + (ivl->plot-label x-ivl) (ivl->plot-label y-ivl) (ivl->plot-label z-ivl))] + [(rect-zero-area? plot-bounds-rect) + (for/vector ([i (in-vector plot-bounds-rect)]) + (match-define (ivl a b) i) + (cond [(= a b) (if (zero? a) (ivl -1 1) (ivl (* a (- 1 1e-2)) (* b (+ 1 1e-2))))] + [else i]))] + [else + plot-bounds-rect])]) + (rect-inexact->exact plot-bounds-rect))) (define (get-ticks renderer-list bounds-rect) (define-values (all-x-ticks all-x-far-ticks all-y-ticks all-y-far-ticks all-z-ticks all-z-far-ticks) diff --git a/pkgs/plot-pkgs/plot-test/plot/tests/plot2d-tests.rkt b/pkgs/plot-pkgs/plot-test/plot/tests/plot2d-tests.rkt index e7b9b4c2d4..e0522a30ec 100644 --- a/pkgs/plot-pkgs/plot-test/plot/tests/plot2d-tests.rkt +++ b/pkgs/plot-pkgs/plot-test/plot/tests/plot2d-tests.rkt @@ -156,10 +156,9 @@ #:color "blue") (axes 1 0 #:y-ticks? #f))) -;; error: could not determine x bounds -(check-exn exn:fail? - (λ () (plot (list (function sqr #f -1) - (function sqr 1 #f))))) +(printf "Plot should be empty:~n") +(plot (list (function sqr #f -1) + (function sqr 1 #f))) ; draws both functions with x in [-1,2] (meaning nothing is drawn) (printf "Plot should be empty:~n")