简体   繁体   中英

Find intersection point of segments and list of intersected segments for each intersection point

I am attempting to use CGAL to find out "all intersection points" AND "intersected segments for each intersection point" from a list of segments in 2D. I want to use Bentley–Ottmann algorithm for some reasons. CGAL library has an c++ implementation of this algorithm called Sweepline 2 but with this i am able to find intersection points only. Is there any other implementation exist in CGAL? or how can I solve this problem?

Edit: The really quick fix would just be to generate all intersection points using Sweep line 2, loop through all generated intersection points, and for each intersection point record both the point's coordinates and all line segments that contain that point into a structure of your choice.


A quick solution (albeit not the most efficient) is to:

//Probably start off with a struct to store your information
struct intersection{
    //This stores the x and y position of the intersection.
    int[2] xyCoords;
    //This stores the segment ids or indexes of the intersection line segments.
    //For example, if the 1st and 5th line segment intersect, you would store 1 and 5 in here.
    int[2] segmentIDs;
}

Then just create a vector of the intersection struct so you can store all of your different intersections.

vector<intersection> intersectionVector;

Then just loop through all of the line segments you have in something similar to the following:

for (int i = 0; i < numSegments; i++){
    for (int j = 0; j < numSegments; j++){
        //Don't look at the same points.        
        if (i == j)
            continue;
        //See below for pseudocode for this part.
    }
}

Now to fill in that block, instead of reinventing anything just refer to How do you detect where two line segments intersect? .

As shown in the above link, calculate the values of r × s and (q − p) × r where × is the vector cross product. From there, just use if/else blocks to account for the four cases (ctrl f for "Now there are four cases:") if you care about detail. If you just want what you outlined in your question, just check for the validity of case 3. If it is valid, store the indexes of your line segments as well as the x and y coordinates in the vector/whatever structure you're using.

The only extra thing you need to do is use the calculate u and apply it to your second line segment of the two you're checking, in order to store the xy coordinates of the intersection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM