Task

Draw a   cuboid   with relative dimensions of    2 × 3 × 4. 



The cuboid can be represented graphically, or in   ASCII art,   depending on the language capabilities.

To fulfill the criteria of being a cuboid, three faces must be visible.

Either static or rotational projection is acceptable for this task.



Related tasks
 draw a sphere
 draw a rotating cube
 write language name in 3D ASCII
 draw a Deathstar


#lang racket/gui
(require sgl/gl)
 
; Macro to delimit and automatically end glBegin - glEnd contexts.
(define-syntax-rule (gl-begin-end Vertex-Mode statement ...)
  (let () (glBegin Vertex-Mode) statement ... (glEnd)))
 
(define (resize w h)
  (glViewport 0 0 w h))
 
(define (draw-opengl x y z)
  (glClearColor 0.0 0.0 0.0 0.0)
  (glEnable GL_DEPTH_TEST)
  (glClear GL_COLOR_BUFFER_BIT)
  (glClear GL_DEPTH_BUFFER_BIT)
 
  (define max-axis (add1 (max x y z)))
 
  (glMatrixMode GL_PROJECTION)
  (glLoadIdentity)
  (glOrtho (/ (- max-axis) 2) max-axis (/ (- max-axis) 2) max-axis (/ (- max-axis) 2) max-axis)
  (glMatrixMode GL_MODELVIEW)
  (glLoadIdentity)
  (glRotatef -45 1.0 0.0 0.0)
  (glRotatef 45 0.0 1.0 0.0)
 
  (gl-begin-end GL_QUADS
                (glColor3f 0 0 1)
                (glVertex3d x 0.0 z)
                (glVertex3d x y z)
                (glVertex3d x y 0.0)
                (glVertex3d x 0.0 0.0))
  (gl-begin-end GL_QUADS
                (glColor3f 1 0 0)
                (glVertex3d x 0.0 0.0)
                (glVertex3d x y 0.0)
                (glVertex3d 0.0 y 0.0)
                (glVertex3d 0.0 0.0 0.0)) 
  (gl-begin-end GL_QUADS
                (glColor3f 0 1 0)
                (glVertex3d x y 0.0)
                (glVertex3d x y z)
                (glVertex3d 0.0 y z)
                (glVertex3d 0.0 y 0.0)))
 
(define my-canvas%
  (class* canvas% ()
    (inherit with-gl-context swap-gl-buffers)
    (init-field (x 2) (y 3) (z 4))
 
    (define/override (on-paint)
      (with-gl-context
        (lambda ()
          (draw-opengl x y z)
          (swap-gl-buffers))))
 
    (define/override (on-size width height)
      (with-gl-context
        (lambda ()
          (resize width height))))
 
    (super-instantiate () (style '(gl)))))
 
(define win (new frame% (label "Racket Draw a cuboid") (min-width 300) (min-height 300)))
(define gl  (new my-canvas% (parent win) (x 2) (y 3) (z 4)))
 
(send win show #t)