Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CountIntersections ¶
CountIntersections implements the Bentley-Ottmann algorithm to find the total number of intersection points in a given set of line segments.
The algorithm uses a sweep-line approach, processing events (segment endpoints and intersections) from left to right. It maintains a status structure (a Red-Black Tree) of segments that cross the sweep line, ordered vertically. This allows it to efficiently find new intersections only between adjacent segments.
The time complexity is O((n+k) log n), where n is the number of segments and k is the number of intersections. The space complexity is O(n+k).
It correctly handles complex cases, including vertical segments and multiple segments intersecting at a single point.
Types ¶
type Event ¶
type Event struct {
// Point is the coordinate in the 2D plane where the event occurs.
Point Point
// Type is the nature of the event (e.g., SegmentStart, Intersection).
Type EventType
// Seg1 is the primary segment associated with this event.
Seg1 *Segment
// Seg2 is the second segment, used only for Intersection events.
Seg2 *Segment
}
Event represents a significant point in the 2D plane to be processed by the Bentley-Ottmann algorithm. Each event has a location (Point), a Type, and one or more associated Segments.
type EventQueue ¶
type EventQueue []*Event
EventQueue is a min-priority queue of events, implemented using Go's container/heap. Events are ordered primarily by their X-coordinate, then by their Y-coordinate as a tie-breaker. This ensures the sweep-line processes points from left-to-right, bottom-to-top.
func (EventQueue) Len ¶
func (eq EventQueue) Len() int
Len returns the number of events in the queue.
func (EventQueue) Less ¶
func (eq EventQueue) Less(i, j int) bool
Less reports whether the event at index i should be sorted before the event at index j.
func (*EventQueue) Pop ¶
func (eq *EventQueue) Pop() any
Pop removes and returns the event with the lowest value (highest priority) from the queue.
func (*EventQueue) Push ¶
func (eq *EventQueue) Push(x any)
Push adds an event to the priority queue.
func (EventQueue) Swap ¶
func (eq EventQueue) Swap(i, j int)
Swap swaps the events at indices i and j.
type EventType ¶
type EventType int
EventType defines the nature of an event in the sweep-line algorithm.
const ( // SegmentStart signifies that the sweep line has reached the left endpoint of a segment. SegmentStart EventType = iota // SegmentEnd signifies that the sweep line has reached the right endpoint of a segment. SegmentEnd // Intersection signifies that the sweep line has reached a point where two or more segments cross. Intersection )
type Point ¶
type Point struct {
X, Y float64
}
Point represents a point in a 2D Cartesian coordinate system.
type Segment ¶
type Segment struct {
P1, P2 Point
// contains filtered or unexported fields
}
Segment represents a line segment in 2D space, defined by two endpoints, P1 and P2.
type Status ¶
type Status struct {
// contains filtered or unexported fields
}
Status represents the sweep-line status structure. It maintains the set of segments that are currently intersecting the sweep line, ordered vertically.
It is implemented using a Red-Black Tree to achieve efficient O(log n) for add, remove, and neighbor-finding operations.
func (*Status) FindNeighbors ¶
FindNeighbors finds the segments immediately above and below a given segment in the status tree. It returns `nil` for a neighbor if one does not exist.

