Calculates the intersection, intersection, closed area, and closed collection between points, lines, and surfaces (Continued 5)

Source: Internet
Author: User

Monotonous Link Description: For line L = {P1, P2, P3 ,..., Pn}, Xi is the abscissa of Pi. If xi <= xi + 1, or xi> = xi + 1, the line is called a monotonic increasing (or decreasing) link about the X axis. The Y axis is also applicable.

Intersection of multiple line segments on the plane.
For N line segments on the plane, if the most direct method is used, that is, the intersection of the two, then (N-1) + (N-2) +... 1 = N (N-1)/2 operation, time complexity is O (n2 ). This method is feasible when the number of intersections is close to n (N-1)/2, but in most cases, the number of intersections formed by N line segments is small, in this case, the algorithm can be adjusted to a more efficient method related to the number of input line segments and the number of output intersections, such as the Bentley-ottmann algorithm.


The idea of the algorithm is to construct the vertex queue in ascending order of the X axis and initialize the scanning line linked list, and then cycle all the elements in the vertex queue:
When the retrieved element is the left endpoint of a line segment, First insert the line segment into the Scan Line Segment linked list, and sort all the elements in the linked list in ascending order of the Y axis, the intersection between the line segment and the upper and lower adjacent lines in the Scan Line list is calculated respectively. If there is an intersection, the intersection is inserted into the vertex queue.

When the retrieved element is the right side of a line segment, the line segment is first deleted from the list of scanned line segments, and then the intersection between the two adjacent line segments and the line segment is calculated, if there is an intersection but it does not exist in the vertex queue, insert it into the vertex queue.

When the retrieved element is an intersection, the intersection is first added to the final result set to be output. Then we can obtain the two lines that are subordinate to the intersection point, exchange their positions in the Scan Line linked list, and calculate the intersection points with the new adjacent lines respectively. If there is an intersection, but it does not exist in the vertex queue, insert it into the vertex queue.

In the preceding three cases, the extracted elements are removed from the vertex queue.

When the loop processing is complete, the elements in the result set are all intersections.

In the end, this method replaces the two loops with one loop to calculate the intersection point of the most direct twy line segments. However, it should be noted that when the number of intersections is close to N2, the efficiency of the Bentley-ottmann algorithm is lower than the method for directly finding their intersections.

Reference code:

Int intersect_polygon (polygon PN)

{

Eventqueue eq (PN); // vertex queue

Sweepline SL (PN); // scan-line linked list

Point E; // The current vertex

Slseg * s; // The current line segment

Point singlepoint; // the intersection point to be obtained

Pointlist pl; // all the obtained intersection points are saved in it.

While (EQ! = Empty) // The cycle starts until the queue is empty.

{

E = eq-> next (); // obtain the starting vertex element.

If (e-> type = left) {// start vertex of a line segment

S = SL. Add (E); // adds the line segment to the scan link list.

If (SL. Intersect (S, S-> abve, singlepoint) // determines whether the line segment to which the vertex belongs overlaps with the line segment on it

{

Eq-> insert (singlepoint); // Insert the intersection point to the vertex queue

SL. Record (S, S-> abve, singlepoint); // records the upper and lower line segments of the intersection in the Scan Line linked list

}

If (SL. Intersect (S, S-> below, singlepoint ))

Eq-> insert (singlepoint); // Insert the intersection point to the vertex queue

}

Else if (e-> type = right) // when it is the end vertex of a line segment

{

S = SL. Find (E); // find the line segment of the vertex from the scan line linked list

If (SL. Intersect (S-> above, S-> below, singlepoint) // determines whether the adjacent upper and lower lines of the line segment are intersecting.

If (Eq-> Find (singlepoint) = false) // If the obtained intersection does not exist in the vertex queue

Eq-> insert (singlepoint) // Insert the intersection of the new result to the vertex queue

SL. Remove (s); // remove the line segment to which the vertex belongs from the scan line linked list

}

Else // when it is an intersection

{

Pl. Add (E); // Save the result to the final output set.

Slseg * SE1 = SL. findrecord (E, above); // obtain the first line segment of the intersection in the Scan Line linked list.

Slseg * se2 = SL. findrecord (E, below); // obtain the second line segment of the intersection in the Scan Line linked list.

SL. Swap (SE1, se2); // swap the position of two line segments in the linked list. It can be viewed from the ry as: after passing through the vertex, the relationship between the upper and lower positions of the two line segments is exchanged.

If (SL. Intersect (se2, se2-> above, singlepoint) // determines whether the new adjacent line segments of the upper and lower sides are intersecting.

If (Eq-> Find (singlepoint) = false) // If the obtained intersection does not exist in the vertex queue

Eq-> insert (singlepoint) // Insert the intersection of the new result to the vertex queue

If (SL. Intersect (SE1, SE1-> below, singlepoint) // determines whether the new adjacent line segments of the upper and lower sides are intersecting.

If (Eq-> Find (singlepoint) = false) // If the obtained intersection does not exist in the vertex queue

Eq-> insert (singlepoint) // Insert the intersection of the new result to the vertex queue

}

Eq-> remove (E); // This element is processed and displayed.

}

Return 1;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.