 Welcome back, in the last segment we saw a second idea for solving the Taxi Dispatch problem. In this segment we are going to take another interesting use of arrays and this is in a problem called the Disk Intersection problem. So what is the Disk Intersection problem? So given a collection of n disks in the plane, decide if any two intersect. So before I get into the algorithm I should explain to you what exactly is given as input. So what is given as input are say x1, y1 which are the centers of the first disk and r1 which is the radius of the first disk, x2, r2, x2, y2 are the centers, the coordinates of the centers of the first disk and r2 which is the radius of the second disk and so on until some xn, yn, rn. So you are given these n triples of numbers, the first two numbers give the coordinates of the center of the disk and the last number gives the radius of that disk. So you are given three n numbers, each triple representing a disk and you want to know whether the disks are intersecting. So pictorially if this is say x1, r1, x1, y1 and if this is r1 then this is the disk, this is the first disk. Let us say this is x2, y2, the point x2, y2 and let us say this is the radius, this distance is the radius. So then the disk will look something like this and if the radius is large enough then there is an intersection over here. So in this case if this happens for any pair of disks then we are supposed to print an answer 1. On the other hand if all the pairs, if all the circles are completely in different, they do not intersect at all. So maybe they look something like this, they could be really small, they do not have to be of the same size of course. If they look something like this then we are supposed to print out a 0 or a false that there is no intersection, say 1 or 2 if there is an intersection and 0 if there is no intersection whatsoever. You could make the problem more general by asking say for one thing to print out which disks intersect or say maybe how many pairs of disks intersect and so on. So there are variations possible but let us just worry about the simplest. So now how do we solve this problem? Well we could check all possible pairs. So there are some n disks and in these disks say 1 through n, how many pairs are there? Well there are n choose 2 pairs. So we should, we could check all such pairs. What do I mean by that? So let I denote the index of some disk and let us say it is the smaller number disk in a pair. So what could the other elements be, the other disks in the pair possibly be? So first of all it is the smaller number disk in a pair then I must range from 0 to n minus 2 because if it is n minus 1 then the other disk will have to have a smaller number and we are just saying that i is the index of the smaller number disk within a pair. And what should j be? For each i the other disk must range from i plus 1 to n minus 1. So we want to find such i and such other disk numbers j so that j goes from i plus 1 to n minus 1 for every i which in turn goes from 0 to n minus 2. But that is basically it as far as deciding how do you go over all the pairs? And the intersection check is reasonably simple, the distance between the centers must be less than the sum of the radii. So we have all the information about the centers and about the radii and we just have to check that. So here is the program, so again I am going to use a name n which is going to denote the center, the number of disks. So I am going to have an array to keep track of all the x coordinates of the centers of all the disks, an array to keep track of the y coordinate of all the centers of all the disks and an array radius to keep track of the radii of all the disks. Then I am going to read in this data. So that is all simple enough. Then is the code which goes over all pairs of objects, all pairs of disks and this is really the interesting part in this. So we have some n objects and how do you go over all pairs of them? So first, so before going over all pairs we are going to have a Boolean variable intersect which will say whether we have found an intersection or not. So originally we have not found an intersection so it is going to be false. And then our pair is going to be ij but we said earlier that i has to lie between 0 and n minus 2. So i less than n minus 1 is exactly what we want, 0 between 0 and n minus 2. I could have written this as i less than or equal to n minus 2. So in each iteration we will consider each such value of i and therefore we are incrementing i at the end of the iteration. Now corresponding to every i, j is going to take values from i plus 1 to n minus 1 and therefore these parameters have been set in this manner and of course in each iteration at the end of the iteration I am going to increment j. And what do I do in each iteration? In each iteration I have to check whether disc i and disc j are intersecting. So how do I do that check? Well my check is going to be done. Normally I should check whether the distance from center of i to center of j is greater than the sum of the radii but the distance from the center of i to center of j is going to be something like square root of x1 minus x2 or xi xi minus xj square plus yi minus yj squared if the ith disc is at centered at xi yi and the jth disc is centered at xj yj. So I could say that I need to calculate this and take the square root of it. Instead of that I am just going to square this distance. So I will just get xi minus xj squared plus yi minus yj squared. And if I square it I am going to take the sum of the radius squared. So I am going to check whether this is r1 plus r2 but instead of that I am also going to square that. So instead of being a square root I will just square because squaring is easier. So that is what is happening over here. So this term power of center of i minus center j squared is this xi minus xj squared term. This is center of, center yi minus center j power 2 so this is yi minus yj squared and as I said I could have written a square root over here but instead of that I am taking radius of i plus radius j and squaring it. So if I find that the centers are closer than the sum of the radii or the squares of the distance between the center is less than the sum of the radii squared then I know that the intersection is happening and therefore I am going to set intersection equal to true. And the moment I find that at an intersection is happening I can break because my variable intersect is only going to take values false or true and I have found a true value I have found an intersection and I must report that. So if I run through this entire nested loop saying that and do not find an intersection then my intersection will remain false. So in any case when I come to this point either through the break or after going through all the iterations intersect will tell me whether this intersect or not. So that is what I am going to print and that is going to be the end of my program and of course I have to put this in a main program and I have to include header files and things like that. So what have we discussed in this? We have discussed given a set of objects how do you go over all pairs? So this is an interesting operation that happens from time to time and as an exercise I would like you to generalize the idea. So specifically write a program to read in end points coordinates of end points and check if any three of them are collinear. So you know how to do this, you know how to figure out whether a point is whether three points are collinear, you just have to calculate slopes and things like that. And in while doing this calculation I will suggest that you avoid division because division will create problems if the coordinates have, if the points have the same x coordinate or the same y coordinate and if you are dividing, if you are calculating the slope you are dividing by the difference of the y coordinate and that happens to be 0. So instead of that you can do a little bit multiplication rather than some division, you cross multiply the terms and then you can get the division. So that is basically it and in the next segment we are going to talk about how you deal with whether we can have arrays of graphic subjects and we will also conclude this entire lecture sequence. So we will take a short break.