Nested Triangles NetLogo Model
Produced for the book series "Exercises for Artificial Intelligence";
Author: W. J. Teahan; Publisher: Ventus Publishing Aps, Denmark.
powered by NetLogo
view/download model file: Nested-Triangles.nlogo
WHAT IS IT?
This model illustrates solutions to Exercises 4.2.3, 4.2.4 and 4.2.5 for the Exercises for Artificial Intelligence book series. It also shows what you can do with a single turtle agent wandering around drawing things.
WHAT IS ITS PURPOSE?
Its purpose is to show how turtle agents in NetLogo are programmed, and to illustrate the use of the turtle drawing commands. A secondary purpose is to illustrate how patterns can emerge through the execution of relatively straightforward commands. (If further interested, the reader should research the topics of L-systems and fractals).
HOW IT WORKS
The model uses a turtle agent to draw a set of nested equilateral triangles. The outer triangle is drawn first, then the pen is pulled up while the turtle jumps to the next inner triangle using a jump angle and length specified by slider values. The length-modulus is a ratio that is used to reduce the size of each successive triangle. The inner-iterations slider is used to specify the number of triangles drawn.
The value of the outer-iterations slider controls the number of times the set of triangles are redrawn. Once all the triangles in one particular set are drawn, then the turtle changes it heading by adding the init-heading-change slider value to its current heading. For example, if this value is 30, and the value of the outer-iterations slider is set to 12, then the pattern will cover the full 360 degrees (if the turtle returns to its origin on its same heading once each set of triangles is drawn).
HOW TO USE IT
Press the setup button first, then press either the draw-pattern-1 or draw-pattern-2 button.
The draw-pattern-1 button can be used to produce the patterns for Exercises 4.2.3 and 4.2.4. (For the values of the switches and sliders used to produce the pattern in Exercise 4.2.3, see Figure S-4.2.3-1 at the end of the Exercises for Artificial Intelligence - Agents and Environments book, but change the value of the inner-iterations slider to 20. For Exercise 4.2.4, use the same switch and slider values, but change the inner-change-colour? switch to On, and change the jump-angle-change-2 slider value to 15.)
The draw-pattern-2 button can be used to produce the pattern shown for Exercise 4.2.5 and also shown in Figure S-4.2.3-1. (For the former, use the same values as shown for the swtches and sliders in Figure S-4.2.3-1 but set the outer-change-colour? switch to Off).
The settings for two further patterns are described in Figure S-4.2.3-2.
THE INTERFACE
The model's Interface buttons are defined as follows:
- setup: This will clear the environment, reset all variables and set all patches to white.
- draw-pattern-1: This draws a single set of (usually nested) triangles. The number of triangles drawn is determined by the value of the inner-iterations slider.
- draw-pattern-2: This draws a multiple set of (usually nested) triangles. The number of sets drawn is determined by the value of the outer-iterations slider.
The modelÕs Interface switches and sliders are defined as follows:
- inner-change-colour?: This switch if set to On will change the colours of the triangles as they are drawn successively.
- outer-change-colour?: This switch if set to On will change the drawing colour at the start of a new set of triangles.
- inner-iterations: This is the number of (usually nested) triangles that are drawn in each set of triangles.
- outer-iterations: This determines the number of sets that are drawn.
- colour-of-sides: This is the initial colour that is used to draw the sides of each triangle.
- initial-length: This is the initial length of the sides of the outer triangle in each set.
- length-modulus: This ratio controls how much the length of the sides of the triangle is reduced or enlarged each time a new triangle is drawn.
- line-thickness: This is the thickness of the lines that are drawn by the turtle (i.e. this is the width of the sides of the triangles).
- jump-angle-change-1: This specifies the change of direction in degrees when the turtle jumps across to draw the next triangle.
- jump-length: This specifies the distance the turtle moves (with its pen up temporarily) when it jumps across to start drawing the next triangle.
- jump-angle-change-2: This specifies the value of a further directional change the turtle makes after each jump.
- init-heading-change: This is the change of direction in degrees made by the turtle when it has completed drawing a set of triangles. This is done before the start of drawing the next set of triangles.
THINGS TO NOTICE
Notice the infinite variety of patterns that can be created using a set of simple commands. Notice also that although the sets of triangles are drawn locally (i.e. using a turtle executing a small set of commands from a first-person perspective based on its current location), global patterns emerge from the interactions between the different sets.
THINGS TO TRY
Try changing the values of the switches and sliders to create further patterns.
Try changing the values of the sliders dynamically as the draw-pattern-1 and draw-pattern-2 buttons are still pressed. For example, change the jump angles and length, or change the colour.
EXTENDING THE MODEL
Change the model so that other shapes can be drawn - for example, isosceles triangles, squares, rectangles, hexagons etc.
Change the model so that the user can specify: 1) the set of commands to be executed when the object (triangle, square, hexagon etc.) is drawn; 2) the set of commands to be executed when the jump is made; and 3) the set of commands to be executed when the drawing of a set of objects is completed before the start of the drawing of the next set of objects. This will give the user greater control over how the patterns are drawn, and greatly increase the variety of patterns that are possible.
NETLOGO FEATURES
The model demonstrates the use of turtle drawing commands.
RELATED MODELS
See the Nested Squares model.
CREDITS AND REFERENCES
To refer to this model in publications, please use:
Teahan, W. J. (2010). Nested Triangles NetLogo model.
Exercises for Artificial Intelligence. Ventus Publishing Aps.
PROCEDURES
; Nested Triangles model. ; ; ; Copyright 2010 William John Teahan. All Rights Reserved. ; to setup ca ;; clear everything ask patches [ set pcolor white ] ;; make background full of white patches end to draw-triangles [initial-colour initial-x initial-y initial-heading] let this-turtle nobody let this-length initial-length let inner-colour initial-colour create-turtles 1 [ set this-turtle self set color inner-colour set size line-thickness + 4 setxy initial-x initial-y set heading initial-heading pen-down set pen-size line-thickness ] ask this-turtle [ repeat inner-iterations [ draw-triangle this-turtle inner-colour this-length pen-up right jump-angle-change-1 forward jump-length right jump-angle-change-2 - jump-angle-change-1 pen-down set this-length this-length * length-modulus if (inner-change-colour?) [ set inner-colour inner-colour + 10 ] ] ] ask this-turtle [ die ] ; the turtle has done its work, so kill it off end to draw-triangle [this-turtle this-colour this-length] ; Gets the turtle this-turtle to draw an equilateral triangle of length this-length ask this-turtle [ set color this-colour pen-down forward this-length right 120 forward this-length right 120 forward this-length right 120 ] end to draw-pattern-1 draw-triangles colour-of-sides 0 0 -30 end to draw-pattern-2 let init-heading -30 let outer-colour colour-of-sides repeat outer-iterations [ draw-triangles outer-colour 0 0 init-heading set init-heading init-heading + init-heading-change if (outer-change-colour?) [ set outer-colour outer-colour + 10 ] ] end ; ; Copyright 2010 by William John Teahan. All rights reserved. ; ; Permission to use, modify or redistribute this model is hereby granted, ; provided that both of the following requirements are followed: ; a) this copyright notice is included. ; b) this model will not be redistributed for profit without permission ; from William John Teahan. ; Contact William John Teahan for appropriate licenses for redistribution for ; profit. ; ; To refer to this model in publications, please use: ; ; Teahan, W. J. (2010). Nested Triangles NetLogo model. ; Artificial Intelligence. Ventus Publishing Aps ;