Geometry Algorithms
Geometry Algorithms
Geometry
Computational geometry
(3, 6)
(1, 3)
(6, 3)
(3, 6)
(1, 3)
(6, 3)
u
v
Simplest operation,
addition is defined as
( ) ( ) (
)
x0
x1
x0 + x1
+
=
y0
y1
y0 + y1
u
v
u + v
Simplest operation,
addition is defined as
( ) ( ) (
)
x0
x1
x0 + x1
+
=
y0
y1
y0 + y1
u
v
Simplest operation,
addition is defined as
( ) ( ) (
)
x0
x1
x0 + x1
+
=
y0
y1
y0 + y1
Subtraction is defined in
the same manner
( ) ( ) (
)
x0
x1
x0 x1
=
y0
y1
y0 y1
u
v
Simplest operation,
addition is defined as
( ) ( ) (
)
x0
x1
x0 + x1
+
=
y0
y1
y0 + y1
Subtraction is defined in
the same manner
( ) ( ) (
)
x0
x1
x0 x1
=
y0
y1
y0 y1
u
v
u v
p1 p0
p1
p0
p1 p0
= (x1 x0 )2 + (y1 y0 )2
struct point {
...
double distance(point oth = point(0,0)) const {
return sqrt(pow(x - oth.x, 2.0)
+ pow(y - oth.y, 2.0));
}
...
}
struct point {
...
double distance(point oth = point(0,0)) const {
return sqrt(pow(x - oth.x, 2.0)
+ pow(y - oth.y, 2.0));
}
...
}
Or use the abs function with complex<double>.
10
p1
q0
q1
p0
10
r0
p1
r1
p0
10
r0
p1
r1
p0
Either way
pair<point,point>
10
Circles
11
Circles
p1
p0
11
Circles
r0
r1
p1
p0
11
Circles
r0
r1
p1
p0
11
Dot product
Given two vectors
( )
x0
u =
y0
( )
x1
v =
y1
12
Dot product
Given two vectors
( )
x0
u =
y0
( )
x1
v =
y1
12
Dot product
13
Dot product
vu
13
Dot product
v
q
vu
13
Dot product
vu
13
Dot product
vu
13
Dot product
Rest of the code will use the complex class.
#define P(p) const point &p
#define L(p0, p1) P(p0), P(p1)
double dot(P(a), P(b)) {
return real(a) * real(b) + imag(a) * imag(b);
}
double angle(P(a), P(b), P(c)) {
return acos(dot(b - a, c - b) / abs(b - a) / abs(c - b));
}
point closest_point(L(a, b), P(c), bool segment = false) {
if (segment) {
if (dot(b - a, c - b) > 0) return b;
if (dot(a - b, c - a) > 0) return a;
}
double t = dot(c - a, b - a) / norm(b - a);
return a + t * (b - a);
}
14
Cross product
Given two vectors
( )
x0
u =
y0
( )
x1
v =
y1
15
Cross product
Given two vectors
( )
x0
u =
y0
( )
x1
v =
y1
15
Cross product
16
Cross product
1
|u
2
v|
u
16
Cross product
1
|u
2
v|
u
|u v| < 0
|u v| = 0
|u v| > 0
iff
iff
iff
<
=
>
16
Counterclockwise
C
v
17
Counterclockwise
C
+
u
u = B C v = B A
u v > 0
17
Counterclockwise
C
u
17
Counterclockwise
C
CBA
u
B
Counterclockwise
double cross(P(a), P(b)) {
return real(a)*imag(b) - imag(a)*real(x);
}
double ccw(P(a), P(b), P(c)) {
return cross(b - a, c - b);
}
bool collinear(P(a), P(b), P(c)) {
return abs(ccw(a, b, c)) < EPS;
}
18
Intersections
Very common task is to find the intersection of two lines or
line segments.
19
Intersections
Very common task is to find the intersection of two lines or
line segments.
Given a pair of points (x0 , y0 ), (x1 , y1 ), representing a
line we want to start by obtaining the form
Ax + By = C.
19
Intersections
Very common task is to find the intersection of two lines or
line segments.
Given a pair of points (x0 , y0 ), (x1 , y1 ), representing a
line we want to start by obtaining the form
Ax + By = C.
We can do so by setting
A = y1 y0
B = x0 x1
C = A x0 + B y1
19
Intersections
Very common task is to find the intersection of two lines or
line segments.
Given a pair of points (x0 , y0 ), (x1 , y1 ), representing a
line we want to start by obtaining the form
Ax + By = C.
We can do so by setting
A = y1 y0
B = x0 x1
C = A x0 + B y1
Intersections
For two lines
A0 x + B0 y = C0
A1 x + B1 y = C1
The intersection point is
(B1 C0 B0 C1 )
D
(A0 C1 A1 C0 )
y=
D
x=
Where
D = A0 B1 A1 B0
20
Intersections
Quite similar problem is to find the intersections of two
circles.
rA
A
rB
d
21
Intersections
Quite similar problem is to find the intersections of two
circles.
rA
A
rB
d
21
Intersections
Quite similar problem is to find the intersections of two
circles.
rA
A
rB
d
21
Intersections
Quite similar problem is to find the intersections of two
circles.
rA
A
rB
d
If d = 0 and r0 = r1 , the
circles are the same.
21
Intersections
Quite similar problem is to find the intersections of two
circles.
rA
A
rB
d
If d = 0 and r0 = r1 , the
circles are the same.
21
Intersections
Quite similar problem is to find the intersections of two
circles.
rA
A
rB
h
a
b2 + h2 = r21
21
Intersections
Quite similar problem is to find the intersections of two
circles.
rA
A
rB
h
a
b2 + h2 = r21
We get
a=
r2A r2B + d2 )
2d
h2 = r2A a2
21
Intersections
#define C(p, r) const point &p, double r
int intersect(C(A, rA), C(B, rB), point & res1, point & res2) {
double d = abs(B - A);
if ( rA + rB < d - EPS || d < abs(rA - rB) - EPS){
return 0;
}
double a = (rA*rA - rB*rB + d*d) / 2*d;
double h = sqrt(rA*rA - a*a);
point v = normalize(B - A, a);
u = normalize(rotate(B-A), h);
res1 = A + v + u;
res2 = A + v - u;
if (abs(u) < EPS){
return 1;
}
return 2;
}
22
Polygons
23
Polygons
23
Polygons
23
Polygons
23
Polygons
23
Polygons
23
Polygons
23
Polygons
23
Polygons
23
Polygons
double polygon_area_signed(const vector<point> &p) {
double area = 0;
int cnt = size(p);
for (int i = 1; i + 1 < cnt; i++){
area += cross(p[i] - p[0], p[i + 1] - p[0])/2;
}
return area;
}
double polygon_area(vector<point> &p) {
return abs(polygon_area_signed(p));
}
24
Convex hull
25
Convex hull
25
Convex hull
25
Convex hull
25
Convex hull
Graham scan:
26
Convex hull
Graham scan:
Pick the point p0 with the lowest y coordinate.
26
Convex hull
Graham scan:
Pick the point p0 with the lowest y coordinate.
Sort all the points by polar angle with p0 .
26
Convex hull
Graham scan:
Pick the point p0 with the lowest y coordinate.
Sort all the points by polar angle with p0 .
Iterate through all the points
26
Convex hull
Graham scan:
Pick the point p0 with the lowest y coordinate.
Sort all the points by polar angle with p0 .
Iterate through all the points
If the current point forms a clockwise angle with the
last two points, remove last point from the convex set.
26
Convex hull
Graham scan:
Pick the point p0 with the lowest y coordinate.
Sort all the points by polar angle with p0 .
Iterate through all the points
If the current point forms a clockwise angle with the
last two points, remove last point from the convex set.
Otherwise, add the current point to the convex set.
26
Convex hull
Graham scan:
Pick the point p0 with the lowest y coordinate.
Sort all the points by polar angle with p0 .
Iterate through all the points
If the current point forms a clockwise angle with the
last two points, remove last point from the convex set.
Otherwise, add the current point to the convex set.
Time complexity O(N log N).
26
Convex hull
27
Convex hull
27
Convex hull
27
Convex hull
9
11
12
7
6
8
10
5
p0
2
1
27
Convex hull
9
11
12
7
6
8
10
5
p0
2
1
27
Convex hull
9
11
12
7
6
8
10
5
p0
2
1
27
Convex hull
9
11
12
7
6
8
10
5
p0
2
1
27
Convex hull
9
11
12
7
6
8
10
5
p0
2
1
27
Convex hull
9
11
12
7
6
8
10
5
p0
2
1
27
Convex hull
9
11
12
7
6
8
10
5
p0
2
1
27
Convex hull
9
11
12
7
6
8
10
5
p0
2
1
27
Convex hull
9
11
12
7
6
8
10
5
p0
2
1
27
Convex hull
9
11
12
7
6
8
10
5
p0
2
1
27
Convex hull
9
11
12
7
6
8
10
5
p0
2
1
27
Convex hull
9
11
12
7
6
8
10
5
p0
2
1
27
Convex hull
9
11
12
7
6
8
10
5
p0
2
1
27
Convex hull
9
11
12
7
6
8
10
5
p0
2
1
27
Convex hull
point hull[MAXN];
int convex_hull(vector<point> p) {
int n = size(p), l = 0;
sort(p.begin(), p.end(), cmp);
for (int i = 0; i < n; i++) {
if (i > 0 && p[i] == p[i - 1])
continue;
while (l >= 2 && ccw(hull[l - 2], hull[l - 1], p[i]) >= 0)
l--;
hull[l++] = p[i];
}
int r = l;
for (int i = n - 2; i >= 0; i--) {
if (p[i] == p[i + 1])
continue;
while (r - l >= 1 && ccw(hull[r - 2], hull[r - 1], p[i]) >= 0)
r--;
hull[r++] = p[i];
}
return l == 1 ? 1 : r - 1;
}
28
Convex hull
29
Convex hull
29
Convex hull
29
Convex hull
29
Convex hull
29
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
30
31
31
31
31
32
32
32
32
32
32
32
32
33
33
33
33
33
34