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

NetLogo Models Library: 
If you download the NetLogo application, this model is included. You can also Try running it in NetLogo Web 
This shows how to determine whether line segments cross, and if they do, where.
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.
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.
To keep the math relatively simple, the code represents lines in slopeintercept 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)