Beginners Interactive NetLogo Dictionary (BIND)
Farsi / Persian
NetLogo Models Library:
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.
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 slope-intercept form (y=mx+c); see http://mathworld.wolfram.com/Slope.html.
Thanks to Gagandeep Singh for his work on this example.