70 likes | 167 Views
Explore testing methods for 2D intersections with known and unknown geometries, affine transforms, and line equations. Understand the process of finding intersection points of line segments using mathematical algorithms.
E N D
Lecture 04:Intersection January 29, 2013 COMP 150-2Visualization
Admin • Citing other people’s work • Online references • Grading • Note: the “posting online” requirement will be judged during the time of the demo. If the page doesn’t load, the student will not receive credit. There will be no “real time debugging”! • Grading Rubric • frame.setResizable(true); • Caveat – does not work in a browser!!
Testing for 2D Intersections • With known geometry (e.g. with a circle, or with a square) • With known geometry, but rotated/translated/scaled/sheared (affine transforms) • Transformation stack – inverse transform • Isomorphic shapes (applies to invertible deformation) • With geometry made up of defined line segments • With unknown geometry
Line-Line Intersection • Given two points P(Px, Py), Q(Qx, Qy) • Solve for the line equation: • ax + by = c • Start by finding y = mx + d • Note that m is the slope… • Plug in the numbers, solve for m and d • Rearrange to find a, b, c • General form: • (py – qy)x + (qx – px)y = (qxpy - pxqy)
Line-Line Intersection • Given ax + by = c for two line segments, find it they intersect • a1x + b1y = c1 • a2x + b2y = c2 • Multiply the top equation by b2, bottom by b1 • a1b2 x + b1b2 y = c1b2 • a2b1 x + b1b2 y = c2b1 • Subtract bottom by top • (a1b2 –a2b1)x = (c1b2 –c2b1) • X = (c1b2 – c2b1) / (a1b2 – a2b1); • Multiply the top equation by a2, bottom by a1 • a1a2 x + b1a2 y = c1a2 • a1a2 x + b2a1 y = c2a1 • Same as above, get • Y = (c1a2 – c2a1) / (b1a2 – b2a1), //which is the same as • Y = (c2a1 – c1a2) / (b2a1 – b1a2); //rearrange the det • Y = (c2a1 – c1a2) / (a1b2 – a2b1);
Line-Line Intersection double det = A1*B2 - A2*B1 if(det== 0) { //Lines are parallel } else { double x = (B2*C1 - B1*C2)/det double y = (A1*C2 - A2*C1)/det } //need to check for the range if doing line-segment to line-segment test