 Hello and welcome to this next session on data structures and algorithms. Starting today we will discuss some very simple yet interesting algorithms from the area of computational geometry. Today we will discuss some properties associated with line segments. For example, how would you know if given 3 points P0, P1 and P2 whether the angle suspended at P0 by the line segments P1, P0 and P2, P0 is clockwise or counterclockwise that is how do you find out if P1 lies above or below the line segment P0, P2 and vice versa. So computational geometry happens to be a branch of computer science that studies algorithms for solving geometrical problems and it has applications in graphics, robotics, BLSI design, computer design, molecular modeling etc. Basically all areas that have something to do with shapes of objects and understanding properties associated with those shapes. So the general input is a set of geometric objects, set of points, line segments, vertices of polygon etc. Output is either a response to query about the objects or in some cases you synthesize new geometric objects. So today's discussion will be largely about query about some objects. For example, two line segments intersect. In the next class we will discuss an instance of the second kind of output, the synthesis of a new geometric object. We will discuss the synthesis of something called the hull or more specifically the convex hull of a set of points. You could think of the convex hull as the tightest enclosing polygon around a set of points. For this you might have to drop some points from the boundary of the hull and let them lie in the interior of the hull. Coming back to line segments, we will be interested in characterization of line segments as a convex combination of the two points that form the end points of the line segment. So let us call these two points P1 and P2. So any point P3 that lies between P1 and P2 is understood to be a convex combination of P1 and P2 and the coordinates of P3 are basically convex combinations of the coordinates of P1 and P2 which is alpha x1 plus 1 minus alpha x2 for some alpha between 0 and 1. A y3 is alpha y1 plus 1 minus alpha y2. So more generally we call the point itself to be a convex combination of the two points P1 and P2. So P3 is alpha P1 plus 1 minus alpha P2. So we will start with the simplest of enquiries given points P0, P1 and P2 and let us say for simplicity sake we let P0 be just 00 that is we set P0 to be the reference point origin and our question is whether P1 P0 is clockwise or counterclockwise with respect to P2 P0 and this how you depict as you can see in this figure P0 P1 is counterclockwise from P0 P2. One might also be interested in whether any two given line segments intersect at all in which case the enquiry would be more like do P1 P2 and P3 P4 intersect at all. Let us answer the first question direction of P1 from P2 with respect to P0. A very fundamental tool to answer this question is to look at the cross product. The cross product is depicted as P1 cross P2 and it is not commutative. So P1 cross P2 is not necessarily equal to P2 cross P1. Now one can understand this cross product as follows. So if P0 is here and we consider a parallelogram with corners at P0 P1 and P2 and the fourth corner being the sum of P1 and P2 which happens to be here. However one can interpret this cross product as the area of this parallelogram. However this area is not commutative with respect to P1 and P2. So we call this the signed area. So the signed area P1 cross P2 is not the same as a signed area P2 cross P1. So one can recall that area of such a parallelogram can be obtained as the determinant of this matrix with P1 as a first column and P2 as a second column. This determinant is x1 y2 minus x2 y1. Similarly the cross product P2 cross P1 can be written as this determinant x2 y2 x1 y1 and well this is nothing but x2 y2 minus x1 y1. So one can see that P2 cross P1 is exactly the opposite sign as P1 cross P2 and that is why we refer to this area as a signed area. Now what does this sign actually denote? Well it turns out that if P1 is clockwise from P2 which is the case here then this signed area or the determinant of P1 cross P2 is positive. On the other hand you can verify that the sign of P2 cross P1 which is exactly the negative of P1 cross P2 is negative. So what this says is that while P1 is clockwise from P2 P2 is counter clockwise from P1. So continuing with this same example where we assume that P0 is the origin the cross product is computed as follows P1 cross P2 is x1 into y2 minus x2 into y1 and for the specific example P2 is 4 comma 1 P1 is 2 comma 3 and this cross product is minus 10. Since the result P1 cross P2 is negative we have verified that P1 is counter clockwise from P2 with respect to the origin P0. One can immediately see that P2 cross P1 is plus 10 and therefore P2 is clockwise from P1 with respect to the origin P0. How about two arbitrary line segments P0 P1 and P1 P2? So what does this mean? Well this means P0 P1 and P1 P2. Now we will not make any specific assumption about P1 or P0 or P2 do they turn left or right at point P1? Now it is similar to the previous question of whether P2 turns clockwise or counter clockwise from P0 with respect to P1. So this will be equivalent to the question does P2 turn counter clockwise or clockwise from P0 with respect to P1 and that is what we have summarized here. The approach will be to check whether the directed segment P0 P2 is clockwise or counter clockwise with respect to the directed segment P0 P1 and that is through this cross product P2 minus P0 that is the first vector that we want P2 minus P0 that is the vector whose orientation needs to be checked with respect to P1 minus P0. One can generalize the discussion so far to address the following question. We are interested in determining whether two line segments intersect or not. We will answer this question by looking at the direction of turn. We have already seen that if our points were P0 P1 and P2 then the cross product P1 minus P0 P2 minus P0 if it is positive then we know that P1 turns clockwise from P2 with respect to P0. Now can we use this fact to answer the general question if two line segments P1 P2 and P3 P4 intersect. One would look at different possible angles here but let me explain the main motivation. The motivation is to see if the points P1 and P2 lie on opposite sides of the line P3 P4 and also simultaneously the points P3 and P4 lie on opposite sides of the line P1 P2. So you can convince yourself that if both occur that is P1 and P2 are on opposite sides of the line P3 P4 and P3 and P4 are on opposite sides of the line P1 and P2 then indeed the line segments P1 P2 and P3 P4 intersect. There are of course some corner cases that we need to handle. What is the point of intersection? What if the points actually meet at one of P1 P2 P3 or P4? But keeping that aside for the time being we will answer this question by looking at angles. So what we expect is that if P3 and P4 lie on opposite sides of the line P1 P2 then the angle formed by P3 at P1 with respect to P2 should have the opposite sign as the angle formed by P4 at P1 with respect to P2. In other words we would expect that on one hand if P3 was clockwise from P2 with respect to P1 then P4 should be anticlockwise from P2 with respect to P1. Likewise you would expect P1 to be anticlockwise or counterclockwise from P4 with respect to P3 and P2 should be clockwise from P4 with respect to P3. In other words we would expect the signs of the cross product P1-P3 with P4-P3 to be exactly opposite to that of P2-P3 with P4-P3. This has been depicted in this picture. We expect these line segments to intersect if P1-P3 cross product with P4-P3 is negative which means P1 turns counterclockwise from P4 with respect to P3 and on the other hand P2 turns clockwise from P4 with respect to P3. This is indeed the case here. It is also possible that the P1 and P2 get exchanged in which case all we expect is one to be clockwise and the other to be counterclockwise. So what we are looking for in this entire process is the sign of P1-P3 cross product with P4-P3 to be exactly opposite of the sign of P2-P3 cross product with P4-P3. Similarly we would expect the sign of P4-P1 cross product with P2-P1 to be exactly opposite of the sign of P3-P1 cross product with P2-P1. One can also consider the case where P3 and P4 are on opposite sides of the line P1-P2 whereas P1 and P2 are on the same side of the line P3 and P4. In such a case, yes the lines do intersect but the line segments do not. This precisely is a significance of both the tests. One needs to determine that the cross products have opposite signs for both the pairs P1-P2 and P3-P4. It does not suffice to check for opposite signs only for one of them. Here is the corner case what if one of the points say P3 happens to be collinear with P1-P2. In general what do you expect if three points P1-P2 and P3 are collinear? Well if they are collinear we would expect that P3 is neither counter clockwise nor clockwise from P2 with respect to P1. It can be neither positive nor negative. So, we would expect this cross product of P3-P1 with P2-P1 to be 0. So, the question is that if indeed P1 and P2 lie on opposite sides of the line P3-P4 but if between the cross products associated with P3 or P4 one of them turns out to be 0 then it is possible that the point with the 0 cross product lies exactly on the line segment joining P1 and P2. To check if P3 lies on the line segment P1-P2 we are going to check if the x coordinate of P3 is between the x coordinates of P1 and P2. X3 should be exactly in the line segment or in the closed interval x1, x2 and check if y3 belongs to the closed interval y2, y1. We have very implicitly referred to this specific orientation of P1 and P2 in defining these intervals x1, x2 and y2, y1. We know that y2 is indeed smaller than y1 and x1 is smaller than x2. In general what you require is that x3 lies in the closed interval min x1, x2, max of x1, x2 and y3 should also lie in this closed interval min of y1, y2, max of y1, y2. If this is not the case it could mean that P3 is indeed collinear but it does not lie on the line segment. So, let me present in a case that P3 is collinear and here is P3. However, we find that its x coordinate as well as the y coordinate lie outside the range the closed interval provided by P1 and P2. This is precisely the example P3 is indeed collinear with P1 and P2 but unfortunately P3 does not lie on the line segment P1 and P2. So, please note how we have at times talked about lines and at some other times talked about line segments. As far as points lying on opposite sides is concerned we talk of their lying on opposite sides of a line. However, when it comes to collinearity we insist on line segments. So, we put together our algorithm for detecting if two segments intersect. Initially we compute four directions. Direction D1 is the direction of P1 from P4 with respect to P3 and remind you what this means. This means that we are pivoted at P3. We look at P1 minus P3 and P4 minus P3. If the cross product P1 minus P3 times P4 minus P3 is positive then it means that P1 is clockwise from P4 with respect to P3. So, likewise we look for the direction of P2 from P4 with respect to P3. Direction of P3 from P2 with respect to P1 of P4 from P2 with respect to P1. Note that we have arbitrarily chosen P3 and P1 from the line segments P3, P4 and P1, P2. As homework you might want to look at the direction of P1 from P3 with respect to P4. Basically swapping P3 and P4 here and similarly swapping P1 and P2 in the directions D3 and D4. You will find that not much will change at all. Now if you find that D1 and D2 have opposite signs irrespective of whether the pivot is P3 or P4 and if directions D3 and D4 have opposite signs then you are done. If it turns out that any of the four D1 to D4 are 0 which means some three points are collinear then one checks if the collinearity is on the line segment itself. So, if D1 is 0 that is if the direction of P1 from P4 with respect to P3 is a 0 then you check if P1 lies on the line segment P3, P4. If D2 is 0 you check if P2 is on the line segment P3, P4 and so on. In either of these four cases again you know that the two line segments indeed intersect. If none of these are true which means the directions D1 and D2 and D3 and D4 do not have opposite signs none of these three points are collinear certainly the line segments do not intersect at all and the case for this is shown here. As already indicated direction Pi Pj Pk looks at the direction of Pk with respect to Pj from Pi. If the direction is positive it means that Pk is clockwise from Pj with respect to Pi and the on segment routine does exactly what we described earlier. You look at the min of xi and xj and the max of xi and xj and ensure that xk lies exactly between the two. You also need to ensure that yk lies in the closed interval determined by the min of yi and yj and the max of yi and yj. Thank you.