#lang racket
(require plot/plot-extend)
(define (draw-column-given-widths
x x-width y y-width width-factor z view)
(let* ((half-x-whitespace (/ (* x-width (- 1.0 width-factor)) 2.0))
(x1 (+ x half-x-whitespace))
(x2 (- (+ x x-width) half-x-whitespace))
(half-y-whitespace (/ (* y-width (- 1.0 width-factor)) 2.0))
(y1 (+ y half-y-whitespace))
(y2 (- (+ y y-width) half-y-whitespace)))
(send view plot-polygon
`(,x1 ,x2 ,x2 ,x1 ,x1)
`(,y1 ,y1 ,y2 ,y2 ,y1)
'(0 0 0 0 0)
'(1 1 1 1 1)
1)
(send view plot-polygon
`(,x1 ,x2 ,x2 ,x1 ,x1)
`(,y1 ,y1, y1 ,y1 ,y1)
`(0 0 ,z ,z 0)
'(1 1 1 1 1)
1)
(send view plot-polygon
`(,x2 ,x2 ,x2 ,x2 ,x2)
`(,y1 ,y2 ,y2 ,y1 ,y1)
`(0 0 ,z ,z 0)
'(1 1 1 1 1)
1)
(send view plot-polygon
`(,x2 ,x1 ,x1 ,x2 ,x2)
`(,y2 ,y2 ,y2 ,y2 ,y2)
`(0 0 ,z ,z 0)
'(1 1 1 1 1)
1)
(send view plot-polygon
`(,x1 ,x1 ,x1 ,x1 ,x1)
`(,y2 ,y1 ,y1 ,y2 ,y2)
`(0 0 ,z ,z 0)
'(1 1 1 1 1)
1)
(send view plot-polygon
`(,x1 ,x2 ,x2 ,x1 ,x1)
`(,y1 ,y1 ,y2 ,y2 ,y1)
`(,z ,z ,z ,z ,z)
'(1 1 1 1 1)
1)))
(define (draw-column x1 x2 y1 y2 width-factor z view)
(let* ((half-x-whitespace (/ (* (- x2 x1) (- 1.0 width-factor)) 2.0))
(half-y-whitespace (/ (* (- y2 y1) (- 1.0 width-factor)) 2.0)))
(set! x1 (+ x1 half-x-whitespace))
(set! x2 (- x2 half-x-whitespace))
(set! y1 (+ y1 half-y-whitespace))
(set! y2 (- y2 half-y-whitespace))
(send view plot-polygon
`(,x1 ,x2 ,x2 ,x1 ,x1)
`(,y1 ,y1 ,y2 ,y2 ,y1)
'(0 0 0 0 0)
'(1 1 1 1 1)
1)
(send view plot-polygon
`(,x1 ,x2 ,x2 ,x1 ,x1)
`(,y1 ,y1, y1 ,y1 ,y1)
`(0 0 ,z ,z 0)
'(1 1 1 1 1)
1)
(send view plot-polygon
`(,x2 ,x2 ,x2 ,x2 ,x2)
`(,y1 ,y2 ,y2 ,y1 ,y1)
`(0 0 ,z ,z 0)
'(1 1 1 1 1)
1)
(send view plot-polygon
`(,x2 ,x1 ,x1 ,x2 ,x2)
`(,y2 ,y2 ,y2 ,y2 ,y2)
`(0 0 ,z ,z 0)
'(1 1 1 1 1)
1)
(send view plot-polygon
`(,x1 ,x1 ,x1 ,x1 ,x1)
`(,y2 ,y1 ,y1 ,y2 ,y2)
`(0 0 ,z ,z 0)
'(1 1 1 1 1)
1)
(send view plot-polygon
`(,x1 ,x2 ,x2 ,x1 ,x1)
`(,y1 ,y1 ,y2 ,y2 ,y1)
`(,z ,z ,z ,z ,z)
'(1 1 1 1 1)
1)))
(define (draw-histogram-2d
bins x-ranges y-ranges width-factor view)
(let ((nx (- (vector-length x-ranges) 1))
(ny (- (vector-length y-ranges) 1)))
(do ((i 0 (+ i 1)))
((= i nx) (void))
(do ((j 0 (+ j 1)))
((= j ny) (void))
(let ((bin (+ (* i ny) j)))
(draw-column (vector-ref x-ranges i)
(vector-ref x-ranges (+ i 1))
(vector-ref y-ranges j)
(vector-ref y-ranges (+ j 1))
width-factor
(vector-ref bins bin)
view))))))
(define-plot-type histogram-2d
data 3dview (x-min x-max y-min y-max) ((color 'black) (width 1.0))
(begin
(send 3dview set-line-color color)
(set! width (max width 0.0))
(set! width (min width 1.0))
(draw-histogram-2d (car data)
(cadr data)
(caddr data)
width
3dview)))
(provide
histogram-2d)