A little bit of geometry Jordi Cortadella Department of Computer Science.

Slides:



Advertisements
Similar presentations
Computer Graphics- SCC 342
Advertisements

CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1 Emmanuel Agu.
CMPE 466 COMPUTER GRAPHICS
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.
Motion Planning CS 6160, Spring 2010 By Gene Peterson 5/4/2010.
CMPE 466 COMPUTER GRAPHICS Chapter 8 2D Viewing Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition by Donald Hearn,

Introduction to Programming (in C++) Numerical methods I Jordi Cortadella Dept. of Computer Science, UPC.
Graphics Programming: Polygon Filling
Objects in 3D – Parametric Surfaces Computer Graphics Seminar MUM, summer 2005.
CS 376 Introduction to Computer Graphics 04 / 06 / 2007 Instructor: Michael Eckmann.
Geometric Algorithms1 segment intersection orientation point inclusion simple closed path.
Introduction to Programming (in C++) Data types and visibility Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. Computer Science, UPC.
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.
CS 450: Computer Graphics REVIEW: OVERVIEW OF POLYGONS
Basics of Rendering Pipeline Based Rendering –Objects in the scene are rendered in a sequence of steps that form the Rendering Pipeline. Ray-Tracing –A.
1. Show geometrically that given any two points in the hyperbolic plane there is always a unique line passing through them. § 23.1 Given point A(x1, y1)
Technology and Historical Overview. Introduction to 3d Computer Graphics  3D computer graphics is the science, study, and method of projecting a mathematical.
Reasoning with invariants Jordi Cortadella Department of Computer Science.
Data types and their representation Jordi Cortadella Department of Computer Science.
Part 6: Graphics Output Primitives (4) 1.  Another useful construct,besides points, straight line segments, and curves for describing components of a.
Chars and strings Jordi Cortadella Department of Computer Science.
Matrices Jordi Cortadella Department of Computer Science.
Prime numbers Jordi Cortadella Department of Computer Science.
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
CS 325 Introduction to Computer Graphics 04 / 26 / 2010 Instructor: Michael Eckmann.
Gaussian elimination Jordi Cortadella Department of Computer Science.
Approximate computations Jordi Cortadella Department of Computer Science.
1 Filling Graphical Shapes. 2 We know how to draw outlines Can we just fill the “inside”? …but how do we know the difference between the inside and outside?
10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping.
CS 376 Introduction to Computer Graphics 04 / 20 / 2007 Instructor: Michael Eckmann.
Polynomials Jordi Cortadella Department of Computer Science.
College of Computer and Information Science, Northeastern UniversityMay 27, CS 4300 Computer Graphics Prof. Harriet Fell Fall 2012 Lecture 9 – September.
Sudoku Jordi Cortadella Department of Computer Science.
Reusing computations Jordi Cortadella Department of Computer Science.
Graphing Linear Equations. Standard Form Equation where x and y are on the same side of the equal sign. Example:
Vertices, Edges and Faces By Jordan Diamond. Vertices In geometry, a vertices is a special kind of point which describes the corners or intersections.
Vocabulary Algebra 1.
GRE: Graphical Representations
Chapter 7 Graphing Linear Equations REVIEW. Section 7.1 Cartesian Coordinate System is formed by two axes drawn perpendicular to each other. Origin is.
Vectors Jordi Cortadella Department of Computer Science.
Chapter 2 Notes Graphing Linear Equations and Linear Systems.
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
Chapter 10 Conic Sections
Approximate computations
Quick Graphs of Linear Equations
Reasoning with invariants
Jordi Cortadella Department of Computer Science
Chapter 8 : Analytic Geometry
Lines in the Coordinate Plane
University of New Mexico
Implementation III.
3D Graphics Rendering PPT By Ricardo Veguilla.
Approximate computations
Grids Geometry Computational Geometry
Grids Geometry Computational Geometry
Introduction to Computer Graphics with WebGL
Grids Geometry Computational Geometry
Jordi Cortadella Department of Computer Science
Jordi Cortadella Department of Computer Science
Computer Graphics Implementation III
A little bit of geometry
Chapter 3 Graphics Output Primitives
– Graphics and Visualization
Implementation III Ed Angel Professor Emeritus of Computer Science
Computational Geometry
Presentation transcript:

A little bit of geometry Jordi Cortadella Department of Computer Science

// 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, UPC2

Line equation from two points Introduction to Programming© Dept. CS, UPC3 P Q

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, UPC4

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

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

Relative position of a point Introduction to Programming© Dept. CS, UPC7

Relative position of two points Introduction to Programming© Dept. CS, UPC8 same sign?

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 } Introduction to Programming© Dept. CS, UPC9 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.

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

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

Representation of polygons Introduction to Programming© Dept. CS, UPC12 (1,3) (4,1) (7,3) (5,4) (6,7) (2,6) 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. 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) typedef vector Polygon;

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

Point inside a polygon? Introduction to Programming© Dept. CS, UPC14 Human view Polygon: Point: Computer view

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

Point inside a polygon? Introduction to Programming© Dept. CS, UPC16 A

Point inside a polygon? #include // 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 ::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, UPC17

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

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, UPC19