Intersecting Lines Example

Intersecting Lines Example preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

code example 

Tagged by Reuven M. Lerner almost 12 years ago

Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 772 times • Downloaded 117 times • Run 1 time
Download the 'Intersecting Lines 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?

This shows how to determine whether line segments cross, and if they do, where.

HOW IT WORKS

The work is done in the procedure called intersection. First the code computes the intersection point of the lines containing the segments, then checks to see if that intersection point is actually within both segments. (The second check is easy to do because we already know the intersection point is on the lines, so we only need to check that the point's x coordinate.) If two segments have the same slope, no intersections are marked.

Since slope is undefined for vertical lines, special case code is used if either of the segments is vertical.

THINGS TO NOTICE

The intersection procedure computes m and c for both segments every time it is called. This is wasteful if we are checking every turtle against every other turtle looking for intersections. To speed up the model, make m and c segment variables instead and compute them for all segments before checking for any intersections.

The math assumes that the turtles don't extend beyond the edges of the world. If you need to intersect turtles which go off the edges of the world, you'll need to add extra code.

RELATED MODELS

  • Code Examples -> Intersecting Links Example: same as this example, but for links instead of line turtles
  • Games -> Planarity: has shorter code for determining whether two line segments intersect, without bothering to compute the location of the intersection point

CREDITS AND REFERENCES

To keep the math relatively simple, the code represents lines in slope-intercept form (y=mx+c); see http://mathworld.wolfram.com/Slope.html.

Thanks to Gagandeep Singh for his work on this example.

Comments and Questions

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

Click to Run Model

breed [ segments segment ]
breed [ markers marker ]

to setup
  clear-all
  set-default-shape markers "circle"
  set-default-shape segments "line"
  create-segments 9 [
    setxy (random-xcor / 2) (random-ycor / 2)
    set size 10 + random 5
  ]
  place-markers
  reset-ticks
end 

to go
  ask segments [ rt 0.1 + who / 30 ]
  place-markers
  tick
end 

to place-markers
  ask markers [ die ]
  ;; each pair of segments checks for intersections once
  ask segments [
    ;; performing this check on the who numbers keeps
    ;; us from checking every pair twice
    ask segments with [self > myself] [
      let result intersection self myself
      if not empty? result [
        hatch-markers 1 [
          set color gray
          set size 1
          setxy (first result) (last result)
        ]
      ]
    ]
  ]
end 

;; reports a two-item list of x and y coordinates, or an empty
;; list if no intersection is found

to-report intersection [t1 t2]
  let m1 [tan (90 - heading)] of t1
  let m2 [tan (90 - heading)] of t2
  ;; treat parallel/collinear lines as non-intersecting
  if m1 = m2 [ report [] ]
  ;; is t1 vertical? if so, swap the two turtles
  if abs m1 = tan 90
  [
    ifelse abs m2 = tan 90
      [ report [] ]
      [ report intersection t2 t1 ]
  ]
  ;; is t2 vertical? if so, handle specially
  if abs m2 = tan 90 [
     ;; represent t1 line in slope-intercept form (y=mx+c)
      let c1 [ycor - xcor * m1] of t1
      ;; t2 is vertical so we know x already
      let x [xcor] of t2
      ;; solve for y
      let y m1 * x + c1
      ;; check if intersection point lies on both segments
      if not [x-within? x] of t1 [ report [] ]
      if not [y-within? y] of t2 [ report [] ]
      report list x y
  ]
  ;; now handle the normal case where neither turtle is vertical;
  ;; start by representing lines in slope-intercept form (y=mx+c)
  let c1 [ycor - xcor * m1] of t1
  let c2 [ycor - xcor * m2] of t2
  ;; now solve for x
  let x (c2 - c1) / (m1 - m2)
  ;; check if intersection point lies on both segments
  if not [x-within? x] of t1 [ report [] ]
  if not [x-within? x] of t2 [ report [] ]
  report list x (m1 * x + c1)
end 

to-report x-within? [x]  ;; turtle procedure
  report abs (xcor - x) <= abs (size / 2 * dx)
end 

to-report y-within? [y]  ;; turtle procedure
  report abs (ycor - y) <= abs (size / 2 * dy)
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 11 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky about 12 years ago Updated version tag Download this version
Uri Wilensky almost 13 years ago Updated to NetLogo 5.0 Download this version
Uri Wilensky over 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 14 years ago Model from NetLogo distribution Download this version
Uri Wilensky over 14 years ago Intersecting Lines Example Download this version
Uri Wilensky over 14 years ago Intersecting Lines Example Download this version

Attached files

File Type Description Last updated
Intersecting Lines Example.png preview Preview for 'Intersecting Lines Example' over 11 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.