Presentation is loading. Please wait.

Presentation is loading. Please wait.

Geometry.

Similar presentations


Presentation on theme: "Geometry."β€” Presentation transcript:

1 Geometry

2 Geometric Problems Usually refers to problems involving points, lines, polygons Will focus on 2D problems here. Some 3D extensions are straightforward General characteristics Geometric algorithms usually built on simpler geometric structures/algorithms Often there are many special cases Often need to be sure to account for floating-point error

3 Points Points in 2D: For point p, just a pair of values: px and py
Store as a struct, class, or pair Distance between points p and q: 𝑝 π‘₯ βˆ’ π‘ž π‘₯ 𝑝 𝑦 βˆ’ π‘ž 𝑦 2

4 Lines and Line Segments
3 main representations: Implicit, Pair of Points, Point and Ray Implicit form: ax + by + c = 0 If b=1, this is equivalent to y=mx+b. But, it’s also capable of representing vertical lines (such as x=1). Store as 3 values: a, b, c Represents the infinite line To find a point on the line, we must solve for a value, e.g. by setting x or y to a specific value and solving for the other. Some of the information, however, is redundant – an infinite line should need only 2 parameters, but this requires 3

5 Lines and Line Segments
3 main representations: Implicit, Pair of Points, Point and Ray Pair of points: p and q Store as a structure with 2 points Thus, 4 total degrees of freedom: 2 for the line, and 1 for each endpoint Can represent a line segment, not just an infinite line Implies a direction: p to q (as opposed to q to p) A common way that lines are specified as input – two known points and we want the line (shortest path) between them But, on its own, not the most useful form

6 Lines and Line Segments
3 main representations: Implicit, Pair of Points, Point and Ray Point and Ray A point p, and a direction vector (ray) v Both p and v can be stored as points (the vector has an x and a y component, just like a point) Can obtain from a pair of points: v = q-p That is, vx = qx-px, vy=qy-py Gives a parameterization of the line: p+vs s in [0,1] is the original line segment s < 0 are points before p, s > 1 are points after q

7 Intersecting Lines To intersect lines, need to solve for common point
Implicit form of line: see book Parametric form of lines: Assume lines L1 and L2, defined by p1+v1s and p2+v2t i.e. parameterized by s and t So, we must have p1x+v1xs = p2x+v2xt and p1y+v1ys = p2y+v2yt Can solve for s and t: 𝑠= 𝑝 1𝑦 𝑣 2π‘₯ + 𝑝 2π‘₯ 𝑣 2𝑦 βˆ’ 𝑝 1π‘₯ 𝑣 2𝑦 βˆ’ 𝑝 2𝑦 𝑣 2π‘₯ 𝑣 1π‘₯ 𝑣 2𝑦 βˆ’ 𝑣 1𝑦 𝑣 2π‘₯ 𝑑= 𝑝 1𝑦 𝑣 1π‘₯ + 𝑝 2π‘₯ 𝑣 1𝑦 βˆ’ 𝑝 1π‘₯ 𝑣 1𝑦 βˆ’ 𝑝 2𝑦 𝑣 1π‘₯ 𝑣 1π‘₯ 𝑣 2𝑦 βˆ’ 𝑣 1𝑦 𝑣 2π‘₯ Notice: parallel (no intersection) when denominators are 0 Once we solve for s and/or t, can plug values in to find point of intersection But, often, all we need are the s and t values

8 Vectors Vector has x and y components: vx, vy Vectors have Direction
Direction can be thought of as the angle from horizontal tan πœƒ = 𝑣 𝑦 𝑣 π‘₯ Note: Use atan2(numerator,denominator) function in C++ to get full range of angles Vectors have Magnitude 𝑣 = 𝑣 π‘₯ 2 + 𝑣 𝑦 2

9 Dot Product Given two vectors, v and w, the dot product is defined as vxwx+vywy The dot product is also equal to 𝑣 𝑀 cos πœƒ Where q is the angle between the two vectors Can be used to project one vector along another Computes portion of one vector in direction of another w q v |w|cosq / |v|

10 Distance between point and a line
Assume line is point-ray form: p and v are given. Want distance to point q. Form vector from p to q – call it w. Compute v’ = v/|v| (a unit-length vector in the same direction as v) Compute dot product of w and v’ (call this u) This is the amount of w projected in the direction of v The point a = p+uv’ is the nearest point on the line to the point q So, just compute distance from a to q. This is the same as the magnitude of w-uv’ If we want distance to line segment, then use only if 0<=u<=1 Otherwise, we are closer to an endpoint of the line segment

11 Cross product and sidedness test
Defined in 3D, but can be used in 2D to do a sidedness test Cross product of v and w is vxwy-vywx This is the magnitude of the perpendicular vector in 3D If Cross product is positive, then w is to the left of v If Cross product is negative, then w is to the right of v This is often called a sidedness test: Given points a, b, c Form vector v from a to b (v=b-a) Form vector w from a to c (w=c-a) (or can form w from b to c) Determine sign of cross product between v and w Determines if c is on the left or right of the line from a to b

12 Cross product and area The magnitude of the cross product of v and w is 𝑣 𝑀 sin πœƒ This is the area of a parallelogram formed by v and w So, the area of the triangle with edges v and w is Β½ the magnitude of the cross product. Positive if w is to the left of v, negative if to the right w q v

13 Area of a Polygon Order vertices in counterclockwise order.
Take every pair of vertices (vi and vi+1) and compute vi,xvi+1,y-vi,yvi+1,x Sum these values up Then, divide by 2 This is the area of the polygon What is really going on: we are forming triangles from the origin and the two vertices. Then, we compute the area of the triangle using the cross product.

14 Point inside polygon test
Order vertices in counterclockwise order. Take all pairs of vertices (including first and last) Compute the angle from the query point q to each of the 2 vertices Add the angle if it’s a left-hand side when going from vertex i to i+1 Subtract if it’s a right-side turn If the point is inside the polygon, the angles will sum to 360 degrees (2*PI radians) Be sure to allow for numerical error – it is unlikely to be exactly 2*PI radians If outside, they will sum to 0

15 Convex Hull Given a set of points, find the set of points that contain all other points Think of it as wrapping a rubber band around all points, and letting go – the points the band is touching will be the ones on the convex hull.

16 Graham Scan for computing Convex Hull
Several algorithms for computing Convex Hull Graham Scan is one of the easiest to implement Find the lowest point (smallest y value – if tie, choose leftmost = smallest x) This point must be on the convex hull (any extreme point in some direction will be) Order all other points in order by angle from this point Then, add points from smallest to largest angle Check last 3 points added – if left side (left hand turn), keep point Otherwise, remove previous point, and retest with remainder

17 Find lowest point, and order points in order

18 Add lowest point (must be on convex hull)

19 Add next point and check if left turn It is, so keep the point

20 Add next point and check if left turn It is not, so we will delete previous

21 Check the last 3 – they now do form a left turn

22 Add another point and check for left turn It is left, so keep the point

23 Add another point and check for left turn It is not, so we’ll remove previous

24 Check again for a left hand turn It is not, so we’ll remove previous

25 Now the last 3 points form a left hand turn

26 This will continue until the full convex hull is found


Download ppt "Geometry."

Similar presentations


Ads by Google