CS133 Computational Geometry Computational Geometry Primitives 1
Point Representation A point in the 2D Cartesian space is represented as a vector from the origin to the point π = π¦, π§ p p y O x 2
Line Segment Representation A line segment is represented by its two end points Length= π β π Straight lines are usually represented by two points on it p1 p2 3
Application: CCW sort How to sort a list of points in a CCW order around the origin? NaΓ―ve solution: Calculate all the angles and sort Advantages: Easy and can reuse an existing sort algorithm as-is Disadvantages: arctan calculation is complex and might be inaccurate 4
CCW Sort What is the cross/dot product of the vectors of two points? 5
CCW Comparator π 2 π 1 A point in π 2 or π 3 precedes a point in π 4 or π 1 Two points in the Two points with same half are the same angle ordered based on are assumed to their angle be equal π 3 π 4 6
-1: π 1 precedes π 2 CCW Comparator +1: π 2 precedes π 1 0: On the same angle compare( π 1 = π¦ 1 , π§ 1 , π 2 = (π¦ 2 , π§ 2 ) ) If ( π¦ 1 < 0 and π¦ 2 > 0 ) OR ( π¦ 1 > 0 and π¦ 2 < 0 ) Return π¦ 1 < 0 ? -1 : +1 cp = π 1 Γ π 2 If ππ < 0 Return -1 If ππ > 0 Return +1 // cp = 0 If ( π§1 < 0 and π§ 2 > 0 ) OR ( π§1 > 0 and π§2 < 0 ) Return π§ 1 < 0 ?-1 : +1 Return 0 7
CCW Sort How to sort a set of points around their geometric center? First, compute the geometric center Then, translate the points to make the origin at the center 8
Collinearity Given three points, check if they are on the same straight line π 2 Collinear( π 1 , π 2 , π ) Return π 1 π 2 Γ π 1 π == 0 π π π 1 9
Missing Square Problem Hint: Test the collinearity of three points on the figure 10
Direction Given a straight line (ray) and a point, find whether the point is to the right or left of the line π Direction( π 1 , π 2 , π ) ππ = π 1 π 2 Γ π 1 π π 2 π If ππ < 0 Return βrightβ π π 1 If ππ > 0 Return βleftβ Return βon -the- lineβ 11
Point-line Relationship Given a line segment and a point, find whether the point is on the line segment or not Coincident( π 1 (π¦ 1 , π§ 1 ) , π 2 (π¦ 2 , π§ 2 ) , π(π¦, π§) ) If NOT Collinear( π 1 , π 2 , π ) Return false Return π¦ β min π¦ 1 , π¦ 2 , max π¦ 1 , π¦ 2 AND ( π§ β min π§ 1 , π§ 2 , max π§ 1 , π§ 2 12
Line-line Relationship Given two straight lines, find whether they intersect or not IsIntersected( π 1 , π 2 , π 3 , π 4 ) If π 1 π 2 Γ π 3 π 4 β 0 Return true // intersected in a point // The two lines are parallel If Collinear( π 1 , π 2 , π 3 ) Return true // Lines are coincident Return false // Parallel ant not coincident 13
Line-line Intersection Given two straight lines, find their intersection Solve the two linear equations that represent the two lines One solution β The lines intersect in a point Infinite solutions β The lines are coincident No solutions β The lines are disjoint parallel 14
Line-line Intersection π 1 , π 2 : First line (input) π 3 , π 4 : Second line (input) π 0 : Intersection point (output) π 0 must be collinear with π 1 π 2 and π 3 π 4 π 1 π 2 Γ π 1 π 0 = 0 π 3 π 4 Γ π 3 π 0 = 0 π 2 π 3 π 0 See the rest of the π 4 derivation in the notes π 1 15
Line Segment Intersection Given two line segments, find whether they intersect or not. If they intersect, find their intersection point (Homework) 16
Recommend
More recommend