πŸ“ Minggu 14: Computational Geometry

Bimbingan OSN Informatika - Universitas Bengkulu

Februari 2026
Progress Pembelajaran 0%

🎯 Pengenalan

Selamat! Minggu 14 adalah **minggu terakhir Stage 3**! Kita akan mempelajari **Computational Geometry** - geometri komputasi untuk menyelesaikan masalah geometri dengan algoritma!

πŸ’‘ Apa itu Computational Geometry?

Computational Geometry adalah cabang ilmu komputer yang mempelajari algoritma untuk menyelesaikan masalah geometri. Aplikasinya meliputi:

  • Computer Graphics: Rendering, collision detection
  • Robotics: Path planning, motion planning
  • GIS (Geographic Information Systems): Mapping, spatial analysis
  • Game Development: Hit detection, AI navigation

Tujuan Pembelajaran

Peta Konsep Minggu 14

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚        COMPUTATIONAL GEOMETRY                       β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                     β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”‚
β”‚  β”‚  Basic Geometry Primitives              β”‚       β”‚
β”‚  β”‚  (Points, Lines, Vectors)               β”‚       β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β”‚
β”‚                    β”‚                                β”‚
β”‚         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                     β”‚
β”‚         β–Ό                     β–Ό                     β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”            β”‚
β”‚  β”‚ DOT PRODUCT  β”‚      β”‚CROSS PRODUCT β”‚            β”‚
β”‚  β”‚ (Projection) β”‚      β”‚  (Area, CCW) β”‚            β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜            β”‚
β”‚                    β”‚                                β”‚
β”‚         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                     β”‚
β”‚         β–Ό                     β–Ό                     β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”            β”‚
β”‚  β”‚ CONVEX HULL  β”‚      β”‚  POLYGON     β”‚            β”‚
β”‚  β”‚ (Graham Scan)β”‚      β”‚   AREA       β”‚            β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜            β”‚
β”‚                                                     β”‚
β”‚         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                     β”‚
β”‚                    β–Ό                                β”‚
β”‚           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                      β”‚
β”‚           β”‚  OSN GEOMETRY    β”‚                      β”‚
β”‚           β”‚  PROBLEMS        β”‚                      β”‚
β”‚           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                

Struktur Data Point

#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));
    }
};

πŸ“ Geometry Basics

1. Dot Product (Produk Titik)

πŸ“Œ Definisi

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);
}

2. Cross Product (Produk Silang)

πŸ“Œ Definisi

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;
}

3. Line Segment Intersection

// 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;
}

4. Polygon Area

// 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;
}
πŸ’‘ Tips Geometry
  • Gunakan double untuk koordinat, bukan int
  • Hati-hati dengan floating point precision (gunakan epsilon 1e-9)
  • Cross product positif = counter-clockwise (CCW)
  • Cross product negatif = clockwise (CW)
  • Cross product = 0 = collinear (segaris)

πŸ”Ί Convex Hull

Apa itu Convex Hull?

πŸ“Œ Definisi

Convex 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!

Convex Hull

Titik biru = input points, Garis ungu = convex hull

Monotone Chain Algorithm

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;
}
πŸ’‘ Kompleksitas Monotone Chain

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!

Convex Hull Perimeter & Area

// 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;
}

Circle Geometry

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;
}

πŸ† Soal OSN Relevan

Kumpulan soal OSN-K, OSP, dan OSN Provinsi untuk materi Minggu 14 (Computational Geometry):

πŸ“Œ Keterangan Tingkat Kompetisi
  • OSN-K: Kabupaten/Kota (seleksi pertama)
  • OSP: Provinsi (seleksi kedua)
  • OSN-P: Provinsi (seleksi ketiga)
  • Nasional: OSN tingkat Nasional

A. Basic Geometry

