Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining unordered line segments

My algorithm produces a list of (usually) several thousand line segments (all 2D) which I need to join up into large polylines. These resulting polylines might be closed or open, but they are never self-intersecting. The line segments are not directed, i.e. it might be needed to flip a line segment before it can be joined to its neighbour.

What would be an extremely fast way of finding these polylines? I have to do this in real-time, so anything that takes longer than -say- 10ms is not a solution.

I'm doing this in C#, but I'm looking for ideas, not source.

like image 442
David Rutten Avatar asked Sep 16 '09 23:09

David Rutten


People also ask

What is Klee algorithm?

Klee's Algorithm (Length Of Union Of Segments of a line) Count maximum points on same line. Minimum lines to cover all points. Represent a given set of points by the best possible straight line. Program to find line passing through 2 Points.

Which line segments are intersecting?

Two given line segments are said to be intersecting lines when they have a point of intersection. A point of intersection is referred to the point where two or more line segments meet. These lines are not parallel to each other and hence intersect when drawn forward.

How much time is required to find whether two line segment given in a plane intersect or not?

Naive Algorithm A naive solution to solve this problem is to check every pair of lines and check if the pair intersects or not. We can check two line segments in O(1) time. Therefore, this approach takes O(n2).


1 Answers

Would a hash of endpoints work?

If the endpoints match exactly then you can just store each object twice in a hash, once by each endpoint. Then, for each object, look up both its end points. You will get any other objects that need to be joined.

If the endpoints have any kind of imprecision, then you will need a spatial index, and probably one that uses an R-tree. You can get a similar effect by just making a 2d hash grid. The hash grid contains buckets of nearby endpoints. You will need to check neighboring cells.

like image 174
DigitalRoss Avatar answered Oct 07 '22 13:10

DigitalRoss