Point in polygon containment

Point in polygon containment preview image

1 collaborator

Eugene_martin Eugene Martin (Author)


(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by the author
Model was written in NetLogo 5.3.1 • Viewed 125 times • Downloaded 7 times • Run 0 times
Download the 'Point in polygon containment' modelDownload this modelEmbed this model

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

Comments and Questions

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

Click to Run Model

turtles-own [name my_to]
globals [reset Contained imonitor x3 y3 x4 y4 thisslope check_m check_b test_x test_y test_m test_b left_x left_y hlist Ix Iy radius ExtList wholist step pos colors slopes intercepts Ax Ay Bx By Cx Cy ABx ABy ABm ABb BCx BCy BCm BCb CAx CAy CAm CAb midx midy]

to doit
  if reset = 1 [ca clear-output set reset 0]
  if count turtles with [name = "vert"] != 0 and count turtles with [name = "test"] != 0 [contain stop set reset 1]
  if count turtles with [name = "vert"] != 0 and count turtles with [name = "test"] = 0 [testpoint stop]
  if count turtles with [name = "vert"] = 0 [makepoly stop]

to makepoly
  ;make color set
  set pos 0
  ;make points
  set hlist []
  repeat (10 + random 20) [
    set hlist lput random 360 hlist
  set hlist sort hlist
  foreach hlist [
     crt 1 [set shape "square" set size 0 set shape "dot" set color red set name "vert" set heading ? jump (3 + random 25 + (random 100) / 100)]
  set wholist []
  ask turtles[set wholist lput who wholist]
  set wholist sort wholist
  set pos -1
  repeat (length wholist - 1) [
   set pos pos + 1
   ask turtle (item pos wholist) [create-link-with turtle (item (pos + 1) wholist) set my_to item (pos + 1) wholist]]
  ask turtle (last wholist) [create-link-with turtle (first wholist) set my_to first wholist]
  ask links [set thickness 0.5 set color 5]

to testpoint
  crt 1 [set shape "star" set size 2 set color 97 set name "test" setxy mouse-xcor mouse-ycor]  ;set heading random 360 jump (random 25 + (random 100) / 100)]
  set test_x [xcor] of first sort turtles with [name = "test"]
  set test_y [ycor] of first sort turtles with [name = "test"]

to contain
  set left_x world-width / -2
  set left_y test_y
  crt 1 [set color black set name "left" setxy left_x left_y]
  ask turtle [who] of one-of turtles with [name = "test"][create-link-with one-of turtles with [name = "left"]]
  carefully[ask link ([who] of one-of turtles with [name = "test"]) ([who] of one-of turtles with [name = "left"]) [set thickness 0.5 set color white]][]

  set wholist []
  ask turtles with [name = "vert"][set wholist lput who wholist]
  foreach wholist [
    intersect_test ?
  ifelse (count turtles with [name = "i"] / 2) =  int (count turtles with [name = "i"] / 2) [show (se "INTERSECTIONS:" count turtles with [name = "i"] "    OUTSIDE    ")   set Contained "FALSE"][show (se "INTERSECTIONS:" count turtles with [name = "i"] "   INSIDE    ")   set Contained "TRUE"]
  set imonitor count turtles with [name = "i"]
  ask links [set thickness 0.2]
  ask links with [color = pink] [set thickness 0.5]
  set reset 1

to intersect_test [w]
  set test_m 0
  set test_b test_y
  set x3 [xcor] of turtle w
  set y3 [ycor] of turtle w
  set x4 [xcor] of turtle [my_to] of turtle w
  set y4 [ycor] of turtle [my_to] of turtle w
  set check_m slope x3 y3 x4 y4
  set check_b intercept x3 y3 check_m
  carefully [
    set Ix ((check_b - test_b) / (test_m - check_m))
    set Iy (check_m * Ix + check_b)
    if Ix < test_x and (abs Ix < world-width / 2) and (abs Iy < world-height / 2) and not (y3 < test_y and y4 < test_y) and not (y3 > test_y and y4 > test_y)
     crt 1 [setxy Ix Iy set shape "square" set name "i" set color blue set size 1]
     ask link w [my_to] of turtle w [set color pink]

to-report slope [x1 y1 x2 y2]
  carefully [set thisslope ((y1 - y2) / (x1 - x2))]
     [set thisslope 10000000000000]
  report thisslope

to-report intercept [x y m]
  report (y - (m * x))

There is only one version of this model, created about 3 years ago by Eugene Martin.

Attached files

File Type Description Last updated
Point in polygon containment.png preview Preview for 'Point in polygon containment' about 3 years ago, by Eugene Martin Download

This model does not have any ancestors.

This model does not have any descendants.