Geom
Geom
Computational Geometry
Vincent Chiu
19 Mar, 2022
Computational Geometry
Table of Contents
1. Points, Lines, Segments
2. Vectors
3. Polygons
4. Convex Hull
5. Precision Handling
6. More
Computational Geometry
For example,
● & −2, −1
● * 0, 3
● , 3, 2
Computational Geometry
Points
How do we store the points in our C++ program?
● Is the following way good? sorry no syntax highlighting
Points – struct
How do we store the points in our C++ program?
● We should use struct!
struct Point {
double x = 0, y = 0;
Point() {}
Point(double x, double y) : x(x), y(y) {}
}; // don’t forget the semi-colon ;
Computational Geometry
Points – struct
How do we store the points in our C++ program?
● We should use struct!
Output Points
● Operator overloading (see “Programming in C++” 2021 Slides P.54 - 60)
# − #! !" − !! = #" − #! ! − !!
Collinear Points
=
#$#" #!$#"
Three points are collinear iff
%$%" %!$%"
● Further expanding the terms:
# − #! !" − !! = #" − #! ! − !!
!" # − !! # − !" #! + !! #! = !#" − !! #" − !#! + !! #!
!#! − !! # + !! #" − !" #! + !" # − !#" = 0
Collinear Points
First one: !#! − !! # + !! #" − !" #! + !" # − !#" = 0
● Any special meanings?
# − #'
=.
! − !'
# − #' = . ! − !'
Computational Geometry
#−4 =. !−0
# = .! + 4
Computational Geometry
# − #' = . ! − !'
# = .! + #' − .!'
⇒ 4 = #' − .!'
Computational Geometry
● Slope . = −
(
)
● #-intercept = − : Set ! = 0
*
)
● !-intercept = − (: Set # = 0
*
Computational Geometry
Distances – Implementation
Distance between points !! , #! and !" , #" :
● Euclidean distance = !" − !! " + #" − #! "
● sin D =
#
+
● tan D =%
#
Computational Geometry
Angle in Radians
● The radian (rad) is the SI unit for
measuring angles
● ; in radians = =
,+- ./0123 7
+,4567 +
● L rad = 180°
Computational Geometry
Angle in Radians
● From now on, unless otherwise specified,
angles will be measured in radians:
1 L 1
sin 30° = ⇒ sin rad =
2 6 2
Trigonometric Functions
● Why introduce radians? Because <cmath> uses radian measure!
int main() {
cout << PI << endl;
cout << sin(-5 * PI / 6) << endl; // -0.5
cout << acos(-0.5) * 180 / PI << endl; // 120
}
Computational Geometry
struct Point {
double len() const { return sqrt(x * x + y * y); }
double rad() const { return atan2(y, x); }
}
Computational Geometry
Table of Contents
1. Points, Lines, Segments
2. Vectors
3. Polygons
4. Convex Hull
5. Precision Handling
6. More
Computational Geometry
Vectors
● Don’t get confused with std::vector in C++
● Vector has magnitude (or length) and direction
● We usually represent a vector U⃗ as U% , U#
● They have no position, just “an arrow”
For example,
● &* = V = +4, +3
● ,W = U⃗ = +4, +3
● XY = Z = −4, −3
● V = U⃗ ≠ Z, while Z = −V
Computational Geometry
Vectors
● Note that vectors only reflect the direction and magnitude (length) but
not the position
● Also, vector is not a description of coordinates
● But if we set the “tail” of the vector to be
the origin %,
● We can see how the endpoint/”head” of the
vector corresponds to the coordinates of the
head: coordinate vector
● So, treat them as the same at this stage :)
Computational Geometry
But Why?
Dot product: V · U⃗ = V% U% + V# U#
● V · U⃗ = V U⃗ cos ;
Table of Contents
1. Points, Lines, Segments
2. Vectors
Extra: Complex Numbers
3. Polygons
4. Convex Hull
5. Precision Handling
6. More
Computational Geometry
Complex Plane
Geometric representation of complex numbers!
● Real axis, Imaginary axis
● a = Q + c0 ⇒ Q, c = ℜ a , ℑ a
● : = a = Q" + c "
● Argument D
Computational Geometry
Complex Plane
Complex conjugate of a = Q + c0 = :, D :
a̅ = Q − c0 = :, −D
Properties:
● a ± Z = a̅ ± Z,l aZ = a̅ Z,
l a/Z = a/̅ Z
l
● a a̅ = a " = Q " + c "
= =
! 9̅ 9̅
●
9 9 9̅ 9!
:! ∠;! :!
= ∠ ;! − ;"
:" ∠;" :"
2 5; = cos D + 0 sin D
⇒ a = Q + c0 = :2 5;
● Euler’s Identity:
2 58 = cos L + 0 sin L = −1
2 58 + 1 = 0
Computational Geometry
! !" !< !=
2% =1+ + + + +⋯
1! 2! 3! 4!
!" !=
cos ! = 1 − + −⋯
2! 4!
!< !>
sin ! = ! − + −⋯
3! 5!
Computational Geometry
0; 0; " 0; < 0; =
2 5? =1+ + + + +⋯
1! 2! 3! 4!
; " 0; < ; =
= 1 + 0; − − + −⋯
2! 3! 4!
;" ;= ;<
= 1− + −⋯ +0 ;− +⋯
2! 4! 3!
= cos ; + 0 sin ;
Computational Geometry
Complex Multiplication
a! = Q + c0 = :! ∠;! , a" = 4 + h0 = :" ∠;"
a" a!̅ = 4 + h0 Q − c0
= Q4 + ch + Qh − c4 0
Table of Contents
1. Points, Lines, Segments
2. Vectors (cont.)
3. Polygons
4. Convex Hull
5. Precision Handling
6. More
Computational Geometry
Short Break
Computational Geometry
struct Vector {
// ...
Vector(double x) : x(x), y(0) {} // conversion constructor
};
Complex operator*(const Complex& u, const Complex& v){
return Complex(u.x * v.x - u.y * v.y,
u.x * v.y + u.y * v.x);
}
Computational Geometry
V×U⃗ > 0
V×U⃗ < 0
Computational Geometry
V×U⃗ < 0
Computational Geometry
Table of Contents
1. Points, Lines, Segments
2. Vectors
3. Polygons
4. Convex Hull
5. Precision Handling
6. More
Computational Geometry
DSE Trigonometry
● Area of triangle = " Qℎ = " Qc sin ,
! !
● Sine formula:
Qc4 Q c 4
= = = = W = 2x
2×Q:2Q sin & sin * sin ,
● Cosine formula:
4 " = Q − c cos , " + c sin , "
DSE Trigonometry
● Heron’s formula:
Combine and expand
1
&:2Q = Qc sin ,
2
sin , = 1 − cos " ,
Q" + c " − 4 "
cos , =
2Qc
Computational Geometry
V×U⃗ = V U⃗ sin ;
⇒ Δarea = V×U⃗
!
"
Polygons
Let’s follow the nice tutorial prepared by Percy in 2019!
'SQTYXEXMSREP +ISQIXV]
'SRXIRX
3RLQWV /LQHV 6HJPHQWV
9HFWRUV
3RO\JRQV
&RQYH[ +XOO
3UHFLVLRQ +DQGOLQJ
0RUH
榀㸖曢免⥎㝾⌠Ⅎ䫝峤
+RQJ .RQJ 2O\PSLDG LQ ΖQIRUPDWLFV
'SQTYXEXMSREP +ISQIXV]
4SP]KSRW
• 6LPSOH SRO\JRQ QR VHOILQWHUVHFWLRQ H[FHSW DW YHUWLFHV FRQQHFWLQJ
QHLJKERULQJ OLQH VHJPHQWV
• &RQYH[ SRO\JRQ $ VLPSOH SRO\JRQ ZLWK HYHU\ LQWHULRU DQJOH QR PRUH
WKDQ π RU r
• 6WUFLWO\ FRQYH[ SRO\JRQ $ VLPSOH SRO\JRQ ZLWK HYHU\ LQWHULRU DQJOH OHVV
WKDQ π RU r
4SP]KSRW -QTPIQIRXEXMSR
• +RZ GR ZH UHSUHVHQW D SRO\JRQ"
• $V D OLVW RI SRLQWV p0 , p1 , . . . , pN −1
• $ OLQH VHJPHQW FRQQHFWLQJ
• pi DQG pi+1 IRU DOO 0 ≤ i < N − 1
• pN −1 DQG p0
bi`m+i SQHv;QM &
p2+iQ`ISQBMi= Tc
SQHv;QMUV &'
SQHv;QMUp2+iQ`ISQBMi= TV , TUTV &'
'c
榀㸖曢免⥎㝾⌠Ⅎ䫝峤
+RQJ .RQJ 2O\PSLDG LQ ΖQIRUPDWLFV
'SQTYXEXMSREP +ISQIXV]
4SP]KSRW -QTPIQIRXEXMSR
([DPSOH
SQHv;QM TQHv 4 SQHv;QMU&SQBMiUy- yV- SQBMiU9- yV- SQBMiU9- jV'Vc
+Qmi II ]7B`bi TQBMi 4 ] II TQHvXT(y)Xt II ] ] II TQHvXT(y)Xv II 2M/Hc
7Q` USQBMi Ti , TQHvXTV &
+Qmi II TiXt II ] ] II TiXv II 2M/Hc
'
榀㸖曢免⥎㝾⌠Ⅎ䫝峤
+RQJ .RQJ 2O\PSLDG LQ ΖQIRUPDWLFV
'SQTYXEXMSREP +ISQIXV]
4SP]KSRW %VIE
5HFDOOLQJ WKDW WKH DUHD RI WULDQJOH IRUPHG E\ "u DQG "v LV
1
VLJQHG DUHD ("u × "v )
2
ΖQ RWKHU ZRUGV
• LI "u WR "v LV LQ &&: GLUHFWLRQ GLUHFWLRQ DUHD !
• LI "u WR "v LV LQ &: GLUHFWLRQ GLUHFWLRQ DUHD
榀㸖曢免⥎㝾⌠Ⅎ䫝峤
+RQJ .RQJ 2O\PSLDG LQ ΖQIRUPDWLFV
'SQTYXEXMSREP +ISQIXV]
4SP]KSRW %VIE
)RU D FRQYH[ SRO\JRQ ZH FDQ GLYLGH WKH DUHD LQWR N − 2 WULDQJOHV
∆p0 p1 p2 , ∆p0 p2 p3 , . . . , ∆p0 pN −2 pN −1
!
N −2
1 ! −−→ −−−−→
N −2
7KH DUHD RI WKH SRO\JRQ Area(∆p0 pi pi+1 ) = p0 pi × p0 pi+1
2
i=1 i=1
榀㸖曢免⥎㝾⌠Ⅎ䫝峤
+RQJ .RQJ 2O\PSLDG LQ ΖQIRUPDWLFV
'SQTYXEXMSREP +ISQIXV]
榀㸖曢免⥎㝾⌠Ⅎ䫝峤
+RQJ .RQJ 2O\PSLDG LQ ΖQIRUPDWLFV
'SQTYXEXMSREP +ISQIXV]
4SP]KSRW %VIE
!
N −2
$UHD 1 −
p−→ −−−−→
2 0 pi × p0 pi+1
i=1
$FWXDOO\ WKLV IRUPXOD ZRUNV RQ DQ\ VLPSOH SRO\JRQ ERWK FRQYH[ DQG FRQFDYH
:K\" EHFDXVH WKH FURVV SURGXFW LV VLJQHG
榀㸖曢免⥎㝾⌠Ⅎ䫝峤
+RQJ .RQJ 2O\PSLDG LQ ΖQIRUPDWLFV
'SQTYXEXMSREP +ISQIXV]
4SP]KSRW %VIE
!
N −2
$UHD 1 −
p−→ −−−−→
2 0 pi × p0 pi+1
i=1
榀㸖曢免⥎㝾⌠Ⅎ䫝峤
+RQJ .RQJ 2O\PSLDG LQ ΖQIRUPDWLFV
'SQTYXEXMSREP +ISQIXV]
4SP]KSRW %VIE
!
N −2
$UHD 1 −
p−→ −−−−→
2 0 pi × p0 pi+1
i=1
榀㸖曢免⥎㝾⌠Ⅎ䫝峤
+RQJ .RQJ 2O\PSLDG LQ ΖQIRUPDWLFV
'SQTYXEXMSREP +ISQIXV]
4SP]KSRW %VIE
!
N −2
$UHD 1 −
p−→ −−−−→
2 0 pi × p0 pi+1
i=1
榀㸖曢免⥎㝾⌠Ⅎ䫝峤
+RQJ .RQJ 2O\PSLDG LQ ΖQIRUPDWLFV
'SQTYXEXMSREP +ISQIXV]
4SP]KSRW %VIE
!
N −2
$UHD 1 −
p−→ −−−−→
2 0 pi × p0 pi+1
i=1
榀㸖曢免⥎㝾⌠Ⅎ䫝峤
+RQJ .RQJ 2O\PSLDG LQ ΖQIRUPDWLFV
'SQTYXEXMSREP +ISQIXV]
4SP]KSRW %VIE
!
N −2
$UHD 1 −
p−→ −−−−→
2 0 pi × p0 pi+1
i=1
榀㸖曢免⥎㝾⌠Ⅎ䫝峤
+RQJ .RQJ 2O\PSLDG LQ ΖQIRUPDWLFV
'SQTYXEXMSREP +ISQIXV]
4SP]KSRW %VIE
!
N −2
$UHD 1 −
p−→ −−−−→
2 0 pi × p0 pi+1
i=1
榀㸖曢免⥎㝾⌠Ⅎ䫝峤
+RQJ .RQJ 2O\PSLDG LQ ΖQIRUPDWLFV
'SQTYXEXMSREP +ISQIXV]
4SP]KSRW %VIE
!
N −2
$UHD 1 −
p−→ −−−−→
2 0 pi × p0 pi+1
i=1
7KLV '2(6 127 ZRUN RQ QRQVLPSOH SRO\JRQ
榀㸖曢免⥎㝾⌠Ⅎ䫝峤
+RQJ .RQJ 2O\PSLDG LQ ΖQIRUPDWLFV
Computational Geometry
B$"
1
&:2Q = y -' -5 ×-' -5@!
2
5A!
Computational Geometry
B$!
1
&:2Q = y %-5 ×%-5@!
2
5A'
Computational Geometry
B$!
1
&:2Q = y %-5 ×%-5@!
2
5A'
B$!
1
&:2Q = y %-5 ×%-5@!
2
5A'
Table of Contents
1. Points, Lines, Segments
2. Vectors
3. Polygons
4. Convex Hull
5. Precision Handling
6. More
Computational Geometry
Convex Hull
A convex hull is the smallest convex polygon that contains all the given points
Computational Geometry
Convex Hull
Let’s follow the nice tutorial prepared by Alex Tung in 2017!
<Finding convex hull>
• Given ¨ points |[ , |] , … , |Æ on plane, now we want to find a
smallest convex polygon H which contains all ¨ points
• Fact: we can always choose the vertices of H to be a subset of
{|[ , |] , … , |Æ } (why?)
i=1
STACK = [|[ ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=2
STACK = [|[ , |] ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=3
STACK = [|[ , |] , |Ü ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=4
STACK = [|[ , |] , |Ü , |à ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=4
STACK = [|[ , |] , |Ü , |à ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=4
STACK = [|[ , |] , |à ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=5
STACK = [|[ , |] , |à , |æ ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=6
STACK = [|[ , |] , |à , |æ , |ø ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=6
STACK = [|[ , |] , |à , |æ , |ø ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=6
STACK = [|[ , |] , |à , |ø ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=7
STACK = [|[ , |] , |à , |ø , |¿ ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=7
STACK = [|[ , |] , |à , |ø , |¿ ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=7
STACK = [|[ , |] , |à , |¿ ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=8
STACK = [|[ , |] , |à , |¿ , |¡ ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=9
STACK = [|[ , |] , |à , |¿ , |¡ , |¬ ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=9
STACK = [|[ , |] , |à , |¿ , |¡ , |¬ ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=9
STACK = [|[ , |] , |à , |¿ , |¬ ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=9
STACK = [|[ , |] , |à , |¿ , |¬ ]
• Step 2: Start with an empty stack.
Maintain the stack s.t. every turn is
a right turn
i=9
STACK = [|[ , |] , |à , |¬ ]
i=9
STACK = [|[ , |] , |à , |¬ ]
• Step 3: Similar to step 2, but
process points in reverse order
i=8
STACK = [|[ , |] , |à , |¬ , |¡ ]
• Step 3: Similar to step 2, but
process points in reverse order
i=7
STACK = [|[ , |] , |à , |¬ , |¡ , |¿ ]
• Step 3: Similar to step 2, but
process points in reverse order
i=6
STACK = [|[ , |] , |à , |¬ , |¡ , |¿ , |ø ]
• Step 3: Similar to step 2, but
process points in reverse order
i=6
STACK = [|[ , |] , |à , |¬ , |¡ , |¿ , |ø ]
• Step 3: Similar to step 2, but
process points in reverse order
i=6
STACK = [|[ , |] , |à , |¬ , |¡ , |ø ]
• Step 3: Similar to step 2, but
process points in reverse order
i=5
STACK = [|[ , |] , |à , |¬ , |¡ , |ø , |æ ]
• Step 3: Similar to step 2, but
process points in reverse order
i=5
STACK = [|[ , |] , |à , |¬ , |¡ , |ø , |æ ]
• Step 3: Similar to step 2, but
process points in reverse order
i=5
STACK = [|[ , |] , |à , |¬ , |¡ , |æ ]
• Step 3: Similar to step 2, but
process points in reverse order
i=4
STACK = [|[ , |] , |à , |¬ , |¡ , |æ , |à ]
i=3
STACK = [|[ , |] , |à , |¬ , |¡ , |æ , |à , |Ü ]
• Step 3: Similar to step 2, but
process points in reverse order
i=3
STACK = [|[ , |] , |à , |¬ , |¡ , |æ , |à , |Ü ]
• Step 3: Similar to step 2, but
process points in reverse order
i=3
STACK = [|[ , |] , |à , |¬ , |¡ , |æ , |Ü ]
• Step 3: Similar to step 2, but
process points in reverse order
i=2
STACK = [|[ , |] , |à , |¬ , |¡ , |æ , |Ü , |] ]
• Step 3: Similar to step 2, but
process points in reverse order
i=2
STACK = [|[ , |] , |à , |¬ , |¡ , |æ , |Ü , |] ]
• Step 3: Similar to step 2, but
process points in reverse order
i=2
STACK = [|[ , |] , |à , |¬ , |¡ , |æ , |] ]
• Step 3: Similar to step 2, but
process points in reverse order
i=1
STACK = [|[ , |] , |à , |¬ , |¡ , |æ , |] , |[ ]
• Step 3: Similar to step 2, but
process points in reverse order
i=1
STACK = [|[ , |] , |à , |¬ , |¡ , |æ , |] , |[ ]
• Step 3: Similar to step 2, but
process points in reverse order
i=1
STACK = [|[ , |] , |à , |¬ , |¡ , |æ , |[ ]
• Done :)
• I haven’t told you how to check if a
turn is a right turn
Table of Contents
1. Points, Lines, Segments
2. Vectors
3. Polygons
4. Convex Hull
5. Precision Handling
6. More
Computational Geometry
Precision Handling
If the problem allows, try to use int or long long to avoid precision errors!
Table of Contents
1. Points, Lines, Segments
2. Vectors
3. Polygons
4. Convex Hull
5. Precision Handling
6. More
Computational Geometry
More
There are still many other interesting tricks to learn!
● distance to line
● distance to segment
● intersection
● …]
However, probably you won’t see these stuffs in secondary school level OI
competitions…
● To practice, one simple way is Codeforces (tag = geometry)
Still, it’s important to learn how to code nicely :)
Computational Geometry
Questions?