Geometry Algorithms

Embed Size (px)

Citation preview

  • 7/25/2019 Geometry Algorithms

    1/142

    Geometry

    Tmas Ken MagnssonBjarki gst Gumundsson

    School of Computer Science

    Reykjavk University

    rangursrk forritun og lausn verkefna

    http://ru.is/http://ru.is/td
  • 7/25/2019 Geometry Algorithms

    2/142

    Today were going to cover

    Geometry Computational geometry

    2

  • 7/25/2019 Geometry Algorithms

    3/142

    Points and vectors

    3

  • 7/25/2019 Geometry Algorithms

    4/142

    Points and vectors

    (3, 6)

    (6, 3)

    (1, 3)

    Points are representedby a pair of numbers,(x,y).

    3

  • 7/25/2019 Geometry Algorithms

    5/142

    Points and vectors

    (3, 6)

    (6, 3)

    (1, 3)

    Points are representedby a pair of numbers,(x,y).

    Vectors are representedin the same way.

    Thinking of points asvectors us to do manythings.

    3

  • 7/25/2019 Geometry Algorithms

    6/142

    Points and vectors

    uv

    4

  • 7/25/2019 Geometry Algorithms

    7/142

    Points and vectors

    uv

    Simplest operation,addition is dened asx0y0

    +

    x1y1

    =

    x0+x1y0+y1

    4

  • 7/25/2019 Geometry Algorithms

    8/142

    Points and vectors

    uv

    u + v

    Simplest operation,addition is dened asx0y0

    +

    x1y1

    =

    x0+x1y0+y1

    4

  • 7/25/2019 Geometry Algorithms

    9/142

    Points and vectors

    uv

    Simplest operation,addition is dened asx0y0

    +

    x1y1

    =

    x0+x1y0+y1

    Subtraction is dened inthe same manner

    x0y0x

    1y1

    =x

    0x

    1y0 y1

    4

  • 7/25/2019 Geometry Algorithms

    10/142

    Points and vectors

    uv

    u v

    Simplest operation,addition is dened asx0y0

    +

    x1y1

    =

    x0+x1y0+y1

    Subtraction is dened inthe same manner

    x0y0x

    1y1

    =x

    0x

    1y0 y1

    4

  • 7/25/2019 Geometry Algorithms

    11/142

    Points and vectors

    struct point {

    double x, y;point(double _x, double _y) {

    x = _x, y = _y;

    }

    point operator+(const point &oth){

    return point(x + oth.x, y + oth.y);

    }

    point operator-(const point &oth){return point(x - oth.x, y - oth.y);

    }

    };

    5

  • 7/25/2019 Geometry Algorithms

    12/142

    Points and vectors

    or we could use thecomplexclass.typedef complex point;

    6

  • 7/25/2019 Geometry Algorithms

    13/142

    Points and vectors

    or we could use thecomplexclass.typedef complex point;

    Thecomplexclass inC++and Javahas methods denedfor Addition Subtraction Multiplication by a scalar

    Length Trigonometric functions

    6

  • 7/25/2019 Geometry Algorithms

    14/142

    Points and vectors

    Complex numbers have the real part and the imaginarypart. Can be thought of as vectors or points on thecomplex plane.

    7

  • 7/25/2019 Geometry Algorithms

    15/142

    Points and vectors

    Complex numbers have the real part and the imaginarypart. Can be thought of as vectors or points on thecomplex plane. double real(p)returns the real part, or in our case,

    thexvalue ofp

    7

  • 7/25/2019 Geometry Algorithms

    16/142

    Points and vectors

    Complex numbers have the real part and the imaginarypart. Can be thought of as vectors or points on thecomplex plane. double real(p)returns the real part, or in our case,

    thexvalue ofp double imag(p)returns the imaginary part,yvalue of

    p.

    7

  • 7/25/2019 Geometry Algorithms

    17/142

    Points and vectors

    Complex numbers have the real part and the imaginarypart. Can be thought of as vectors or points on thecomplex plane. double real(p)returns the real part, or in our case,

    thexvalue ofp double imag(p)returns the imaginary part,yvalue of

    p. double abs(p)returns the absolute value of the

    complex number, the length of the vector.

    7

  • 7/25/2019 Geometry Algorithms

    18/142

    Points and vectors

    Complex numbers have the real part and the imaginarypart. Can be thought of as vectors or points on thecomplex plane. double real(p)returns the real part, or in our case,

    thexvalue ofp double imag(p)returns the imaginary part,yvalue of

    p. double abs(p)returns the absolute value of the

    complex number, the length of the vector. double sin(p),double cos(p),double tan(p),

    trigonometric functions.

    7

  • 7/25/2019 Geometry Algorithms

    19/142

    Lines and line segments

    Line segments arerepresented by a pair ofpoints,((x0,y0), (x1,y1)).

    8

    Li d li

  • 7/25/2019 Geometry Algorithms

    20/142

    Lines and line segments

    p0

    p1p1 p0

    Line segments arerepresented by a pair ofpoints,((x0,y0), (x1,y1)).

    Distance between twopoints is the length of the

    line segment or vectorbetween the points.

    8

    Li d li t

  • 7/25/2019 Geometry Algorithms

    21/142

    Lines and line segments

    p1 p0

    Line segments arerepresented by a pair ofpoints,((x0,y0), (x1,y1)).

    Distance between twopoints is the length of the

    line segment or vectorbetween the points.

    d((x0,y0), (x1,y1))

    = |(x1 x0,y1 y0)|=

    (x1 x0)2 + (y1 y0)2

    8

    Li d li t

  • 7/25/2019 Geometry Algorithms

    22/142

    Lines and line segments

    struct point {

    ...

    double distance(point oth = point(0,0)) const {

    return sqrt(pow(x - oth.x, 2.0)

    + pow(y - oth.y, 2.0));}

    ...

    }

    9

    Li d li t

  • 7/25/2019 Geometry Algorithms

    23/142

    Lines and line segments

    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 theabsfunction withcomplex.

    9

    Lines and line segments

  • 7/25/2019 Geometry Algorithms

    24/142

    Lines and line segments

    Line representation same asline segments.

    10

    Lines and line segments

  • 7/25/2019 Geometry Algorithms

    25/142

    Lines and line segments

    q1

    p1

    q0

    p0

    Line representation same asline segments.

    Treat them as lines passingthrough the two points.

    10

    Lines and line segments

  • 7/25/2019 Geometry Algorithms

    26/142

    Lines and line segments

    p1

    p0

    r0

    r1

    Line representation same asline segments.

    Treat them as lines passingthrough the two points.

    Or as a point and a directionvector.

    p+t r

    10

    Lines and line segments

  • 7/25/2019 Geometry Algorithms

    27/142

    Lines and line segments

    p1

    p0

    r0

    r1

    Line representation same asline segments.

    Treat them as lines passingthrough the two points.

    Or as a point and a directionvector.

    p+t r

    Either way

    pair

    10

    Circles

  • 7/25/2019 Geometry Algorithms

    28/142

    Circles

    Circles are very easy torepresent.

    11

    Circles

  • 7/25/2019 Geometry Algorithms

    29/142

    Circles

    p0

    p1

    Circles are very easy torepresent.

    Center pointp= (x,y).

    11

    Circles

  • 7/25/2019 Geometry Algorithms

    30/142

    Circles

    p0

    p1

    r0r1

    Circles are very easy torepresent.

    Center pointp= (x,y).

    And the radiusr.

    11

    Circles

  • 7/25/2019 Geometry Algorithms

    31/142

    Circles

    p0

    p1

    r0r1

    Circles are very easy torepresent.

    Center pointp= (x,y).

    And the radiusr.pair

    11

    Dot product

  • 7/25/2019 Geometry Algorithms

    32/142

    Dot product

    Given two vectors

    u=

    x0y0

    v=

    x1y1

    the dot product of uand vis dened as

    x0y0

    x1y1

    =x0 x1+y0 y1

    12

    Dot product

  • 7/25/2019 Geometry Algorithms

    33/142

    Dot product

    Given two vectors

    u=

    x0y0

    v=

    x1y1

    the dot product of uand vis dened as

    x0y0

    x1y1

    =x0 x1+y0 y1

    Which in geometric terms isu v= |u||v| cos

    12

    Dot product

  • 7/25/2019 Geometry Algorithms

    34/142

    Dot product

    u

    v

    Allows us to calculate the

    angle between uand v.

    =arccos

    u v

    |u||v|

    13

    Dot product

  • 7/25/2019 Geometry Algorithms

    35/142

    Dot product

    u

    v

    vu

    Allows us to calculate the

    angle between uand v.

    =arccos

    u v

    |u||v|

    And the projection of vonto u.

    vu=

    u v

    |u|

    u

    13

    Dot product

  • 7/25/2019 Geometry Algorithms

    36/142

    Dot product

    u

    v

    vu

    p

    q

    The closest point on uto pisq.

    13

    Dot product

  • 7/25/2019 Geometry Algorithms

    37/142

    Dot product

    u

    v

    vu

    p

    q

    The closest point on uto pisq.

    The distance frompto uis thedistance fromptoq.

    13

    Dot product

  • 7/25/2019 Geometry Algorithms

    38/142

    p

    u

    v

    vu

    p

    q

    The closest point on uto pisq.

    The distance frompto uis thedistance fromptoq.

    Unlessqis outside u, then theclosest point is either of theendpoints.

    13

    Dot product

  • 7/25/2019 Geometry Algorithms

    39/142

    p

    Rest of the code will use the complexclass.#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

  • 7/25/2019 Geometry Algorithms

    40/142

    p

    Given two vectors

    u=

    x0y0

    v=

    x1y1

    the cross product of uand vis dened as

    x0y0

    x1y1

    =x0 y1 y0 x1

    15

    Cross product

  • 7/25/2019 Geometry Algorithms

    41/142

    p

    Given two vectors

    u=

    x0y0

    v=

    x1y1

    the cross product of uand vis dened as

    x0y0

    x1y1

    =x0 y1 y0 x1

    Which in geometric terms is|u v| = |u||v| sin

    15

    Cross product

  • 7/25/2019 Geometry Algorithms

    42/142

    p

    u

    v

    Allows us to calculate the area

    of the triangle formed by uand v.

    |u v|

    2

    16

    Cross product

  • 7/25/2019 Geometry Algorithms

    43/142

    u

    v

    12|u v|

    u

    v

    Allows us to calculate the area

    of the triangle formed by uand v.

    |u v|

    2

    16

    Cross product

  • 7/25/2019 Geometry Algorithms

    44/142

    u

    v

    12|u v|

    u

    v

    Allows us to calculate the area

    of the triangle formed by uand v.

    |u v|

    2

    And can tell us if the anglebetweenuand vis positive ornegative.

    |u v| 0 i >

    16

    Counterclockwise

  • 7/25/2019 Geometry Algorithms

    45/142

    A

    B

    C

    v

    u

    Given three pointsA,Band C, we

    want to know if they form acounter-clockwise angle in thatorder.

    A B C

    17

    Counterclockwise

  • 7/25/2019 Geometry Algorithms

    46/142

    A

    B

    C

    v

    u

    +

    Given three pointsA,Band C, we

    want to know if they form acounter-clockwise angle in thatorder.

    A B C

    We can examine the cross productof and the area of the triangleformed by

    u=B C

    v= B A

    u v>0

    17

    Counterclockwise

  • 7/25/2019 Geometry Algorithms

    47/142

    A

    B

    C

    u

    v

    The points in the reverse order do

    not form a counter clockwise angle.

    C B A

    In the reverse order the vectorsswap places

    u=B A v= B C

    u v

  • 7/25/2019 Geometry Algorithms

    48/142

    v

    u

    A

    B

    C

    The points in the reverse order do

    not form a counter clockwise angle.

    C B A

    In the reverse order the vectorsswap places

    u=B A v= B C

    u v

  • 7/25/2019 Geometry Algorithms

    49/142

    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

  • 7/25/2019 Geometry Algorithms

    50/142

    Very common task is to nd the intersection of two lines orline segments.

    19

    Intersections

  • 7/25/2019 Geometry Algorithms

    51/142

    Very common task is to nd the intersection of two lines orline segments. Given a pair of points(x0,y0),(x1,y1), representing a

    line we want to start by obtaining the formAx+By= C.

    19

    Intersections

  • 7/25/2019 Geometry Algorithms

    52/142

    Very common task is to nd the intersection of two lines orline segments. Given a pair of points(x0,y0),(x1,y1), representing a

    line we want to start by obtaining the formAx+By= C.

    We can do so by setting

    A=y1 y0

    B=x0 x1

    C=A x0+B y1

    19

    Intersections

  • 7/25/2019 Geometry Algorithms

    53/142

    Very common task is to nd the intersection of two lines orline segments. Given a pair of points(x0,y0),(x1,y1), representing a

    line we want to start by obtaining the formAx+By= C.

    We can do so by setting

    A=y1 y0

    B=x0 x1

    C=A x0+B y1

    If we have two lines given by such equations, wesimply need to solve for the two unknowns,xandy.

    19

    Intersections

  • 7/25/2019 Geometry Algorithms

    54/142

    For two lines

    A0x+B0y=C0A1x+B1y=C1

    The intersection point is

    x=(B1 C0 B0 C1)

    D

    y=(A0 C1 A1 C0)

    D

    WhereD=A0 B1 A1 B0

    20

    IntersectionsQ l bl d h f

  • 7/25/2019 Geometry Algorithms

    55/142

    Quite similar problem is to nd the intersections of twocircles.

    rA rB

    A Bd

    21

    IntersectionsQ it i il bl i t d th i t ti f t

  • 7/25/2019 Geometry Algorithms

    56/142

    Quite similar problem is to nd the intersections of twocircles.

    rA rB

    A Bd

    Ifd> r0+r1the circles donot intersect.

    21

    IntersectionsQ it i il bl i t d th i t ti f t

  • 7/25/2019 Geometry Algorithms

    57/142

    Quite similar problem is to nd the intersections of twocircles.

    rA rB

    A Bd

    Ifd> r0+r1the circles donot intersect.

    Ifd< |r0 r1|, one circles iscontained within the other.

    21

    IntersectionsQ ite similar problem is to nd the intersections of t o

  • 7/25/2019 Geometry Algorithms

    58/142

    Quite similar problem is to nd the intersections of twocircles.

    rA rB

    A Bd

    Ifd> r0+r1the circles donot intersect.

    Ifd< |r0 r1|, one circles iscontained within the other.

    Ifd= 0and r0=r1, thecircles are the same.

    21

    IntersectionsQuite similar problem is to nd the intersections of two

  • 7/25/2019 Geometry Algorithms

    59/142

    Quite similar problem is to nd the intersections of twocircles.

    rA rB

    A Bd

    Ifd> r0+r1the circles donot intersect.

    Ifd< |r0 r1|, one circles iscontained within the other.

    Ifd= 0and r0=r1, thecircles are the same.

    Lets look at the last case.

    21

    IntersectionsQuite similar problem is to nd the intersections of two

  • 7/25/2019 Geometry Algorithms

    60/142

    Quite similar problem is to nd the intersections of twocircles.

    rA rB

    A B

    h

    a b

    We can solve for the vectorsaand hfrom the equations

    a2 +h2 =r20 b2 +h2 =r21

    21

    IntersectionsQuite similar problem is to nd the intersections of two

  • 7/25/2019 Geometry Algorithms

    61/142

    Quite similar problem is to nd the intersections of twocircles.

    rA rB

    A B

    h

    a b

    We can solve for the vectorsaand hfrom the equations

    a2 +h2 =r20 b2 +h2 =r21

    We get

    a= r2A r

    2

    B+d2)

    2 dh2 =r2A a

    2

    21

    Intersections

  • 7/25/2019 Geometry Algorithms

    62/142

    #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

  • 7/25/2019 Geometry Algorithms

    63/142

    Polygons are represented by

    a list of points in the orderrepresenting the edges.

    23

    Polygons

  • 7/25/2019 Geometry Algorithms

    64/142

    Polygons are represented by

    a list of points in the orderrepresenting the edges. To calculate the area

    23

    Polygons

  • 7/25/2019 Geometry Algorithms

    65/142

    Polygons are represented by

    a list of points in the orderrepresenting the edges. To calculate the area

    We pick one starting point.

    23

    Polygons

  • 7/25/2019 Geometry Algorithms

    66/142

    Polygons are represented by

    a list of points in the orderrepresenting the edges. To calculate the area

    We pick one starting point. Go through all the other

    adjacent pair of points and sumthe area of the triangulation.

    23

    Polygons

  • 7/25/2019 Geometry Algorithms

    67/142

    Polygons are represented by

    a list of points in the orderrepresenting the edges. To calculate the area

    We pick one starting point. Go through all the other

    adjacent pair of points and sumthe area of the triangulation.

    23

    Polygons

  • 7/25/2019 Geometry Algorithms

    68/142

    Polygons are represented by

    a list of points in the orderrepresenting the edges. To calculate the area

    We pick one starting point. Go through all the other

    adjacent pair of points and sumthe area of the triangulation.

    23

    Polygons

  • 7/25/2019 Geometry Algorithms

    69/142

    Polygons are represented by

    a list of points in the orderrepresenting the edges. To calculate the area

    We pick one starting point. Go through all the other

    adjacent pair of points and sumthe area of the triangulation.

    23

    Polygons

  • 7/25/2019 Geometry Algorithms

    70/142

    Polygons are represented by

    a list of points in the orderrepresenting the edges. To calculate the area

    We pick one starting point. Go through all the other

    adjacent pair of points and sumthe area of the triangulation.

    Even if we sum up area outsidethe polygon, due to the crossproduct, it is subtracted later.

    23

    Polygons

  • 7/25/2019 Geometry Algorithms

    71/142

    Polygons are represented by

    a list of points in the orderrepresenting the edges. To calculate the area

    We pick one starting point. Go through all the other

    adjacent pair of points and sumthe area of the triangulation.

    Even if we sum up area outsidethe polygon, due to the crossproduct, it is subtracted later.

    23

    Polygons

  • 7/25/2019 Geometry Algorithms

    72/142

    double polygon_area_signed(const vector &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 &p) {

    return abs(polygon_area_signed(p));

    }

    24

    Convex hull Given a set of points, we want to nd the convex hull

  • 7/25/2019 Geometry Algorithms

    73/142

    G e a set o po ts, e a t to d t e co e uof the points.

    25

    Convex hull Given a set of points, we want to nd the convex hull

  • 7/25/2019 Geometry Algorithms

    74/142

    p ,of the points.

    The convex hull of points can be visualized as theshape formed by a rubber band around the set ofpoints.

    25

    Convex hull Given a set of points, we want to nd the convex hull

  • 7/25/2019 Geometry Algorithms

    75/142

    p ,of the points.

    The convex hull of points can be visualized as theshape formed by a rubber band around the set ofpoints.

    25

    Convex hull Given a set of points, we want to nd the convex hull

  • 7/25/2019 Geometry Algorithms

    76/142

    pof the points.

    The convex hull of points can be visualized as theshape formed by a rubber band around the set ofpoints.

    25

    Convex hull

  • 7/25/2019 Geometry Algorithms

    77/142

    Graham scan:

    26

    Convex hull

  • 7/25/2019 Geometry Algorithms

    78/142

    Graham scan: Pick the pointp0with the lowestycoordinate.

    26

    Convex hull

  • 7/25/2019 Geometry Algorithms

    79/142

    Graham scan: Pick the pointp0with the lowestycoordinate. Sort all the points by polar angle withp0.

    26

    Convex hull

  • 7/25/2019 Geometry Algorithms

    80/142

    Graham scan: Pick the pointp0with the lowestycoordinate. Sort all the points by polar angle withp0. Iterate through all the points

    26

    Convex hull

  • 7/25/2019 Geometry Algorithms

    81/142

    Graham scan: Pick the pointp0with the lowestycoordinate. Sort all the points by polar angle withp0. 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

  • 7/25/2019 Geometry Algorithms

    82/142

    Graham scan: Pick the pointp0with the lowestycoordinate. Sort all the points by polar angle withp0. 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

  • 7/25/2019 Geometry Algorithms

    83/142

    Graham scan: Pick the pointp0with the lowestycoordinate. Sort all the points by polar angle withp0. 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 complexityO(Nlog N).

    26

    Convex hull

  • 7/25/2019 Geometry Algorithms

    84/142

    27

    Convex hull

  • 7/25/2019 Geometry Algorithms

    85/142

    27

    Convex hull

  • 7/25/2019 Geometry Algorithms

    86/142

    27

    Convex hull

    7 49

  • 7/25/2019 Geometry Algorithms

    87/142

    p01

    2

    3

    7

    5

    11

    86

    9

    1210

    27

    Convex hull

    7 49

  • 7/25/2019 Geometry Algorithms

    88/142

    p01

    2

    3

    7

    5

    11

    86

    9

    1210

    27

    Convex hull

    7 49

  • 7/25/2019 Geometry Algorithms

    89/142

    p01

    2

    3

    7

    5

    11

    86

    9

    1210

    27

    Convex hull

    7 49

  • 7/25/2019 Geometry Algorithms

    90/142

    p01

    2

    3

    7

    5

    11

    86

    9

    1210

    27

    Convex hull

    7 49

  • 7/25/2019 Geometry Algorithms

    91/142

    p01

    2

    3

    7

    5

    11

    86

    9

    1210

    27

    Convex hull

    7 49

  • 7/25/2019 Geometry Algorithms

    92/142

    p01

    2

    3

    7

    5

    11

    86

    9

    1210

    27

    Convex hull

    7 49

  • 7/25/2019 Geometry Algorithms

    93/142

    p01

    2

    3

    5

    11

    86

    1210

    27

    Convex hull

    7 49

  • 7/25/2019 Geometry Algorithms

    94/142

    p01

    2

    3

    5

    11

    86

    1210

    27

    Convex hull

    7 49

  • 7/25/2019 Geometry Algorithms

    95/142

    p01

    2

    3

    5

    11

    86

    1210

    27

    Convex hull

    7 49

  • 7/25/2019 Geometry Algorithms

    96/142

    p01

    2

    3

    5

    11

    86

    1210

    27

    Convex hull

    7 49

  • 7/25/2019 Geometry Algorithms

    97/142

    p01

    2

    3

    5

    11

    86

    1210

    27

    Convex hull

    7 49

  • 7/25/2019 Geometry Algorithms

    98/142

    p01

    2

    3

    5

    11

    86

    1210

    27

    Convex hull

    7 49

  • 7/25/2019 Geometry Algorithms

    99/142

    p01

    2

    3

    5

    11

    86

    1210

    27

    Convex hull

    7 49

  • 7/25/2019 Geometry Algorithms

    100/142

    p01

    2

    3

    5

    11

    86

    1210

    27

    Convex hullpoint hull[MAXN];

    int convex_hull(vector p) {

    int n = size(p), l = 0;

  • 7/25/2019 Geometry Algorithms

    101/142

    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

  • 7/25/2019 Geometry Algorithms

    102/142

    Many other algorithms exist

    29

    Convex hull

  • 7/25/2019 Geometry Algorithms

    103/142

    Many other algorithms exist Gift wrapping aka Jarvis march.

    29

    Convex hull

  • 7/25/2019 Geometry Algorithms

    104/142

    Many other algorithms exist Gift wrapping aka Jarvis march. Quick hull, similar idea to quicksort.

    29

    Convex hull

  • 7/25/2019 Geometry Algorithms

    105/142

    Many other algorithms exist Gift wrapping aka Jarvis march. Quick hull, similar idea to quicksort. Divide and conquer.

    29

    Convex hull

  • 7/25/2019 Geometry Algorithms

    106/142

    Many other algorithms exist Gift wrapping aka Jarvis march. Quick hull, similar idea to quicksort. Divide and conquer.

    Some can be extended to three dimensions, or higher.

    29

    Point in convex polygonSimple algorithm to check if a point is in a convex polygon.

  • 7/25/2019 Geometry Algorithms

    107/142

    30

    Point in convex polygonSimple algorithm to check if a point is in a convex polygon.

    We start by calculating the

  • 7/25/2019 Geometry Algorithms

    108/142

    We start by calculating thearea of the polygon.

    30

    Point in convex polygonSimple algorithm to check if a point is in a convex polygon.

    We start by calculating the

  • 7/25/2019 Geometry Algorithms

    109/142

    We start by calculating thearea of the polygon.

    To check if our point iscontained in the polygon wesum up the area of the

    triangles formed the pointand every two adjacentpoints.

    30

    Point in convex polygonSimple algorithm to check if a point is in a convex polygon.

    We start by calculating the

  • 7/25/2019 Geometry Algorithms

    110/142

    We start by calculating thearea of the polygon.

    To check if our point iscontained in the polygon wesum up the area of the

    triangles formed the pointand every two adjacentpoints.

    30

    Point in convex polygonSimple algorithm to check if a point is in a convex polygon.

    We start by calculating the

  • 7/25/2019 Geometry Algorithms

    111/142

    We start by calculating thearea of the polygon.

    To check if our point iscontained in the polygon wesum up the area of the

    triangles formed the pointand every two adjacentpoints.

    30

    Point in convex polygonSimple algorithm to check if a point is in a convex polygon.

    We start by calculating the

  • 7/25/2019 Geometry Algorithms

    112/142

    We start by calculating thearea of the polygon.

    To check if our point iscontained in the polygon wesum up the area of the

    triangles formed the pointand every two adjacentpoints.

    30

    Point in convex polygonSimple algorithm to check if a point is in a convex polygon.

    We start by calculating the

  • 7/25/2019 Geometry Algorithms

    113/142

    We start by calculating thearea of the polygon.

    To check if our point iscontained in the polygon wesum up the area of thetriangles formed the pointand every two adjacentpoints.

    30

    Point in convex polygonSimple algorithm to check if a point is in a convex polygon.

    We start by calculating the

  • 7/25/2019 Geometry Algorithms

    114/142

    We start by calculating thearea of the polygon.

    To check if our point iscontained in the polygon wesum up the area of thetriangles formed the pointand every two adjacentpoints.

    30

    Point in convex polygonSimple algorithm to check if a point is in a convex polygon.

    We start by calculating the

  • 7/25/2019 Geometry Algorithms

    115/142

    y garea of the polygon.

    To check if our point iscontained in the polygon wesum up the area of thetriangles formed the pointand every two adjacentpoints.

    30

    Point in convex polygonSimple algorithm to check if a point is in a convex polygon.

    We start by calculating the

  • 7/25/2019 Geometry Algorithms

    116/142

    y garea of the polygon.

    To check if our point iscontained in the polygon wesum up the area of thetriangles formed the pointand every two adjacentpoints.

    The total area of the trianglesis equal to the area of the

    polygon i the point is insidethe polygon.

    30

    Point in convex polygonSimple algorithm to check if a point is in a convex polygon.

    We start by calculating the

  • 7/25/2019 Geometry Algorithms

    117/142

    y garea of the polygon.

    To check if our point iscontained in the polygon wesum up the area of thetriangles formed the pointand every two adjacentpoints.

    The total area of the trianglesis equal to the area of the

    polygon i the point is insidethe polygon.

    30

    Point in convex polygonSimple algorithm to check if a point is in a convex polygon.

    We start by calculating the

  • 7/25/2019 Geometry Algorithms

    118/142

    area of the polygon.

    To check if our point iscontained in the polygon wesum up the area of thetriangles formed the pointand every two adjacentpoints.

    The total area of the trianglesis equal to the area of the

    polygon i the point is insidethe polygon.

    30

    Point in convex polygonSimple algorithm to check if a point is in a convex polygon.

    We start by calculating the

  • 7/25/2019 Geometry Algorithms

    119/142

    area of the polygon.

    To check if our point iscontained in the polygon wesum up the area of thetriangles formed the pointand every two adjacentpoints.

    The total area of the trianglesis equal to the area of the

    polygon i the point is insidethe polygon.

    30

    Point in convex polygonSimple algorithm to check if a point is in a convex polygon.

    We start by calculating the

  • 7/25/2019 Geometry Algorithms

    120/142

    area of the polygon. To check if our point is

    contained in the polygon wesum up the area of thetriangles formed the pointand every two adjacentpoints.

    The total area of the trianglesis equal to the area of the

    polygon i the point is insidethe polygon.

    30

    Point in convex polygonSimple algorithm to check if a point is in a convex polygon.

    We start by calculating the

  • 7/25/2019 Geometry Algorithms

    121/142

    area of the polygon. To check if our point is

    contained in the polygon wesum up the area of thetriangles formed the pointand every two adjacentpoints.

    The total area of the trianglesis equal to the area of the

    polygon i the point is insidethe polygon.

    30

    Point in convex polygonSimple algorithm to check if a point is in a convex polygon.

    We start by calculating the

  • 7/25/2019 Geometry Algorithms

    122/142

    area of the polygon. To check if our point is

    contained in the polygon wesum up the area of thetriangles formed the pointand every two adjacentpoints.

    The total area of the trianglesis equal to the area of the

    polygon i the point is insidethe polygon.

    30

    Point in convex polygonSimple algorithm to check if a point is in a convex polygon.

    We start by calculating the

  • 7/25/2019 Geometry Algorithms

    123/142

    area of the polygon. To check if our point is

    contained in the polygon wesum up the area of thetriangles formed the pointand every two adjacentpoints.

    The total area of the trianglesis equal to the area of the

    polygon i the point is insidethe polygon.

    30

    Point in concave polygon

  • 7/25/2019 Geometry Algorithms

    124/142

    How about non convex polygon?

    31

    Point in concave polygon

  • 7/25/2019 Geometry Algorithms

    125/142

    How about non convex polygon? Theeven-odd rulealgorithm.

    31

    Point in concave polygon

  • 7/25/2019 Geometry Algorithms

    126/142

    How about non convex polygon? Theeven-odd rulealgorithm. We examine a ray passing through the polygon to the

    point.

    31

    Point in concave polygon

    H b l ?

  • 7/25/2019 Geometry Algorithms

    127/142

    How about non convex polygon? Theeven-odd rulealgorithm. We examine a ray passing through the polygon to the

    point.

    If the ray crosses the boundary of the polygon, then italternately goes from outside to inside, and outside toinside.

    31

    Point in concave polygon

  • 7/25/2019 Geometry Algorithms

    128/142

    32

    Point in concave polygon

    Ray from the outside ofthe polygon to the point.

  • 7/25/2019 Geometry Algorithms

    129/142

    32

    Point in concave polygon

    Ray from the outside ofthe polygon to the point.

    C t th b f

  • 7/25/2019 Geometry Algorithms

    130/142

    Count the number ofintersection points.

    32

    Point in concave polygon

    Ray from the outside ofthe polygon to the point.

    C t th b f

  • 7/25/2019 Geometry Algorithms

    131/142

    Count the number ofintersection points.

    If odd, then the point isinside the polygon.

    If even, then the point isoutside the polygon.

    32

    Point in concave polygon

    Ray from the outside ofthe polygon to the point.

    Count the number of

  • 7/25/2019 Geometry Algorithms

    132/142

    Count the number ofintersection points.

    If odd, then the point isinside the polygon.

    If even, then the point isoutside the polygon.

    Does not matter whichray we pick.

    32

    Point in concave polygon Ray from the outside of

    the polygon to the point. Count the number of

  • 7/25/2019 Geometry Algorithms

    133/142

    Count the number ofintersection points.

    If odd, then the point isinside the polygon.

    If even, then the point isoutside the polygon.

    Does not matter whichray we pick.

    32

    Point in concave polygon Ray from the outside of

    the polygon to the point. Count the number of

  • 7/25/2019 Geometry Algorithms

    134/142

    Count the number ofintersection points.

    If odd, then the point isinside the polygon.

    If even, then the point isoutside the polygon.

    Does not matter whichray we pick.

    32

    Point in concave polygon Ray from the outside of

    the polygon to the point. Count the number of

  • 7/25/2019 Geometry Algorithms

    135/142

    Count the number ofintersection points.

    If odd, then the point isinside the polygon.

    If even, then the point isoutside the polygon.

    Does not matter whichray we pick.

    32

    Closest pair of points

    Given a set of points, we want to nd the pair of pointswith the smallest distance between them.

    Di id d l ith

  • 7/25/2019 Geometry Algorithms

    136/142

    Divide and conquer algorithm;

    33

    Closest pair of points

    Given a set of points, we want to nd the pair of pointswith the smallest distance between them.

    Divide and conquer algorithm;

  • 7/25/2019 Geometry Algorithms

    137/142

    Divide and conquer algorithm; Sort points by the x-coordinate.

    33

    Closest pair of points

    Given a set of points, we want to nd the pair of pointswith the smallest distance between them.

    Divide and conquer algorithm;

  • 7/25/2019 Geometry Algorithms

    138/142

    Divide and conquer algorithm; Sort points by the x-coordinate. Split the set into two equal sized sets by the vertical

    line of the medialxvalue.

    33

    Closest pair of points

    Given a set of points, we want to nd the pair of pointswith the smallest distance between them.

    Divide and conquer algorithm;

  • 7/25/2019 Geometry Algorithms

    139/142

    Divide and conquer algorithm; Sort points by the x-coordinate. Split the set into two equal sized sets by the vertical

    line of the medialxvalue. Solve the problem recursively in the left and right

    subset.

    33

    Closest pair of points

    Given a set of points, we want to nd the pair of pointswith the smallest distance between them.

    Divide and conquer algorithm;

  • 7/25/2019 Geometry Algorithms

    140/142

    Divide and conquer algorithm; Sort points by the x-coordinate. Split the set into two equal sized sets by the vertical

    line of the medialxvalue. Solve the problem recursively in the left and right

    subset. Sort the two subsets by they-coordinate.

    33

    Closest pair of points

    Given a set of points, we want to nd the pair of pointswith the smallest distance between them.

    Divide and conquer algorithm;

  • 7/25/2019 Geometry Algorithms

    141/142

    Divide and conquer algorithm; Sort points by the x-coordinate. Split the set into two equal sized sets by the vertical

    line of the medialxvalue. Solve the problem recursively in the left and right

    subset. Sort the two subsets by they-coordinate.

    Find the smallest distance among the pair of pointswhich lie on dierent sides of the line.

    33

    Closest pair of points

    Takk fyrir nnina!

    G i kk l k t !

  • 7/25/2019 Geometry Algorithms

    142/142

    Gangi ykkur vel og ga skemmtun prnu morgun!

    34