Bimbingan OSN Informatika - Universitas Bengkulu
Februari 2026Selamat! Minggu 14 adalah **minggu terakhir Stage 3**! Kita akan mempelajari **Computational Geometry** - geometri komputasi untuk menyelesaikan masalah geometri dengan algoritma!
Computational Geometry adalah cabang ilmu komputer yang mempelajari algoritma untuk menyelesaikan masalah geometri. Aplikasinya meliputi:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β COMPUTATIONAL GEOMETRY β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββββββββββββββββββββββββββ β
β β Basic Geometry Primitives β β
β β (Points, Lines, Vectors) β β
β βββββββββββββββββββββββββββββββββββββββββββ β
β β β
β ββββββββββββ΄βββββββββββ β
β βΌ βΌ β
β ββββββββββββββββ ββββββββββββββββ β
β β DOT PRODUCT β βCROSS PRODUCT β β
β β (Projection) β β (Area, CCW) β β
β ββββββββββββββββ ββββββββββββββββ β
β β β
β ββββββββββββ΄βββββββββββ β
β βΌ βΌ β
β ββββββββββββββββ ββββββββββββββββ β
β β CONVEX HULL β β POLYGON β β
β β (Graham Scan)β β AREA β β
β ββββββββββββββββ ββββββββββββββββ β
β β
β ββββββββββββ¬βββββββββββ β
β βΌ β
β ββββββββββββββββββββ β
β β OSN GEOMETRY β β
β β PROBLEMS β β
β ββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#include <iostream>
#include <cmath>
using namespace std;
struct Point {
double x, y;
Point() : x(0), y(0) {}
Point(double x, double y) : x(x), y(y) {}
// Vector addition
Point operator+(const Point& other) const {
return Point(x + other.x, y + other.y);
}
// Vector subtraction
Point operator-(const Point& other) const {
return Point(x - other.x, y - other.y);
}
// Distance from origin
double dist() {
return sqrt(x*x + y*y);
}
// Distance between two points
double distTo(Point p) {
return sqrt((x-p.x)*(x-p.x) + (y-p.y)*(y-p.y));
}
};
Dot Product dari dua vectors A dan B adalah: **A Β· B = |A| Γ |B| Γ cos(ΞΈ)**
Dalam koordinat: **A Β· B = A.x Γ B.x + A.y Γ B.y**
// Dot product dari dua vectors
double dotProduct(Point a, Point b) {
return a.x * b.x + a.y * b.y;
}
// Angle between two vectors (in radians)
double angle(Point a, Point b) {
return acos(dotProduct(a, b) / (a.dist() * b.dist()));
}
// Projection of a onto b
Point project(Point a, Point b) {
double t = dotProduct(a, b) / dotProduct(b, b);
return Point(b.x * t, b.y * t);
}
Cross Product** dari dua vectors A dan B adalah: **A Γ B = |A| Γ |B| Γ sin(ΞΈ)**
Dalam 2D: **A Γ B = A.x Γ B.y - A.y Γ B.x**
Nilai absolut = 2Γ luas triangle yang dibentuk oleh A dan B!
// Cross product (z-component in 3D)
double crossProduct(Point a, Point b) {
return a.x * b.y - a.y * b.x;
}
// Cross product of vectors OA and OB
double crossProduct(Point O, Point A, Point B) {
return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x);
}
// Check if three points make a counter-clockwise turn
bool isCCW(Point O, Point A, Point B) {
return crossProduct(O, A, B) > 0;
}
// Area of triangle formed by three points
double triangleArea(Point A, Point B, Point C) {
return abs(crossProduct(A, B, C)) / 2.0;
}
// Check if point C is on segment AB
bool onSegment(Point A, Point B, Point C) {
return C.x >= min(A.x, B.x) && C.x <= max(A.x, B.x) &&
C.y >= min(A.y, B.y) && C.y <= max(A.y, B.y);
}
// Check if segments AB and CD intersect
bool segmentsIntersect(Point A, Point B, Point C, Point D) {
double d1 = crossProduct(C, D, A);
double d2 = crossProduct(C, D, B);
double d3 = crossProduct(A, B, C);
double d4 = crossProduct(A, B, D);
if (((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) &&
((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0))) {
return true;
}
// Check collinear cases
if (abs(d1) < 1e-9 && onSegment(C, D, A)) return true;
if (abs(d2) < 1e-9 && onSegment(C, D, B)) return true;
if (abs(d3) < 1e-9 && onSegment(A, B, C)) return true;
if (abs(d4) < 1e-9 && onSegment(A, B, D)) return true;
return false;
}
// Area of polygon using Shoelace formula
double polygonArea(vector& poly) {
double area = 0.0;
int n = poly.size();
for (int i = 0; i < n; i++) {
int j = (i + 1) % n;
area += poly[i].x * poly[j].y;
area -= poly[j].x * poly[i].y;
}
return abs(area) / 2.0;
}
double untuk koordinat, bukan intConvex Hull dari sekumpulan points adalah polygon convex terkecil yang mengandung semua points tersebut.
Bayangkan kamu punya paku-paku di papan, lalu kamu karet gelang di sekelilingnya - bentuk karet gelang adalah convex hull!
Titik biru = input points, Garis ungu = convex hull
Monotone Chain adalah algoritma convex hull yang simple dan efisien dengan kompleksitas O(N log N).
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Point {
long long x, y;
bool operator<(const Point& other) const {
if (x != other.x) return x < other.x;
return y < other.y;
}
};
// Cross product of vectors OA and OB
long long crossProduct(Point O, Point A, Point B) {
return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x);
}
// Monotone Chain Convex Hull
vector<Point> convexHull(vector<Point>& points) {
int n = points.size();
if (n <= 1) return points;
// Sort points by x-coordinate
sort(points.begin(), points.end());
vector<Point> hull;
// Build lower hull
for (int i = 0; i < n; i++) {
while (hull.size() >= 2 &&
crossProduct(hull[hull.size()-2], hull.back(), points[i]) <= 0) {
hull.pop_back();
}
hull.push_back(points[i]);
}
// Build upper hull
int lowerSize = hull.size();
for (int i = n - 2; i >= 0>; i--) {
while (hull.size() > lowerSize &&
crossProduct(hull[hull.size()-2], hull.back(), points[i]) <= 0) {
hull.pop_back();
}
hull.push_back(points[i]);
}
// Remove last point (duplicate of first)
if (hull.size() > 1) {
hull.pop_back();
}
return hull;
}
int main() {
int N;
cin >> N;
vector<Point> points(N);
for (int i = 0; i < N; i++) {
cin >> points[i].x >> points[i].y;
}
vector<Point> hull = convexHull(points);
cout << "Convex Hull has " << hull.size() << " points:" << endl;
for (auto& p : hull) {
cout << p.x << " " << p.y << endl;
}
return 0;
}
Time: O(N log N) untuk sorting + O(N) untuk hull = O(N log N)
Space: O(N) untuk menyimpan hull
Keunggulan: Simple, efisien, tidak perlu sorting by angle!
// Perimeter of convex hull
double perimeter(vector<Point>& hull) {
double perim = 0.0;
int n = hull.size();
for (int i = 0; i < n; i++) {
int j = (i + 1) % n;
perim += hull[i].distTo(hull[j]);
}
return perim;
}
// Area of convex hull (using polygon area formula)
double area(vector<Point>& hull) {
double area = 0.0;
int n = hull.size();
for (int i = 0; i < n; i++) {
int j = (i + 1) % n;
area += hull[i].x * hull[j].y;
area -= hull[j].x * hull[i].y;
}
return abs(area) / 2.0;
}
struct Circle {
Point center;
double radius;
};
// Area of circle
double circleArea(Circle c) {
return M_PI * c.radius * c.radius;
}
// Circumference of circle
double circleCircumference(Circle c) {
return 2.0 * M_PI * c.radius;
}
// Check if point is inside circle
bool pointInCircle(Point p, Circle c) {
return p.distTo(c.center) <= c.radius;
}
Kumpulan soal OSN-K, OSP, dan OSN Provinsi untuk materi Minggu 14 (Computational Geometry):
| Sumber & Tahun | Topik | Deskripsi | Level |
|---|---|---|---|
| OSN-K 2024 Soal No. 36 |
Distance | Menghitung jarak Euclidean | βββ |
| OSN-K 2023 Soal No. 29 |
Polygon Area | Luas polygon dengan Shoelace formula | ββββ |
| Sumber & Tahun | Topik | Deskripsi | Level |
|---|---|---|---|
| OSP 2023 Soal No. 22 |
Convex Hull | Mencari convex hull dari sekumpulan points | βββββ |
| OSN-P 2022 Soal No. 9 |
Convex Hull Area | Luas convex hull | βββββ |
| Sumber & Tahun | Topik | Deskripsi | Level |
|---|---|---|---|
| OSN-K 2022 Soal No. 26 |
Line Intersection | Cek apakah dua garis berpotongan | ββββ |
long long untuk cross product untuk hindari overflowKerjakan soal-soal berikut!
Hitung jarak Euclidean antara dua points.
0 0 3 4
5.00
Hitung luas triangle dari tiga points.
0 0 4 0 0 3
6.00
Hitung luas polygon dengan Shoelace formula.
4 0 0 4 0 4 4 0 4
16.00
Cari convex hull dari sekumpulan points.
6 0 0 2 2 4 0 4 4 2 1 1 3
4 points: (0,0), (4,0), (4,4), (1,3)
Cek apakah dua line segments berpotongan.
0 0 4 4 0 4 4 0
YES
Format: x1 y1 x2 y2 (segment 1), x3 y3 x4 y4 (segment 2)