Angular sweep algorithm in C++
In this article, we will discuss angular sweep algorithm in C++ with several examples.
An effective computational geometry method for resolving a variety of geometric issues is the angular sweep algorithm, commonly referred to as the Rotating Callipers algorithm. The process entails turning a pair of parallel lines, or callipers, around the convex hull of a collection of points to as certain specific attributes or connections within the geometry.
The angular sweep algorithm has used an algorithm to determine how many points a circle with a given radius can have in total. Therefore, we must determine the maximum number of points the circle encloses with a circle of radius r and a given set of 2-D points, which lies inside the circle rather than on its edges. The minimum bounding box of a set of points, the diameter of a convex polygon, and the minimum bounding rectangle are three common uses of the Angular Sweep algorithm in computational geometry. Its efficiency and simplicity make it especially helpful because it frequently achieves linear or nearly linear temporal complexity.
Algorithm:
- First, we must determine the separation between each of the nC2 points that are provided in the problem.
- Once a point P is chosen randomly, find the greatest number of points that remain in the circle when rotated around it.
- Providing the maximum number of enclosed points or the problem's ultimate return value.
Example:
Let us take an example to illustrate the angular sweep algorithm in C++.
#include <bits/stdc++.h>
using namespace std;
#define MAX_POINTS 400
typedef complex<double> Point;
double dis[MAX_POINTS][MAX_POINTS];
// Function to find the maximum number of points within a given radius
int maxPoints(Point arr[], int n, double r) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
dis[i][j] = dis[j][i] = abs(arr[i] - arr[j]);
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
// Count points inside the circle centered at arr[i]
int pointsInside = 1; // Initialize with 1 for the center point
for (int j = 0; j < n; j++) {
if (i != j && dis[i][j] <= 2 * r) {
pointsInside++;
}
}
ans = max(ans, pointsInside);
}
return ans;
}
int main() {
Point arr[] = {Point(3.5, 4.5), Point(5.0, 6.0), Point(7.0, 8.0)}; // Adjusted points
double r = 2.0; // Adjusted radius
int n = sizeof(arr) / sizeof(arr[0]);
cout << "The maximum number of points within radius " << r << " is: " << maxPoints(arr, n, r) << endl; // Adjusted output message
return 0;
}
Output:
The maximum number of points within radius 2 is: 3
Example 2:
Let us take another example to illustrate the angular sweep algorithm in C++.
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
struct Point {
double x, y;
Point(double _x, double _y) : x(_x), y(_y) {}
};
// Function to calculate the Euclidean distance between two points
double distance(const Point& p1, const Point& p2) {
double dx = p1.x - p2.x;
double dy = p1.y - p2.y;
return sqrt(dx * dx + dy * dy);
}
// Function to count the number of points within a given radius of a center point
int countPointsWithinRadius(const Point& center, const vector<Point>& points, double radius) {
int count = 0;
for (const Point& p : points) {
if (distance(center, p) <= radius) {
count++;
}
}
return count;
}
// Function to find the maximum number of points within a given radius among all points
int maxPointsWithinRadius(const vector<Point>& points, double radius) {
int maxCount = 0;
for (const Point& p : points) {
int count = countPointsWithinRadius(p, points, radius);
maxCount = max(maxCount, count);
}
return maxCount;
}
int main() {
// Example usage
vector<Point> points = {Point(3.5, 4.5), Point(5.0, 6.0), Point(7.0, 8.0)}; // Sample points
double radius = 2.0; // Sample radius
int maxPoints = maxPointsWithinRadius(points, radius);
cout << "The maximum number of points within radius " << radius << " is: " << maxPoints << endl;
return 0;
}
Output:
The maximum number of points within radius 2 is: 1
Explanation:
In this example, a simple method is used to determine the maximum number of points within a given radius. It lists functions to find the maximum number of points within a given radius among all points, count the number of points within a given radius of a centre point, and compute the Euclidean distance between two points.
Conclusion:
An effective method for addressing various geometric issues in computational geometry is the Angular Sweep algorithm, which is also referred to as the Rotating Callipers algorithm and is applied in C++. With this algorithm, certain attributes or relationships within the geometry are found by rotating a pair of parallel lines, or callipers, around the convex hull of a set of points. Solving problems like determining the minimum bounding rectangle of a set of points or the diameter of a convex polygon can be accomplished by repeatedly rotating these callipers. Often achieving linear or near-linear time complexity, the Angular Sweep algorithm is especially appreciated for its efficiency and simplicity.