Pinstripe/Display

From Rosetta Code
Revision as of 14:46, 12 July 2011 by rosettacode>Markhobley ({{omit from|GUISS}})
Task
Pinstripe/Display
You are encouraged to solve this task according to the task description, using any language you may know.

The task is to demonstrate the creation of a series of 1 pixel wide vertical pinstripes across the entire width of the display. The pinstripes should alternate one pixel white, one pixel black.

Quarter of the way down the display, we can switch to a wider 2 pixel wide vertical pinstripe pattern, alternating two pixels white, two pixels black. Half way down the display, we switch to 3 pixels wide, and for the lower quarter of the display we use 4 pixels.

Icon and Unicon

Icon and Unicon can't access the screen directly, so this pinstripe is produced in a maximal sized window. The maximal dimensions have been empirically reduced to keep the boundary on the screen. <lang Icon>link graphics

procedure main() # pinstripe

  WOpen("canvas=hidden")                   # hide for query
  height := WAttrib("displayheight") - 45  # adjust for ...
  width  := WAttrib("displaywidth") - 20   # ... window 7 borders
  WClose(&window)
  W := WOpen("size="||width||","||height,"bg=black","fg=white") | 
       stop("Unable to open window")   
  maxbands := 4                             # bands to draw
  bandheight := height / maxbands           # height of each band
  every bands := 1 to maxbands do {         # for each band
        top   := 1 + bandheight * (bands-1) # .. top of band
        step  := 2^bands                    # .. number of steps (width)
        lines := step / 2                   # .. number (width) of stripes
        every c := 1 to width by step & l := 0 to lines-1 do 
           DrawLine(c+l,top,c+l,top+bandheight-1)
        }
  WDone(W)                                  # q to exit

end</lang>

graphics.icn supports graphics

J

<lang j> load'viewmat'

  size=.2{.".wd'qm' NB. J6
  size=. getscreenwh_jgtk_  NB. J7
  'rgb'viewmat- (4<.@%~{:size)# ({.size) $&> 1 2 3 4#&.> <0 1</lang>

Locomotive Basic

<lang basic>10 MODE 2: REM finest resolution 20 LET sh=400: REM screen height 30 LET sw=640: REM screen width 40 INK 0,27: REM white ink for background pen (0) 50 INK 1,0: REM black ink for forground pen (1) 60 FOR sn=1 TO 4: REM four sections 70 LET bh=INT (sh/4): REM bar height 80 LET bb=(4-sn)*bh: REM bar baseline 90 LET dw=0: REM currently drawn bar width 100 LET dc=0: REM current drawing colour 110 FOR l=0 TO sw -1: REM pan width for each section 120 PLOT l,bb,dc 130 DRAWR 0,bh-1,dc: REM subtract 1 pixel (already plotted) 140 LET dw=dw+1 150 REM section number corresponds to maximum bar width 160 REM change bar colour, if maximum bar width exceeded 170 IF dw>sn THEN dw=0:dc=dc+1: REM next colour 180 IF dc>1 THEN dc=0 190 NEXT l 200 NEXT sn</lang>

PureBasic

<lang PureBasic>#White = $FFFFFF ;color

Create a Pinstripe image

Procedure PinstripeDisplay(width, height)

 Protected x, imgID, psHeight = height / 4, psWidth = 1, psTop, horzBand
 imgID = CreateImage(#PB_Any, width, height)
 If imgID
   StartDrawing(ImageOutput(imgID))
     Repeat 
       x = 0
       Repeat
         Box(x, psTop, psWidth, psHeight, #White)
         x + 2 * psWidth
       Until x >= width
       psWidth + 1
       horzBand + 1
       psTop = horzBand * height / 4  ;move to the top of next horizontal band of image
     Until psTop >= height 
   StopDrawing()
 EndIf
 ProcedureReturn imgID

EndProcedure

Open a window and display the pinstripe

If OpenWindow(0, 0, 0, 1, 1,"PureBasic Pinstripe", #PB_Window_Maximize | #PB_Window_SystemMenu)

 PicID = PinstripeDisplay(WindowWidth(0), WindowHeight(0))
 ImageGadget(0, 0, 0, WindowWidth(0), WindowHeight(0), ImageID(PicID))
 While WaitWindowEvent() <> #PB_Event_CloseWindow
 Wend  

EndIf</lang>

Tcl

Library: Tk

<lang tcl>package require Tcl 8.5 package require Tk 8.5

wm attributes . -fullscreen 1 pack [canvas .c -highlightthick 0] -fill both -expand 1 set colors {black white}

set dy [expr {[winfo screenheight .c]/4}] set y 0 foreach dx {1 2 3 4} {

   for {set x 0} {$x < [winfo screenwidth .c]} {incr x $dx} {

.c create rectangle $x $y [expr {$x+$dx}] [expr {$y+$dy}] \

           -fill [lindex $colors 0] -outline {}

set colors [list {*}[lrange $colors 1 end] [lindex $colors 0]]

   }
   incr y $dy

}</lang>