Вы находитесь на странице: 1из 23

This course material is now made available for public usage.

Special acknowledgement to School of Computing, National University of Singapore


for allowing Steven to prepare and distribute these teaching materials.
CS3233 CS3233
CompetitiveProgramming p g g
Dr. Steven Halim Dr.StevenHalim
Week12 (Computational)Geometry
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
ClassPhotos
Importantfordocumentation/promotionalpurpose
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
ThemajorpartofthehardcopymaterialofatopICPCteamisusually
a collection of geometric libraries
GEOMETRYBASICSANDLIBRARIES
acollectionofgeometriclibraries
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Some Comp Geometry Principles SomeCompGeometryPrinciples
Wheneverpossible,weprefertest (predicates) than
computingnumericalanswers
Tests:
Avoiddivisionandsquareroot
(andanyoperationsthatwillproducenumericalerrors)
Preferably,alloperationsaredoneinintegers
Geometry Basics 0D GeometryBasics 0D
0DObjects:
Point
struct point { int x, y }; // preferred mode
struct point_d { double x, y };
Reiterate:wheneverpossible,workwithintegers
PS: for floating point comparisons in this lecture use: PS:forfloatingpointcomparisonsinthislecture,use:
#define EPS 1e-9
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Geometry Basics 1D (1) GeometryBasics 1D(1)
1DObjects:
Lines(ch7_01_lines.cpp)
Lineequation,y=mx +c
Becarefulwithverticallines(specialcase)
Better line equation ax + by + c = 0 Betterlineequation,ax+by+c=0
(Line)Segments:Linewithtwoendpoints
li { d bl b } struct line { double a, b, c; };
void points_to_line(point p1, point p2, line *l)
void point_and_slope_to_line(point p, double m, line *l)
bool parallel_line(line l1, line l2)
bool same_line(line l1, line l2)
void intersect_point(line l1, line l2, point_d *p)
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
left turn/ccw Test left_turn/ccw Test
Given3pointsp,q,r inaplane,theseareequivalent:
Thesequencep,q,r isaleftturn
Thetherepointsareinancounterclockwisemanner/ccw
Thetrianglepqr hasapositivearea
Thepointr isontheleftsideoflinesegmentpq
r
1 p p
0 1
1
2 1
2 1
> q q
p p
q
1
2 1
r r
p
q
Geometry Basics 1D (2) GeometryBasics 1D(2)
collinearandleft_turn (ccw)test
int turn(point p, point q, point r)
bool collinear(point p, point q, point r)
bool ccw(point p, point q, point r)
ComputingDistances
double dist(point p1, point p2) {}
double distToLine(point a, point b, point p, point *c)
double distToLineSegment(point a, point b, point p, point *c)
p4
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
p4
Geometry Basics 1D (3) GeometryBasics 1D(3)
Vectors:magnitude+direction,fortranslation
struct vector { double x, y; }; // similar to point
vector toVector(point_d p1, point_d p2)
point_d translate(point_d p, vector_d v)
Rotation
point_d rotate(point_d p, double degree) // counter clockwise
// be careful on using this, translate to origin first
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Geometry Basics 2D (1) GeometryBasics 2D(1)
2DObjects:
Circles(ch7_02_circles.cpp)
Acirclecenteredat(a,b)andradiusristhesetofallpoints(x,y)
suchthat(x a)
2
+(y b)
2
=r
2
int in circle(point p, point c, int r) _ (p p, p , )
// 0 inside, 1 at border, 2 - outside
=2*acos(0.0)
Di t d 2 * Diameterd=2*r
Circumferencec= *d
Arc length of a circle with circumference c + angle o = o / 360.0 * c Arclengthofacirclewithcircumferencec+angleo o /360.0 c
Chordlengthofacirclewithradiusr+angleo =
sqrt(2*r*r/(1 cos(o))) viatheLawofCosines
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Geometry Basics 2D (2) GeometryBasics 2D(2)
f l * * AreaofcircleA=*r*r
SectorareaofcirclewithareaA+angleo =o /360.0*A
Segment area of circle with area A + angle o = SegmentareaofcirclewithareaA+angleo =
Future:
Circlethrough3points(relatedtoTriangle)
Innercircle
Outercircle
Circlethrough2points
in_circle testversion2,given3pointsthatformsacircleandanotherpointp,
testifpisinsidethecircleornot
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
in circle Test v2 in_circleTestv2
Giventhreepointsp,q,r
Formacircumcircle
Testifapointsisinthecircle
s
1
2 2
+ p p p p
r
? 0 1
1
1
1
1
2 1
2 1
2 2
2
2
2
1 2 1
2 1 2 1
>
+
+
+
q q
p p
q q q q
p p p p
1
1
1
2 1
2
2
2
1 2 1
2
2
2
1 2 1
+
+
r r
s s s s
r r r r
q
p
Circle Through 3 Points CircleThrough3 Points
Giventhreepointsp,q,r
Determinethecircumcenterc1andradiusR1oftheinner
triangleand(c2,R2)oftheoutertriangle
R1=A/s;R2=axbxc/(4*A)
circumcenterc1,c2:willbeaddedinthefuture
r r
c2
R2
R1
q q
c1
R1
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
p p
Circle Through 2 Points CircleThrough2 Points
Given2points(p1,p2)+radiusR,determinec1+c2
bool circle_2_points_radius(point_d p1, point_d p2, double r,
point d *ctr) point_d ctr)
p1
c1
r
c2
c1
r
p2
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Geometry Basics 2D (3) GeometryBasics 2D(3)
Triangles(ch7_03_triangles.cpp)
Polygonwiththreeverticesandthreeedges
Area of Triangle 1: A 0 5 * b x h AreaofTriangle1:A=0.5*bxh
Perimeterp=a+b+c;wherea,b,carethelengthofthe3edges
AreaofTriangle2:A=sqrt(s)*sqrt(s a)*sqrt(s b)*sqrt(s c), g q ( ) q ( ) q ( ) q ( ),
wheresemiperimeters=0.5*p
ThisiscalledtheHeronsformula
Radius of inner circle r = A / s Radiusofinnercircler=A/s
RadiusofoutercircleR=(a*b*c)/(4.0*A)
Trigonometry/LawofCosines g y/
c
2
=a
2
+b
2
2*a*b*cos()
Trigonometry/Phytagorean Theorem
2 2
b
2
b (90 0 d / i ht l ) 0 c
2
=a
2
+b
2
becausecos(90.0degrees/rightangle)=0
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Geometry Basics 2D (4) GeometryBasics 2D(4)
/ f Trigonometry/LawofSines
bc /sin(o)=ca/sin(|)=ab /sin()
Rectangles/Trapezium/Quadrilateral (no sample code) Rectangles/Trapezium/Quadrilateral(nosamplecode)
Area
Perimeter
Etc
Polygon(ch7_04_polygon.cpp)
Planefigurethatisboundedbyaclosedcircuitcomposedofa
finitesequence ofstraightlinesegments
Basic form vertices are ordered either in cw or ccw order Basicform,verticesareorderedeitherincw orccw order
vector<point> P; // preferred mode
vector<point d> P; p _
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Geometry Basics 2D (5) GeometryBasics 2D(5)
f l ( l) Perimeterofpolygon(trivial)
Areaofpolygon(halfdeterminant)
Checking if the polygon is convex (concave otherwise) Checkingifthepolygonisconvex(concaveotherwise)
Checkingifapointisinsideoroutsidea(convex/concave)polygon
Cuttingapolygonwithastraightline
Findingaconvexhullofapolygon/GrahamsScan(discussednext)
do ble pe imete ( ecto <point> P) double perimeter(vector<point> P)
double area(vector<point> P)
bool is_convex(vector<point> P)
b l i t i l ( i t t i t P) bool point_in_polygon(point p, vector<point> P)
vector<point> cut_polygon(point a, point b, vector<point> P)
vector<point> GrahamScan(vector<point> P)
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Area of a Polygon (area) AreaofaPolygon(area)
Giventheverticesofapolygoninacircularmanner
(cw orccw),itsareais
1 1
y x
y x
1 1
3 3
2 2
n
y x
y x

) (
2
1
. .
. .
2
1
mod 1 mod 1
1
i n i n i
i
i
y x y x A
+ +
=
= =

. .
n n
y x
is_convex;point_in_polygon;
cut_polygon
Explanationviaillustration
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Convex Hull Problem ConvexHullProblem
TheConvexHullofasetofpointsPisthesmallest
convexpolygonCH(P)forwhicheachpointinPis
eitherontheboundaryofCH(P)orinitsinterior
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Graham Scans Algorithm for CH GrahamScan sAlgorithmforCH
1. Findpivot(bottommost,rightmostpoint)
2. Angularsortingw.r.t pivot(easywithlibrary)
3. Seriesofccw tests(withhelpofstack)
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Geometry Basics 3D GeometryBasics 3D
3DObjects:
Sphere(veryrarelyused)
Greatcircledistance
Seebookforformulathisisnotthatpopular
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Not Yet Covered This Sem NotYetCoveredThisSem
ThefamousPlaneSweepParadigm
Event,priority_queue
IntersectionProblems
1D 1D;2D 2D;3D 3D
DivideandConquerinGeometry
The closest pair problem Theclosestpairproblem
Bisectionmethodingeometryproblem
Geometric data structure (Segment/Fenwick Tree done Geometricdatastructure(Segment/FenwickTreedone,
OtherDSlikeQuadTree:notyet)
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Вам также может понравиться