NetLogo banner

Home
Download
Help
Resources
Extensions
FAQ
NetLogo Publications
Contact Us
Donate

Models:
Library
Community
Modeling Commons

Beginners Interactive NetLogo Dictionary (BIND)
NetLogo Dictionary

User Manuals:
Web
Printable
Chinese
Czech
Farsi / Persian
Japanese
Spanish

  Donate

NetLogo Models Library:
Code Examples

(back to the library)

Intersecting Links Example

[screen shot]

If you download the NetLogo application, this model is included. You can also Try running it in NetLogo Web

WHAT IS IT?

This code example shows how to determine if two links intersect.

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 between the two nodes of the link, 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 link variables instead and compute them for all segments before checking for any intersections.

The math assumes that the links 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 Lines Example: same as this example, but for line turtles instead of links
  • 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.

(back to the NetLogo Models Library)