Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add materials for week 12 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Materials:
* Week09: Advanced Dynamic Programming
* Week10: String Algorithms
* Week11: SQRT Decomposition and Segment Tree with Lazy Propagation
* Week12: Geometry: Convex Hull

Team:

Expand Down
27 changes: 27 additions & 0 deletions week12/ConvexPolygon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
struct Point {
int x, y;
Point() { }
Point(int x, int y): x(x), y(y) { }
};

long long cross(Point o, Point a, Point b) {
return (long long) (a.x - o.x) * (b.y - o.y) - (long long) (b.x - o.x) * (a.y - o.y);
}

double area(vector<Point> v) {
double ans = 0.0;
for (int i = 0; i < (int) v.size(); ++i) {
int nxt = (i + 1) % v.size();
ans += cross(Point(0, 0), v[i], v[nxt]);
}
return fabs(ans) * 0.5;
}

double perimeter(vector<Point> v) {
double ans = 0.0;
for (int i = 0; i < (int) v.size(); ++i) {
int nxt = (i + 1) % v.size();
ans += hypot(v[i].x - v[nxt].x, v[i].y - v[nxt].y);
}
return ans;
}
54 changes: 54 additions & 0 deletions week12/GrahamScan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Implementation of Graham Scan algorithm

struct Point {
int x, y;
Point() { }
Point(int x, int y): x(x), y(y) { }
bool operator < (const Point& other) const {
// priority: lowest point
// tie: rightmost point
return (y == other.y) ? (x > other.x) : (y < other.y);
}
};

Point o;

long long cross(Point o, Point a, Point b) {
return (long long) (a.x - o.x) * (b.y - o.y) - (long long) (a.y - o.y) * (b.x - o.x);
}

bool cmp(Point a, Point b) {
long long c = cross(o, a, b);
if (c == 0LL)
return hypot(a.x - o.x, a.y - o.y) < hypot(b.x - o.x, b.y - o.y);
return c > 0;
// Angle comparison
// return (atan2(a.y - o.y, a.x - o.x) - atan2(b.y - o.y, b.x - o.x)) < 0;
}

vector<Point> convexHull(vector<Point> v) {
vector<Point> ret;
int st = 0;
// find pivot
for (int i = 1; i < (int) v.size(); ++i) {
if (v[i] < v[st]) {
st = i;
}
}
swap(v[st], v[0]);
o = v[0];
// sort the remaining points
sort(v.begin() + 1, v.end(), cmp);
ret.push_back(v.back());
ret.push_back(o);
int k = 2;
for (int i = 1; i < (int) v.size(); ++i) {
while (cross(ret[k - 2], ret[k - 1], v[i]) < 0) {
ret.pop_back();
--k;
}
ret.push_back(v[i]);
++k;
}
return ret;
}
37 changes: 37 additions & 0 deletions week12/MonotoneChain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Implementation of Andrew's Monotone Chain algorithm

struct Point {
int x, y;
Point() { }
Point(int x, int y): x(x), y(y) { }
bool operator < (const Point& other) const {
return x < other.x;
}
};

long long cross(Point o, Point a, Point b) {
return (long long) (a.x - o.x) * (b.y - o.y) - (long long) (a.y - o.y) * (b.x - o.x);
}

vector<Point> convexHull(vector<Point> v) {
vector<Point> ret;
sort(v.begin(), v.end());
int k = 0;
for (int i = 0; i < n; ++i) {
while (k > 1 && cross(ret[k - 2], ret[k - 1], v[i]) > 0) {
--k;
ret.pop_back();
}
ret.push_back(v[i]);
++k;
}
for (int i = n - 2, t = k; i >= 0; --i) {
while (k > t && cross(ret[k - 2], ret[k - 1], v[i]) > 0) {
--k;
ret.pop_back();
}
ret.push_back(v[i]);
++k;
}
return ret;
}