Home > Backend Development > C++ > body text

Klee's algorithm (union length of line segments) in C++

WBOY
Release: 2023-08-29 20:37:06
forward
1224 people have browsed it

Klees algorithm (union length of line segments) in C++

In this tutorial, we will write a program that finds the length of the union of line segments.

We have given the starting point and end point of the line segments, we need to find the length of the union of the line segments.

The algorithm we will use is called klee's algorithm.

Let’s take a look at the steps to solve this problem.

  • Initialize the array with the coordinates of all line segments.
  • Initialize a vector named points whose size is twice the size of the line segment array.
  • Traverse the line segment array.
    • Fill the first point of the current line segment and false to the position of index i * 2 of the points array.
    • Fill the second point of the current line segment and false to the position of index i * 2 1 of the points array.
  • Sort the points array.
  • Use the counter variable to traverse the points array.
    • If the counter is greater than 0, add the first point of i and i-1 to the result.
    • If there is a second point, decrement the counter by 1, otherwise increase it.
  • Return results.

Example

Let’s take a look at the code.

Demo

#include<bits/stdc++.h>
using namespace std;
int segmentUnionLength(const vector<pair <int,int>> &segments) {
   int n = segments.size();
   vector<pair<int, bool>> points(n * 2);
   for (int i = 0; i < n; i++) {
      points[i*2] = make_pair(segments[i].first, false);
      points[i*2 + 1] = make_pair(segments[i].second, true);
   }
   sort(points.begin(), points.end());
   int result = 0, count = 0;
   for (int i = 0; i < n * 2; i++){
      if (count) {
         result += points[i].first - points[i-1].first;
      }
      points[i].second ? count-- : count++;
   }
   return result;
}
int main() {
   vector<pair<int,int>> segments;
   segments.push_back(make_pair(1, 3));
   segments.push_back(make_pair(2, 7));
   segments.push_back(make_pair(6, 12));
   segments.push_back(make_pair(13, 5));
   cout << segmentUnionLength(segments) << endl;
   return 0;
}
Copy after login

Output

If you run the above code, you will get the following results.

6
Copy after login

Conclusion

If you have any questions during the tutorial, please ask them in the comment section.

The above is the detailed content of Klee's algorithm (union length of line segments) in C++. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template