๐ Computational Geometry โ Comprehensive Study Guide
**Computational Geometry** is the study of **geometric algorithms** that solve problems related to points, lines, and shapes in 2D and 3D space.
โก Why is Computational Geometry Important?
- ๐ **Used in Computer Graphics & Image Processing**.
- ๐ **Essential for AI & Robotics (Path Planning, Object Detection)**.
- ๐ก **Used in Geographic Information Systems (GIS)**.
- ๐ **Helps in 3D Modeling & Simulations**.
๐ Basic Geometric Concepts
1๏ธโฃ **Points & Coordinate Geometry**
Each point is represented as **(x, y) in 2D space**.
2๏ธโฃ **Distance Between Two Points**
double distance(Point p1, Point p2) {
return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
}
3๏ธโฃ **Line Equation (y = mx + c)**
Formula to find slope **m**:
m = (y2 - y1) / (x2 - x1);
4๏ธโฃ **Area of a Triangle (Using Determinant)**
double triangleArea(Point p1, Point p2, Point p3) {
return abs((p1.x * (p2.y - p3.y) +
p2.x * (p3.y - p1.y) +
p3.x * (p1.y - p2.y)) / 2.0);
}
๐ Convex Hull Algorithms
๐ **1. Grahamโs Scan Algorithm (O(n log n))**
Finds the smallest **convex polygon** that encloses all given points.
bool ccw(Point a, Point b, Point c) {
return (b.y - a.y) * (c.x - b.x) > (b.x - a.x) * (c.y - b.y);
}
vector convexHull(vector& points) {
sort(points.begin(), points.end(), [](Point a, Point b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
});
vector hull;
for (int i = 0; i < 2; i++) {
int start = hull.size();
for (auto& p : points) {
while (hull.size() >= start + 2 && !ccw(hull[hull.size()-2], hull.back(), p))
hull.pop_back();
hull.push_back(p);
}
hull.pop_back();
reverse(points.begin(), points.end());
}
return hull;
}
๐ **2. Jarvis March Algorithm (O(nh))**
Finds the convex hull by selecting the **leftmost point** and iteratively adding the next extreme point.
๐ ๏ธ Line Intersection & Closest Pair of Points
๐ **1. Checking if Two Lines Intersect**
bool doIntersect(Point p1, Point q1, Point p2, Point q2) {
return (ccw(p1, q1, p2) != ccw(p1, q1, q2)) &&
(ccw(p2, q2, p1) != ccw(p2, q2, q1));
}
๐ **2. Closest Pair of Points Algorithm (O(n log n))**
double closestPair(vector& points) {
sort(points.begin(), points.end(), [](Point a, Point b) { return a.x < b.x; });
return closestUtil(points, 0, points.size() - 1);
}
๐ Triangulation & Voronoi Diagrams
**Triangulation** is the process of **dividing a shape into triangles**, making computations easier.
๐ Real-World Applications of Computational Geometry
- ๐ก **Geographic Information Systems (GIS)** โ Mapping and navigation.
- ๐ฎ **Game Development** โ Collision detection & physics engines.
- ๐ค **Artificial Intelligence & Robotics** โ Object detection & pathfinding.
- ๐ผ๏ธ **Computer Graphics & 3D Modeling** โ Used in rendering 3D shapes.
โณ Time Complexity Analysis
Algorithm | Time Complexity |
---|---|
Convex Hull (Grahamโs Scan) | O(n log n) |
Closest Pair of Points | O(n log n) |
Line Intersection Check | O(1) |
Voronoi Diagram | O(n log n) |