DOWNLOAD drawing-program.zip containing the .nlogo file and a sample drawing.
Mostly, the drawing program is an adaptation of the Flygame program. We use almost the same method to paint with the mouse, only this time we always color the patch under the mouse cursor when the left mouse key is pressed (and not only if there is a fly there as in Flygame):
if mouse-down? [ ask patch mouse-xcor mouse-ycor [ set pcolor col ] ]
We also want to be able to choose the color (note: in a serious simulation program, you could use this to set objects into the environment and color could distinguish between certain types of objects - eg. one color could be used to denote food and another to denote an obstacle), so we put a chooser named "pencolor" into our interface. Choosers accept string and number values - these are displayed and also used as the variable value for the global variable associated with the choose. We can't expect users to remember color number values, so we display strings with color names instead. To actually use these as a color value in our program we need to do a little conversion from the string names to the color values. We do this by setting up 2 lists - namelist and colorlist in our setup procedure:
set namelist [ "black" "red" "orange" "yellow" "green" "blue" ... ] set colorlist [ black red orange yellow green blue ... ]
At first glance they might look the same, but note that namelist contains strings, while colorlist contains keywords that are actually number values for the colors. We then map from the strings value chosen in the chooser to the actual color number value like this in the go procedure:
let pos position pencolor namelist let col item pos colorlist
The first line gives us the position of the chosen string from the pencolor chooser in namelist (eg. 1 for "red") and the second line fetches the value at that position in the colorlist (eg. red, which is equal to 15 for pos=1) and saves that value in the col variable. The col variable is then used to set the pcolor of that patch as shown above.
When we have drawn to our heart's content, we might want to save our file, so we provide the following procedure:
to save-pic file-open filename let y max-pycor repeat world-height [ let x min-pxcor repeat world-width [ file-type [pcolor] of patch x y file-type " " set x (x + 1) ] set y (y - 1) ] file-close end
As we have seen in the Flygame example, file-open creates a new file if it does not yet exist and appends to the file if it does. Since this is just a simple example we do no real file-checking here; the user has to take care for themselves that they do not save to the same file twice. The interesting thing here are the two repeat loops: we use them to cycle over all x/y coordinates to save the pcolor of all patches with file-type (which has nothing to do with the type of the file, but rather gets its name from typing like a typewriter). We separate the color values (which are simply numbers; you can use a text editor to look at the file) with spaces. They reporters world-height and world-width give us the total width and height of our area and we write starting from the top left towards the bottom right. This is the reason why we start at max-pycor (because the top is the highest y coordinate) but with min-pxcor (because left is the lowest x coordinate).
To load a drawing, I adapted the load-pic procedure from the Sugarscape model we previously used:
to load-pic let c 0 file-open filename foreach sort patches [ ask ? [ set c file-read set pcolor c ] ] file-close end
We open the file and then cycle through all patches and set their color. For this we use the sort command so we get the patches in order instead of randomly. We then ask each patch (denoted by the "?") to set its color to the color value read with file-read from our file. The file-read function expects values to be separated by spaces in the file - just like we saved them.
Instead of saving and loading the colors of patches you could just as well use this kind of technique to save and load attribute values of turtles so you could stop and later resume a long simulation or to use the data in another program (eg. gnuplot) for visualization. Or you could use it like the Sugarscape model does, to set up the initial environment from a file. Since they are simple text files you can always use a text editor to prepare them; but they could just as well be created by some other simulation - even by some other simulation software if necessary.

Keine Kommentare:
Kommentar veröffentlichen