; GridToolkit_10 - Library of Procedures For Drawing a Grid ; -------------- ; ; In lesson 10 (Words and Sentences) we added: ; ; - commands which address a cell by row and column numbers: ; gridGotoRowColCell :rowCol ; gridGotoRowColCellCtr :rowCol ; gridRowColCellFill :rowCol :color ; ; - commands which label a cell (put a character in a cell) ; gridCellLabel :idx :ch :color ; gridRowColCellLabel :rowCol :ch :color ; Symbolic Constants ; -------- --------- ; SETPENCOLOR values to black output 0 end to white output 7 end ; color for a cell's background to gridCellColor output white end ; color of grid's frame to gridFrameColor output black end ; size of the sides of a grid cell to gridCellSize output 40 end ; one-half the size of a side of a grid cell to gridHafCellSiz output quotient gridCellSize 2 end ; number of columns in the grid to gridNumCol output 8 end ; number of rows in the grid to gridNumRow output 5 end ; number of cells in the grid to gridNumCells output product gridNumCol gridNumRow end ; height of grid in turtle steps to gridHeight output product gridNumRow gridCellSize end ; width of grid in turtle steps to gridWidth output product gridNumCol gridCellSize end ; X coordinate for left side of the grid to gridLeftX output minus product gridCellSize (quotient gridNumCol 2) end ; X coordinate for right side of the grid to gridRightX output difference (sum gridLeftX gridWidth) 1 end ; Y coordinate for bottom of the grid to gridBottomY output minus product gridCellSize (quotient gridNumRow 2) end ; Y coordinate for top of the grid to gridTopY output difference (sum gridBottomY gridHeight) 1 end ; Grid Procedures ; ---- ---------- ; move the turtle to the top-left corner of the grid to gridGotoTopLeft penup setxy gridLeftX gridTopY end ; move the turtle to the bottom-left corner of the cell ; specified by the index :idx ; indices start at zero for the top-left cell and increase ; left-to-right, top-to-bottom to gridGotoCell :idx gridGotoTopLeft setheading 180 forward product gridCellSize (int quotient :idx gridNumCol) forward gridCellSize setheading 90 forward product gridCellSize (remainder :idx gridNumCol) end ; move the turtle to the bottom-left corner of the cell ; specified by the two-number sentence input :rowCol ; row numbers start at zero and increase to gridNumRow-1 ; column numbers start at zero and increase to gridNumCol-1 to gridGotoRowColCell :rowCol gridGotoTopLeft setheading 180 forward product gridCellSize (first :rowCol) forward gridCellSize setheading 90 forward product gridCellSize (last :rowCol) end ; with the turtle at the bottom-left corner of ; a cell, move it to the center of the cell to gotoCellCtrHelper setheading 90 forward gridHafCellSiz left 90 forward gridHafCellSiz end ; move the turtle to the center of cell specified by the index :idx ; indices start at zero for the top-left cell and increase ; left-to-right, top-to-bottom to gridGotoCellCtr :idx gridGotoCell :idx gotoCellCtrHelper end ; move the turtle to the center of the cell specified by the ; two number sentence input :rowCol ; row numbers start at zero and increase to gridNumRow-1 ; column numbers start at zero and increase to gridNumCol-1 to gridGotoRowColCellCtr :rowCol gridGotoRowColCell :rowCol gotoCellCtrHelper end ; with the turtle at the center of a cell, ; repaint the cell with a specified background color to fillCellHelper :color setpensize gridCellSize setpencolor :color setheading 0 forward gridHafCellSiz pendown back gridCellSize left 90 penup forward gridHafCellSiz setpensize 1 setpencolor gridFrameColor setheading 0 pendown repeat 4 [forward gridCellSize right 90] end ; draw/redraw the cell specified by the index :idx ; indices start at zero for the top-left cell and increase ; left-to-right, top-to-bottom ; the :color input specifies the cell's background color to gridCellFill :idx :color gridGotoCellCtr :idx fillCellHelper :color end ; draw/redraw the cell specified by the two number sentence input :rowCol ; row numbers start at zero and increase to gridNumRow-1 ; column numbers start at zero and increase to gridNumCol-1 ; the :color input specifies the cell's background color to gridRowColCellFill :rowCol :color gridGotoRowColCellCtr :rowCol fillCellHelper :color end ; with the turtle at the bottom-left corner of a cell, label ; the cell with a specified character in a specified color to labelCellHelper :ch :color setheading 0 setlabelheight difference gridCellSize 8 forward 4 right 90 forward quotient (difference gridCellSize (labelwidth :ch)) 2 setpencolor :color label :ch end ; label the cell specified by the index :idx ; indices start at zero for the top-left cell and increase ; left-to-right, top-to-bottom ; the :ch input, the label, is a single character word ; the :color input specifies the character's color to gridCellLabel :idx :ch :color gridGotoCell :idx labelCellHelper :ch :color end ; label the cell specified by by the two number sentence input :rowCol ; row numbers start at zero and increase to gridNumRow-1 ; column numbers start at zero and increase to gridNumCol-1 ; the :ch input, the label, is a single character word ; the :color input specifies the character's color to gridRowColCellLabel :rowCol :ch :color gridGotoRowColCell :rowCol labelCellHelper :ch :color end ;draw the grid to gridPaint gridGotoTopLeft setheading 180 forward quotient gridHeight 2 setheading 90 setpensize gridHeight setpencolor gridCellColor pendown forward gridWidth gridGotoTopLeft setpensize 1 setpencolor gridFrameColor setheading 180 pendown repeat gridNumRow [fd gridCellSize lt 90 fd gridWidth bk gridWidth rt 90] gridGotoTopLeft setheading 90 pendown repeat gridNumCol [fd gridCellSize rt 90 fd gridHeight bk gridHeight lt 90] end