|
|||
Home Page Download Models Community Models Extensions User Manual:![]() Web version![]() Printable version FAQ Resources Contact Us |
NetLogo Models Library: | ||
|
Run Intersecting Lines Example in your browser uses NetLogo 4.0.2 requires Java 1.4.1+ (system requirements) Note: If you download the NetLogo application, every model in the Models Library (besides the Community Models) is included. If you have trouble running this model in your browser, you may wish to download the application instead. |
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.
(back to the NetLogo Models Library)