A little bit of geometry

Slides:



Advertisements
Similar presentations
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1 Emmanuel Agu.
Advertisements

3-5 Lines in the coordinate plane M11. B
CS 450: COMPUTER GRAPHICS FILLING POLYGONS SPRING 2015 DR. MICHAEL J. REALE.
1 Programming Interest Group Tutorial Eight Computational Geometry.
Copyright © Cengage Learning. All rights reserved.
Introduction to Programming (in C++) Numerical methods I Jordi Cortadella Dept. of Computer Science, UPC.
Geometric Algorithms1 segment intersection orientation point inclusion simple closed path.
CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading.
Solving Equations. Is a statement that two algebraic expressions are equal. EXAMPLES 3x – 5 = 7, x 2 – x – 6 = 0, and 4x = 4 To solve a equation in x.
Reasoning with invariants Jordi Cortadella Department of Computer Science.
Data types and their representation Jordi Cortadella Department of Computer Science.
Prime numbers Jordi Cortadella Department of Computer Science.
CS 325 Introduction to Computer Graphics 04 / 26 / 2010 Instructor: Michael Eckmann.
A little bit of geometry Jordi Cortadella Department of Computer Science.
2.8 Graphing Linear Inequalities in Two Variables
Polynomial Inequalities in Two Variables Section 3.3 **The only way to answer a problem that is an inequality in two variables is with a graph in the x-y.
Approximate computations Jordi Cortadella Department of Computer Science.
10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping.
7.1 R eview of Graphs and Slopes of Lines Standard form of a linear equation: The graph of any linear equation in two variables is a straight line. Note:
Vertices, Edges and Faces By Jordan Diamond. Vertices In geometry, a vertices is a special kind of point which describes the corners or intersections.
Rasterization Overview Raster Display Device. Scan Conversion / Rasterization: converting vector graphics into raster graphics (determining pixels in.
Computer Graphics Lecture 08 Taqdees A. Siddiqi Computer Graphics Filled Area Primitives I Lecture 08 Taqdees A. Siddiqi
Rasterization, or “What is glBegin(GL_LINES) really doing?” Course web page: February 23, 2012  Lecture 4.
Chapter 3 Graphs and Functions
Chapter 10 Conic Sections
Quick Graphs of Linear Equations
Approximate computations
Graphing Linear Equations
Quick Graphs of Linear Equations
Reasoning with invariants
Jordi Cortadella Department of Computer Science
Chapter 8 : Analytic Geometry
Computer Graphics CC416 Week 13 Clipping.
(c) 2002 University of Wisconsin, CS559
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1
m is the slope b is the y-intercept
Lines in the Coordinate Plane
University of New Mexico
Implementation III.
3D Graphics Rendering PPT By Ricardo Veguilla.
Query Processing in Databases Dr. M. Gavrilova
Approximate computations
Sec. 2.4 Library of Functions
Grids Geometry Computational Geometry
Grids Geometry Computational Geometry
Introduction to Computer Graphics with WebGL
© University of Wisconsin, CS559 Fall 2004
Grids Geometry Computational Geometry
Geometry.
Jordi Cortadella Department of Computer Science
Algebra Review.
PMC and Booleans Point-Membership Classification
Prof. Harriet Fell Fall 2011 Lecture 9 – September 26, 2011
Objectives Linear Equations and Inequalities
Equations of Straight Lines
Ray Tracing Polyhedra ©Anthony Steed
Computer Graphics Implementation III
2.3 Graph Equations of Lines
Point-slope Form of Equations of Straight Lines
Standard Form Section 5-5.
Millburn Academy Maths department Higher Equation of a Line y = mx + c.
Chapter 3 Graphics Output Primitives
العلاقات والدوال أ. ريما عباس ريض 152.
2.3 Quick Graphs of Linear Equations
m is the slope b is the y-intercept
Lines in the Plane and Slope
Geometry.
Implementation III Ed Angel Professor Emeritus of Computer Science
Algorithms and Data Structures Lecture XIV
Computational Geometry
Presentation transcript:

A little bit of geometry Jordi Cortadella Department of Computer Science

Representing points and lines in 𝑅 2 // A point has two coordinates struct Point { double x, y; }; // A line: y = mx + b struct Line { double m; // Slope double b; // y-intercept; }; // A segment represented by two points struct Segment { Point P, Q; }; Introduction to Programming © Dept. CS, UPC

Line equation from two points 𝑦=𝑚𝑥+𝑏 Q 𝑚= 𝑑𝑦 𝑑𝑥 = P.y−Q.y P.x−Q.x 𝒅𝒚 P 𝒅𝒙 𝒃 𝑏=P.y−𝑚∙P.x Introduction to Programming © Dept. CS, UPC

Finding a line from a segment // Returns the line defined by the segment. Line FindLine(const Segment& S) { Line L; L.m = (S.P.y – S.Q.y)/(S.P.x – S.Q.x); L.b = S.P.y – L.m*S.P.x; return L; } // Be careful with vertical lines! // A special treatment is required. Introduction to Programming © Dept. CS, UPC

Segment intersection Given two segments: do they intersect? Introduction to Programming © Dept. CS, UPC

Preliminary problem 𝐴 𝐵 Do the two points fall on the same side of the line? Introduction to Programming © Dept. CS, UPC

Relative position of a point ( 𝑥 2 , 𝑦 2 ) 𝑑𝑥 𝑑𝑦 𝐴( 𝑥 3 , 𝑦 3 ) 𝑦 3 − 𝑦 3 ′ 𝐿( 𝑥 3 , 𝑦 3 ′) ( 𝑥 1 , 𝑦 1 ) Is 𝐴 above, below or on the line? Compute the sign of 𝑦 3 − 𝑦 3 ′ Introduction to Programming © Dept. CS, UPC

Relative position of two points 𝑦 3 ′= 𝑑𝑦 𝑑𝑥 𝑥 3 + 𝑦 1 − 𝑑𝑦 𝑑𝑥 𝑥 1 𝑦 3 ′− 𝑦 3 = 𝑑𝑦 𝑑𝑥 𝑥 3 + 𝑦 1 − 𝑑𝑦 𝑑𝑥 𝑥 1 − 𝑦 3 Point A: 𝑑𝑥 𝑦 3 ′− 𝑦 3 =𝑑𝑦 𝑥 3 − 𝑥 1 −𝑑𝑥( 𝑦 3 − 𝑦 1 ) 𝑚 𝑏 Point B: 𝑑𝑥 𝑦 4 ′− 𝑦 4 =𝑑𝑦 𝑥 4 − 𝑥 1 −𝑑𝑥( 𝑦 4 − 𝑦 1 ) same sign? We are only interested on whether 𝑦 3 ′− 𝑦 3 and 𝑦 4 ′− 𝑦 4 have the same sign and not on the actual sign of the expressions. Introduction to Programming © Dept. CS, UPC

Points at the same side // Returns true if A and B at are the same side // of the line defined by S, and false otherwise. bool SameSide(const Segment& S, const Point& A, const Point& B) { double dx = S.P.x – S.Q.x; double dy = S.P.y – S.Q.y; double dxA = A.x – S.P.x; double dyA = A.y – S.P.y; double dxB = B.x – S.P.x; double dyB = B.y – S.P.y; return (dy*dxA – dx*dyA > 0) == (dy*dxB – dx*dyB > 0); // or also: (dy*dxA – dx*dyA)*(dy*dxB – dx*dyB) >= 0 } Important: the function works for any line (even vertical lines!). The expressions are perfectly symmetric with regard to the x and y axes. Note: we work with real numbers. Some inaccuracies may occur when the points are close to the segment. Introduction to Programming © Dept. CS, UPC

The original problem: segment intersection 𝑆 𝑉 𝑇 𝑅 // Returns true if S1 and S2 intersect, and false otherwise. bool Intersect(const Segment& S1, const Segment& S2); Introduction to Programming © Dept. CS, UPC

Segment intersection // Returns true if S1 and S2 intersect, // and false otherwise. bool Intersect(const Segment& S1, const Segment& S2) { return not (SameSide(S1, S2.P, S2.Q) or SameSide(S2, S1.P, S1.Q)); } Introduction to Programming © Dept. CS, UPC

Representation of polygons A polygon can be represented by a sequence of vertices. Two consecutive vertices represent an edge of the polygon. The last edge is represented by the first and last vertices of the sequence. We will consider that all polygons are simple (non-intersecting edges) (1,3) (4,1) (7,3) (5,4) (6,7) (2,6) Vertices: (1,3) (4,1) (7,3) (5,4) (6,7) (2,6) Edges: (1,3)-(4,1)-(7,3)-(5,4)-(6,7)-(2,6)-(1,3) // A polygon (an ordered set of vertices) using Polygon = vector<Point>; Introduction to Programming © Dept. CS, UPC

Why artificial vision is so difficult? Introduction to Programming © Dept. CS, UPC

Point inside a polygon? Human view Computer view 1.8 4.2 5.9 6.0 8.1 9.4 7.3 5.5 3.2 2.6 2.7 4.3 7.9 9.3 8.6 8.3 4.1 3.5 2.9 Polygon: Point: 7.1 5.4 Introduction to Programming © Dept. CS, UPC

Point inside a polygon? Use the crossing number algorithm: 4 2 3 1 Use the crossing number algorithm: Draw a ray (half-line) from the point Count the number of crossing edges: even  outside odd  inside Introduction to Programming © Dept. CS, UPC

Point inside a polygon? A We will use a horizontal ray (x coordinate = ∞) Introduction to Programming © Dept. CS, UPC

Point inside a polygon? #include <limits> // To use the infinity value // Returns true if the point is inside the polygon, // and false otherwise. bool InsidePolygon(const Polygon& P, const Point& A) { Segment Ray; // Horizontal ray Ray.P = A; Ray.Q.x = numeric_limits<double>::infinity(); Ray.Q.y = A.y; Segment Edge; Edge.P = P[P.size() - 1]; // The last point of P bool inside = false; for (int dst = 0; dst < P.size(); ++dst) { Edge.Q = P[dst]; if (Intersect(Ray, Edge)) inside = not inside; Edge.P = Edge.Q; } return inside; } Introduction to Programming © Dept. CS, UPC

Point inside a polygon? Again, problems with the accuracy of real numbers. How about the ray intersecting a vertex? (this problem is beyond the scope of this course) Introduction to Programming © Dept. CS, UPC

Summary Computational geometry has a vast range of applications in different domains: Visualization, Computer Graphics, Virtual Reality, Robotics, etc. Challenge: finding fast solutions for complex geometric problems (think of a 3D real-time video game processing millions of pixels per second). Dealing with the accuracy of real numbers is always an issue, but no so important if a certain tolerance for errors is allowed. Introduction to Programming © Dept. CS, UPC