Sumber & TahunTopikDeskripsiLevel
OSN-K 2024
Soal No. 36
Distance Menghitung jarak Euclidean ⭐⭐⭐
OSN-K 2023
Soal No. 29
Polygon Area Luas polygon dengan Shoelace formula ⭐⭐⭐⭐

B. Convex Hull

Sumber & TahunTopikDeskripsiLevel
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 ⭐⭐⭐⭐⭐

C. Line Intersection

Sumber & TahunTopikDeskripsiLevel
OSN-K 2022
Soal No. 26
Line Intersection Cek apakah dua garis berpotongan ⭐⭐⭐⭐

D. Link Latihan

πŸ“ TLX - Geometry πŸ”Ί TLX - Convex Hull πŸ“Š CSES Geometry πŸ’ͺ Codeforces
πŸ’‘ Tips Latihan Geometry
  • Pemula: Mulai dari distance dan area basic - ⭐⭐⭐
  • Menengah: Latihan cross product dan CCW - ⭐⭐⭐⭐
  • Lanjut: Coba Convex Hull - ⭐⭐⭐⭐⭐
  • Gunakan long long untuk cross product untuk hindari overflow
  • Hati-hati dengan floating point precision!
πŸ“š Referensi Tambahan
  • CP-Algorithms: https://cp-algorithms.com/geometry/
  • USACO Guide: https://usaco.guide/adv/geometry
  • Buku TOKI: "Pemrograman Kompetitif Dasar"

✏️ Latihan Soal

Kerjakan soal-soal berikut!

Soal 1: Distance Between Two Points

Hitung jarak Euclidean antara dua points.

Input
0 0
3 4
Output
5.00

Soal 2: Triangle Area

Hitung luas triangle dari tiga points.

Input
0 0
4 0
0 3
Output
6.00

Soal 3: Polygon Area

Hitung luas polygon dengan Shoelace formula.

Input
4
0 0
4 0
4 4
0 4
Output
16.00

Soal 4: Convex Hull

Cari convex hull dari sekumpulan points.

Input
6
0 0
2 2
4 0
4 4
2 1
1 3
Output
4 points: (0,0), (4,0), (4,4), (1,3)

Soal 5: Line Segment Intersection

Cek apakah dua line segments berpotongan.

Input
0 0 4 4
0 4 4 0
Output
YES

Format: x1 y1 x2 y2 (segment 1), x3 y3 x4 y4 (segment 2)

πŸ“ Kuis Pemahaman

Q1. Cross product dari dua vectors A dan B dalam 2D adalah...

  • A) A.x * B.x + A.y * B.y
  • B) A.x * B.y - A.y * B.x
  • C) A.x * B.x - A.y * B.y
  • D) A.x + A.y + B.x + B.y
βœ… Benar! Cross product 2D = A.x * B.y - A.y * B.x. Nilai absolutnya = 2Γ— luas triangle!

Q2. Jika cross product OAB positif, maka points O, A, B membentuk...

  • A) Clockwise turn
  • B) Collinear points
  • C) Counter-clockwise turn
  • D) Right angle
βœ… Benar! Cross product positif = counter-clockwise (CCW) turn.

Q3. Formula untuk luas polygon dengan n vertices adalah...

  • A) Shoelace formula
  • B) Pythagorean theorem
  • C) Heron's formula
  • D) Euler's formula
βœ… Benar! Shoelace formula menghitung luas polygon dengan koordinat vertices.

Q4. Kompleksitas waktu Monotone Chain algorithm untuk convex hull adalah...

  • A) O(N)
  • B) O(NΒ²)
  • C) O(N logΒ² N)
  • D) O(N log N)
βœ… Benar! Monotone Chain: O(N log N) untuk sorting + O(N) untuk hull = O(N log N).

Q5. Dot product digunakan untuk menghitung...

  • A) Luas triangle
  • B) Angle/projection antara vectors
  • C) Convex hull
  • D) Line intersection
βœ… Benar! Dot product digunakan untuk menghitung angle dan projection antara vectors.