Presentation is loading. Please wait.

Presentation is loading. Please wait.

Similar presentations


Presentation on theme: "A little bit of geometry"โ€” Presentation transcript:

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

2 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

3 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

4 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

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

6 Preliminary problem ๐ด ๐ต
Do the two points fall on the same side of the line? Introduction to Programming ยฉ Dept. CS, UPC

7 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

8 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

9 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

10 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

11 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

12 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

13 Why artificial vision is so difficult?
Introduction to Programming ยฉ Dept. CS, UPC

14 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

15 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

16 Point inside a polygon? A
We will use a horizontal ray (x coordinate = โˆž) Introduction to Programming ยฉ Dept. CS, UPC

17 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

18 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

19 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


Download ppt "A little bit of geometry"

Similar presentations


Ads by Google