Line of Sight Example

Line of Sight Example preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

code example 

Tagged by Reuven M. Lerner over 12 years ago

Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 797 times • Downloaded 146 times • Run 0 times
Download the 'Line of Sight Example' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


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.

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

breed [walkers walker]
breed [markers marker]
patches-own [elevation]

to setup
  clear-all
  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 6 [
    set size 1.5
    setxy random-xcor random-ycor
    set color item who [orange blue magenta violet brown yellow]
    mark-line-of-sight
  ]
  reset-ticks
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
  ]
  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 


; Public Domain:
; To the extent possible under law, Uri Wilensky has waived all
; copyright and related or neighboring rights to this model.

There are 10 versions of this model.

Uploaded by When Description Download
Uri Wilensky over 12 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky almost 13 years ago Updated version tag Download this version
Uri Wilensky over 13 years ago Updated to NetLogo 5.0 Download this version
Uri Wilensky about 15 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky about 15 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky about 15 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky about 15 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky about 15 years ago Model from NetLogo distribution Download this version
Uri Wilensky about 15 years ago Line of Sight Example Download this version
Uri Wilensky about 15 years ago Line of Sight Example Download this version

Attached files

File Type Description Last updated
Line of Sight Example.png preview Preview for 'Line of Sight Example' over 12 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.