Draw a cuboid

From Rosetta Code
Revision as of 00:41, 8 March 2011 by rosettacode>Markhobley (Wiki syntax fixes)
Draw a cuboid is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The task is to draw a cuboid with relative dimensions of 2x3x4.

Brlcad

<lang brlcad> opendb cuboid.g y units cm in cuboid.s 0 2 0 3 0 4 </lang>

Openscad

Drawing a cuboid is easy in openscad:

<lang openscad>// This will produce a simple cuboid cube([2,3,4])</lang>

PicoLisp

Using ASCII

<lang PicoLisp>(de cuboid (DX DY DZ)

  (cubLine (inc DY) "+" DX "-" 0)
  (for I DY
     (cubLine (- DY I -1) "/" DX " " (dec I) "|") )
  (cubLine 0 "+" DX "-" DY "|")
  (do (- (* 4 DZ) DY 2)
     (cubLine 0 "|" DX " " DY "|") )
  (cubLine 0 "|" DX " " DY "+")
  (for I DY
     (cubLine 0 "|" DX " " (- DY I) "/") )
  (cubLine 0 "+" DX "-" 0) )

(de cubLine (N C DX D DY E)

  (space N)
  (prin C)
  (do (dec (* 9 DX)) (prin D))
  (prin C)
  (space DY)
  (prinl E) )</lang>

Output:

: (cuboid 2 3 4)
    +-----------------+
   /                 /|
  /                 / |
 /                 /  |
+-----------------+   |
|                 |   |
|                 |   |
|                 |   |
|                 |   |
|                 |   |
|                 |   |
|                 |   |
|                 |   |
|                 |   |
|                 |   |
|                 |   |
|                 |   +
|                 |  /
|                 | /
|                 |/
+-----------------+

: (cuboid 1 1 1)
  +--------+
 /        /|
+--------+ |
|        | |
|        | +
|        |/
+--------+

: (cuboid 6 2 1)
   +-----------------------------------------------------+
  /                                                     /|
 /                                                     / |
+-----------------------------------------------------+  |
|                                                     |  +
|                                                     | /
|                                                     |/
+-----------------------------------------------------+

Using OpenGL

<lang PicoLisp>(load "@lib/openGl.l")

(setq *AngleX -26.0 *AngleY 74.0) (setq *LastX 0 *LastY 0)

(glutInit) (glutInitDisplayMode (| GLUT_RGBA GLUT_DOUBLE GLUT_DEPTH)) (glutInitWindowSize 512 512) (glutInitWindowPosition 10 50) (glutCreateWindow "PicoLisp Cube")

(glClearColor 1.0 1.0 1.0 1.0) # The background color (glEnable GL_DEPTH_TEST) (glEnable GL_LIGHTING) (glEnable GL_LIGHT0) (glDisable GL_CULL_FACE)

(glEnable GL_BLEND) (glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA) (glEnable GL_LINE_SMOOTH) (glHint GL_LINE_SMOOTH_HINT GL_NICEST) (glLineWidth 2.0)

(mouseFunc

  '((Btn State X Y)
     (setq *LastX X  *LastY Y) ) )

(motionFunc

  '((X Y)
     (inc '*AngleX (* (- Y *LastY) 1.0))
     (inc '*AngleY (* (- X *LastX) 1.0))
     (setq *LastX X  *LastY Y)
     (glutPostRedisplay) ) )

(reshapeFunc

  '((Width Height)
     (glMatrixMode GL_PROJECTION)
     (glLoadIdentity)
     (gluPerspective 45.0 (*/ Width 1.0 Height) 1.0 10.0)
     (glMatrixMode GL_MODELVIEW)
     (glViewport 0 0 Width Height) ) )

(displayPrg (glClear (| GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)) (glLoadIdentity) (glTranslatef 0.0 0.0 -3.0) (glRotatef *AngleX 1 0 0) (glRotatef *AngleY 0 1 0) (glutSolidCube 1.0)

(glDisable GL_LIGHTING) (glColor4f 0.4 0.4 0.4 1.0) (glutWireCube 1.002) (glEnable GL_LIGHTING)

(glFlush) (glutSwapBuffers) )

(glutMainLoop)</lang>

PureBasic

Using generic PureBasic 2D-library. <lang PureBasic>Procedure Draw_a_Cuboid(Window, X,Y,Z)

 w=WindowWidth(Window)
 h=WindowHeight(Window)
 diag.f=1.9
 If Not (w And h): ProcedureReturn: EndIf
 xscale.f = w/(x+z/diag)*0.98
 yscale.f = h/(y+z/diag)*0.98
 If xscale<yscale
   Scale.f = xscale
 Else
   Scale = yscale
 EndIf
 x*Scale: Y*Scale: Z*Scale
 CreateImage(0,w,h)
 If StartDrawing(ImageOutput(0))
   c= RGB(250, 40, 5)
   
   ;- Calculate the cones in the Cuboid
   xk = w/50     : yk = h/50
   x0 = Z/2 + xk : y0 = yk
   x1 = x0 + X   : y1 = y0
   x2 = xk       : y2 = y0 + Z/2
   x3 = x2 + X   : y3 = y2
   x4 = x2       : y4 = y2 + Y
   x5 = x4 + X   : y5 = y4
   x6 = x5 + Z/2 : y6 = y5 - Z/2
   
   ;- Draw it
   LineXY(x0,y0,x1,y1,c)
   LineXY(x0,y0,x2,y2,c)
   LineXY(x2,y2,x3,y3,c)
   LineXY(x1,y1,x3,y3,c)
   LineXY(x2,y2,x4,y4,c)
   LineXY(x4,y4,x5,y5,c)
   LineXY(x5,y5,x4,y4,c)
   LineXY(x5,y5,x6,y6,c)
   LineXY(x5,y5,x3,y3,c)
   LineXY(x6,y6,x1,y1,c)
   
   ;- Fill the areas
   FillArea(x,y,-1,RGB(255, 0, 0))
   FillArea(x,y-z/2,-1,RGB(0, 0, 255))
   FillArea(x+z/2,y,-1,RGB(0, 255, 0))
   StopDrawing()
 EndIf
 ;- Update the graphic
 ImageGadget(0,0,0,w,h,ImageID(0))

EndProcedure

  1. WFlags = #PB_Window_SystemMenu|#PB_Window_SizeGadget
  2. title = "PureBasic Cuboid"

MyWin = OpenWindow(#PB_Any, 0, 0, 200, 250, #title, #WFlags)

Repeat

 WEvent = WaitWindowEvent()
 If WEvent = #PB_Event_SizeWindow
   Draw_a_Cuboid(MyWin, 2, 3, 4)
 EndIf

Until WEvent = #PB_Event_CloseWindow

- Save the image?

UsePNGImageEncoder() respons = MessageRequester("Question","Save the image?",#PB_MessageRequester_YesNo) If respons=#PB_MessageRequester_Yes

 SaveImage(0, SaveFileRequester("","","",0),#PB_ImagePlugin_PNG,9)

EndIf</lang>