Line of Sight Example 2 NetLogo Model
Produced for the book series "Artificial Intelligence";
Author: W. J. Teahan; Publisher: Ventus Publishing Aps, Denmark.
powered by NetLogo
view/download model file: Line-of-Sight-Example-2.nlogo
WHAT IS IT?
On a perfectly flat landscape, you can see all the way to the horizon. But if the landscape has hills, your view of some of the land in front of you may be blocked. This code example shows how to simulate this using turtles moving over a patch landscape of varying elevation.
HOW IT WORKS
Each patch has an elevation. Every patch that a turtle can "see" from its current location and elevation is considered to be in the turtle's "line of sight." To show this, a colored dot is placed on the patch. The patches visible to each turtle are determined by the turtle's heading, the MAXIMUM-VISIBILITY slider (how many patches ahead each turtle can see if it is unobstructed) and the elevations of the patches in between.
Each turtle looks ahead at the patches up to MAXIMUM-VISIBILITY away. If the angle from the location of the turtle to the top of the patch is greater than the angle to the last visible patch then there is a line from the turtle to the patch that does not intersect any of the other patches, thus it is in the "line of sight" of the turtle.
While all six turtles show their line of sight with dots, the orange turtle's line of sight is depicted with a plot, as well. The plot shows the elevation of the MAXIMUM-VISIBILITY patches in front of the turtle, with visible patches drawn as orange bars. (Thus if the three leftmost bars are drawn in orange, the elevations allow the orange turtle to see only three patches ahead.)
EXTENDING THE MODEL
Make each turtle able to see around it in all directions, not only the single direction in which it is facing.
PROCEDURES
; This code written by Uri Wilensky (see copyright message below) ; has been slightly modified by Bill Teahan for the following ; book: ; ; Teahan, W. J. (2009). Artificial Intelligence. Ventus Publishing Aps. ; breed [walkers walker] breed [markers marker] patches-own [elevation] to setup ca set-default-shape markers "dot" ;; setup the terrain ask patches [ set elevation random 10000 ] repeat 2 [ diffuse elevation 1 ] ask patches [ set pcolor scale-color green elevation 1000 9000 ] create-walkers 20 [ set size 1.5 setxy random-xcor random-ycor set color one-of [orange blue magenta violet brown yellow pink cyan sky turquoise red black] mark-line-of-sight ] ask walker 0 [ plot-line-of-sight ] end to go ;; get rid of all the old markers ask markers [ die ] ;; move the walkers ask walkers [ rt random 10 lt random 10 fd 1 mark-line-of-sight ] ;; plot the orange walker only ask walker 0 [ plot-line-of-sight ] tick end to mark-line-of-sight ;; walker procedure let dist 1 let a1 0 let c color let last-patch patch-here ;; iterate through all the patches ;; starting at the patch directly ahead ;; going through MAXIMUM-VISIBILITY while [dist <= maximum-visibility] [ let p patch-ahead dist ;; if we are looking diagonally across ;; a patch it is possible we'll get the ;; same patch for distance x and x + 1 ;; but we don't need to check again. if p != last-patch [ ;; find the angle between the turtle's position ;; and the top of the patch. let a2 atan dist (elevation - [elevation] of p) ;; if that angle is less than the angle toward the ;; last visible patch there is no direct line from the turtle ;; to the patch in question that is not obstructed by another ;; patch. if a1 < a2 [ ask p [ sprout-markers 1 [ set color c ] ] set a1 a2 ] set last-patch p ] set dist dist + 1 ] end to plot-line-of-sight ;; walker procedure plot-pen-reset set-plot-x-range 0 (maximum-visibility + 1) let dist 0 while [dist <= maximum-visibility] [ let p patch-ahead dist ifelse any? (turtles-on p) with [ color = [color] of myself ] [ set-plot-pen-color color ] [ set-plot-pen-color black ] plot-pen-up plotxy dist 0 plot-pen-down plotxy dist [elevation] of p set dist dist + 1 ] end ; *** NetLogo 4.0.4 Code Example Copyright Notice *** ; ; (C) 2006 Uri Wilensky. This code may be freely copied, distributed, ; altered, or otherwise used by anyone for any legal purpose. ; ; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ; OWNERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ; ; *** End of NetLogo 4.0.4 Code Example Copyright Notice ***