To count crossings in a graph drawing, first decide when two drawn edges intersect. For straight-line edges, the relevant event is a meeting away from a permitted common endpoint. A shared endpoint alone is not a crossing. This distinction lets an embedding count edge intersections without counting ordinary connections between edges. section 15.4.1 section 16.1.1
For two straight-line segments, test intersection in stages:
- Optionally compare their axis-aligned bounding boxes. If their projections are disjoint on either axis, the segments cannot intersect; overlapping boxes do not prove that they do. section 15.4.1
- Find where the lines through the segments meet. Express that point using a scaling factor for each segment; it lies on both segments only when both factors are between 0 and 1, inclusive. section 15.4.1
- Exclude a meeting where both factors identify endpoints. The displayed scaling-factor calculation divides by a vector dot product, so an implementation must handle a zero denominator separately rather than divide by it. section 15.4.1
The diagram separates a meeting of the extended lines from a meeting that falls within both finite segments. section 15.4.1
An orthogonal polyline edge needs a different count: compare its constituent horizontal and vertical segments using their endpoint coordinates. Two such edges can meet more than once, unlike the single-intersection cases considered for straight-line edges. section 15.4.2
Counting crossings in a candidate drawing
Place each vertex at a distinct point in a rectangle, reject a placement if a vertex lies on a nonincident edge, then test each unordered pair of edges and increment a crossing count when the edge-intersection test succeeds. Repeating this for random valid placements lets you retain the drawing with the lowest count. With m edges, the pair scan makes O(m²) intersection tests per valid placement. section 16.1.1
Crossing count is also a cost function, but it changes abruptly as vertices move. In a four-vertex complete-graph example, moving one vertex horizontally changes the cost from 1 to 0 when it enters a triangular region; positions that put a vertex on another edge are invalid. Thus counting crossings is straightforward, while using the count to guide an optimization algorithm can be problematic. section 16.2.1




