  Home Download Help Resources Extensions FAQ NetLogo Publications Contact Us Donate Models: Library Community Modeling Commons User Manuals: Web Printable Chinese Czech Farsi / Persian Japanese Spanish # Intersecting Lines Example If you download the NetLogo application, this model is included. You can also Try running it in NetLogo Web

## 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.