Oh okay. Good day. Ladies and gentlemen, we are looking live at problem number 8 19 from the wonderful python book. And it is the rectangle two D. Class. And this one is a doozy. Okay. So what does it all entail? Well to begin with it's all of this stuff. So we got to create this class for tangled to be. We have four private data fields X. Y. Um and the width and height. We had to create their respective get set methods and the constructor creates a rectangle with a specified center, Height and width and all the default value should be zero. Um The message, it should have a get area method which calculates the area. Get perimeter. Of course calculates the perimeter. Uh It contains point um contains point method which is true effects wise in the rectangle. Uh and this contains rectangle two D. True. If it's specified rectangle Is in this rectangle and then you have the overlap one, then you have this other one. This um mm. This other method, which is kind of operator overloading, which it calls contains also rectangle to be I I my only guess here is that he wants to sort of get you to practice this um, over operator overloading thing here. I don't know because it doesn't make any sense to me why you name it the same thing both ways. But This one is true if the if the how would I say this if this rectangle is in the specified rectangle? So in this case the rectangle, our rectangle are constructed. Rectangle is contained in the rectangle two D. Here, whereas in the other one, it's the rectangle to D. Is contained in the specified one in this one. Sorry, rectangle to D. Is contained in this rectangle. Yeah. And then he uh just for added enjoyment, he asked you to write these other methods for area comparisons comparisons and yeah, so boy, all of this is fun, fun, fun, isn't it? Okay, so let me give you a brief idea here, how I went about um you know, getting the function. So this one again because it's way way too much work to try to type it all out live. I've done it already. You know, just talk you through it rather than try to do it because this is way too much work. But I want to sort of give you an idea, at least sort of explained um some of the formulas a bit. Um Okay, so for starters let's just draw, so this rectangle here, um we'll just draw it like this and I know I'm not a very good artist. I apologize. I just I do my best but I'm not very good. Um Okay, um and we'll give it the points. The center points will just call them X. Um Why here? Um And of course this is our with so this I'm just gonna say w instead of with you don't mind and this of course is our height. Okay now because in the in the prop. Well let me be. Yeah. Okay. Um And now I want to point out now this point here okay, is um so remember this would be W over. So this This length is the with over two. Um So this point here is ax minus Um W over two comma why plus H. Over two? Because of course the height is height over too. So it's important to remember that, oops maybe I'll uh put the rest in so we have to go through each of the points and label them. So this is X. Plus uh w um over two comma, Y plus Heights, Heights over two. Okay. And the reason I'm doing this is because these points sort of play important role in the in the whole um process here. W over two. Why minus height Over 2? And then similarly the last point here, um this is uh X plus W over two Y minus uh h uh to Okay, so if we take a point, okay? So if I just take some point out here right here and we'll call, come on God demon. Okay, so this is some point we'll just call, we'll call this point a comma be okay? So for a B two B in the rectangle, um What do we need? True. Well, we need, if you go through while the distance from X to a must be an absolute value less than W over two. Right? So in other words, um for a B two B. In rectangle in wrecked, you must have what I'm sorry, you must have um absolute value of x minus A. To be less than or equal to um W over two. And we must have the absolute value of why minus B. To be less than or equal to H. Over to basically that you know, for for this point to be in in there it has to the Y coordinate has to be the distance from Why to this point has to be less than eight or 12. Yeah, in absolute value has to be um less than H. Or two. And the same with the the ex corde. So both of these statements have to be true now if we have another rectangle, So if we have another rectangle where we have four points um Well maybe I'll do it over here. Sorry? Yeah. Okay well in that case if we have four points. Whoops. Okay, this isn't working very well. Um So we have now we have a uh let's call this to B. Two. Um Boy okay so we have some rectangle here. I want to somehow or another. Okay. Actually what I should do is something like this. Um So the X coordinates. Let me see. Okay maybe I label it like this A. We'll call this be one, and then this point here um is eight a. Maybe a. one B. And this point hope, sorry. Spirit roughly trying to draw roughly. Um Then that point is uh A one B. one. Okay, so the rectangle itself would look something like this, It's not well drawn, I'm sorry about that, but the the idea would be is we want this rectangle to be contained in the larger rectangle. Then we have to check that the distance um from, we have to check like if we want we want to we have to check that all four of the corners of this rectangle lie within the larger one. And um so we we basically have to do this kind of statement on each of the corners. Another way to do that is to make sure that um Well yeah basically that's what you have to do is you have to check That each of the four corners satisfy this statement. So if all for the corners of this smaller rectangle lie within the larger one then you'll have containment otherwise you won't. And it's very similar if you want to do overlap. So that's that's sort of the idea of what we have to do um in the in sort of the second or the second um part of this um The 2nd part of this question. Okay so with all that being said, let's let's open up the console and look at the actual beast. Okay and like I said it's not it's not a not a very it's a bit long. Okay. So it turns out at least as far as I can remember, I don't actually need to import mouth. I did it sort of stupidly but I don't need it, but again I create my class rectangle to d and to do that, I do the initialize er um And here my variables are X, Y. Width and height And again I'm initializing them to zero Or default values zero. and their private variables or at least I'm assuming they're private variables. He doesn't say it explicitly or private data fields, he doesn't say it explicitly but he did say Tampa get and set methods um And so you go through with the underscores again, we've done this now a couple of times. Um So the successor, the assessor methods here. Again, you're just returning the values so and the X. Is just returning the X. Value member since it's private, you can't um see it unless you have a get. Um And then the set same idea this time um you were just setting the the values from outside. Um And uh now we get into a little bit more interesting stuff here with the specified methods. So the get area Again. Now all I'm doing is using the get X and get Y. Or sorry get with and get height, multiplying them to get the area the perimeter. Then of course, again, same idea. It's just two times The West Plus two times to hide. Okay. And now basically I'm using on the contains point very same idea or very similar. This is the idea that I tried to explain. Um now we're given some point X and Y and we just check, we're checking the absolute value if x minus the center of X is the center of our rectangle is the X coordinate of the center is less than the with over two. And um the y minus the uh why center of the center of the rectangle is less than the height over two. Then you return true or false otherwise. Okay. Um, contains now. So what I did in contains I did it a bit different here and I think I actually did it a bit nicer here. So if we look here, what I've done is I formulated before so I'm given a rectangle wrecked. and what I did here is X one corresponds to um, oops, I'm sorry the X one corresponds to this side here, so this is X one hopes no, This is X one, so this is X wait around, No, I was right the first time, this is X one, Yes, that's X one, This is X two. Um this is why one and this is why too Okay now, so um so when I'm looking at contains, what I'm actually looking at, when that statement contains is actually looking at um so the distance, so it's looking at this four statements X minus, whoops, X minus um X one, whoops. So an absolute value here, A B. S of x minus X one being less than or equal to um w over two. So um in particular than what I what I've done here, so what this is, you might hopefully you can see that But the X one actually corresponds to that um that lower uh this lower line of this guy. And then I'm sorry, it's this line here in the next two corresponds to this line here, so on and so forth. And let me see where is it? Here, here we go. And so then each of these, I say each of these are true and then Wide is this logical statement which is to make it a little clear here, if you can't quite see what it says, it says that the distance between The X Coordinate & X. one, which is the like I said, the what is that? Left hand uh Left hand line is less than this And this is the right hand one. So if both of these are true then the then the the x coordinates are inside of it and it the height is similarly. So in other words, if it's true, so if the if the if the wide is if the wide is true then um you have this relationship which is um okay you have all all the points lie between here. Well I'm sorry. Um Yeah I'm sorry the the wide, I actually made a mistake. Um If that statement is true than all the x coordinates lie between here and here. Okay, now the y coordinates could be anything, but that's what the second statement is about. The second statement is about the y coordinates. Second statement says that the why coordinates lie between here and here. Okay? And if you put all four of those statements together, well then you end up with it being in that rectangle and that's what this is about it says. Um So if both of these statements are true, your true, otherwise you'll get false. So they're logical statements basically. Um And similarly I did the same thing here, but in this case, um let me see now this is overlaps, so I just need one of these to be true, but this refers to the let me see, this is just what this is referring to, is basically you it's evaluating for this corner here and then you know, evaluating for each corner, I'm sorry this corner here, if this corner is contained in the rectangle, then you have overlapped similarly with here, it doesn't tell you how much overlap, it just tells you that it it does overlap, so it's a it's a simple check, whereas, you know, you're basically looking at the corner and saying okay if the corners in there, you have overlapped and that's basically what I've done is I've I formulated corner one refers to the first corner or whatever and so on and so forth. Have given logical statement to the corners and then said return if one of these is true, then I return true otherwise it's false. Um and then down here it's the same thing, but basically I've given a logical statement to the rectangle so basically think of it like this, um think of it like this basically what you're doing is you're asking if X. I mean if this corner is in the rectangle or not, how would you formulate that? How would you formulate the question if this record this corner is in the rectangle and to give you a hint. The basic idea is that you have to have a you have a condition on the X coordinate and the Y coordinate of this point here. Okay, that's what that refers to. That's what this statement says. It says X one is that corner. So if X one minus the X coordinate of the center of the large direct angle is less than the width over two. And the why one coordinate minus the why coordinate of the center is less than I or two than this statement would be true. Similarly with the other one. Okay, I am now moving on to the next one. Um The contains is really the same thing as the previous contains. Except this time you're flipping it around and you're you're flipping the rules of the rectangles. So really what I could have done here is I could have done and I just think of it now, I could have done um and I'll just write this out. Let's see, I could have done what I could have done um and I'll just maybe put it um down here. What I could have done is this, I could have done um wrecked um, to dodge contains or wreck too. Dot contains. Um What do I want to do? I want to go? I think I want to say self, uh is that what I wanted to? Uh, I'm getting off. Um Yes, I think that's what I would do. I believe it would work. I shouldn't have, but I think this would work. Is that this would actually be the, the way to do it. Because what you then be doing is wrecked. Two contains um refers to this method here. I mean, basically what this underscores does is it does contains, but in a reverse roll, your reversing the self and the rectangle up there. So, I think that would work though. Um I'm not certain, so I don't, but I believe that would work. Um Finally, down here on the end, we have the all this stuff, which is not fun. Oh, there's a mistake here. I just noticed it. I didn't even notice it. Okay. Um so these these are now comparables for area, so all I'm doing here in these last methods. So um for instance, under double underscore cmp, double underscore self and wreck, I'm just comparing, I'm doing negative one if the area of the self of this rectangle is less than the other one and zero if they're equal and one if it's greater than it and so on and so forth. And I made a mistake here because these are methods. Um but I'm not actually interested in in, I'm not actually going to go through and fix them all because I don't need them for this. It's just supposedly we're just supposed to write them, I guess we're um But I mean, you could actually test them and everything, but I'm not interested in doing that because this is already taken so long. Um But um anyhow let's uh let's go take a gander at the um the test file here before we um go on to the uh so let's just go take a gander at the past file here. Okay again I wrote out the test file um beforehand because again it's too long on, so this one here, I should have done something first, I'm sorry. Shit on. Okay good. Um And finally, okay so this is a test file um Okay up here all I'm doing 1st, I'm forming the string with the The X. coordinates, the R. I should say the respective fields for rectangle one and rectangle too. Okay and I'm getting the input For rectangle one and input for rectangle to um and then I'm breaking the string apart, you know, there are requisites string apart into its parts to its separate parts. Um And once I've broken it into its parts um now I'm instant taking or making an instance of the rectum, making two instances of the rectangle to d. Um you know and then Once I've got rectangle one rectangle too, once I've gotten that then I'm just doing a I'm just doing a print, you know the area and perimeter using the format here. Again ari in print area again the second one using again using format same statement. Um And then finally down here it's just print. I'm just uh is our one contained the center of our to our one contains our two and so on and so forth. Basically I'm running the test that he designs there. So let's just do it once here. Okay. And I'm gonna do it exactly for his values here which is five um uh sorry wrong one. Um Wrong one. So it should be nine 1.3 and and early I've points works 35 point three And the other one is 1.3 um 4.3 four and 5.3 Books five points sorry 5.3 and we run it and there you go. So the areas let me uh Yes. Uh Thank God. Okay, so the first one areas are is 35 point oh perimeter is 19.6. The second one is 21.2 and 18.6. And all the statements are false. Okay, so we've managed to make it all the way through the run and everything now for the final lap. I guess for the for the what you call it violence be the final, the final lap. If you will. Like I said, this is a This is indeed a long one. Okay so we need the uml diagram, of course. Who isn't this exciting? Um Again Mhm. I'm just inclined to cut this short because we've done these uml diagrams quite a few times now. Um I'll work through it but it's it's getting a bit um getting a bit uh long. Okay so ax which I should say yeah X. Floats. Um Why float? It's not you? Sorry? Why float um with boats and height float? And these are all uh private it to fields and um for the respected values respectively values of the of I guess this rectangle uh can go okay. Um And now we have the rectangle class. So rec angle uh two d. and of course acts equals zero. Y. Equals zero. Uh With Little zero and height Equals zero. and constructs a rectangle objects with the respective respective values. Okay? Um And I just want I'm just gonna say this, I'm not gonna write them all out because I'm you know like I said this is very this is getting very long but you have the the respective and get set methods for the private private data fields. Okay. And I hope by now you know what those things do because we've done them a few times. So just please go back. You don't go look at my previous videos. I've talked the previous videos in this chapter because I've I've talked about getting set methods. I'm not gonna do it again. Um Okay so now after that we have the okay now we have the let me see so we have the wait a minute. Sorry? Okay so now we have the contains points. So looks contains points. Um X luch. Why float? And this returns? This is boolean. Um And it just tax checks. Um True. Yeah X. Y. Is in this rectangle. So if it's in this rectangle it's true false otherwise um contains um rectangle rex angle uh to the and which is a rectangle wrecked object or a right angle object? Tangle object. Okay for now on because I have to do this, repeated the rectangle. Two D. Is a rectangle object. Or let's even shorten that because that's even too long. We'll just make it wrecked. Okay wrecked. Is a rectangle object? Just keep in mind it's not this one, it's another instance of rectangle. So it's another rectangular object. Okay? Um And this also uh is bullion cool. And it's true if wrecked is it's not right wrecked is contained and this wreck thing okay And the next one is overlaps and racked again. Um Or and it's true if uh racked over laps with this rectangle. So when I say this rectangle again I'm referring to the one the the instance which is self the one we've created hey and um and then we have the contains and this is wrecked and again this is bullying. It's true if. Uh huh. This rectangle is contained in record, so the specified uh if this one is contained in the specified rectangle And the last so the next ones um there's there's like a list of 10 of these and I'm not I'm really not going to go through them all okay because they're all the same. They're all the same idea but I'm just gonna I'm gonna I'll do one because they're all the same thing which is wrecked or not. I shouldn't say they're all the same thing but they all um they all do basically the same thing, it's just um it returns an integer and um one Or I should say -1 if um area of wrecked, um is greater than um area of specified groups, specified wrecked or sorry, area of this rectangle. This trick tangle. Okay. Um and zero yes. Area of rex uh equals area of this rectangle and um one if area of rex is less than area of this. Yeah. And then you go through the rest. I mean they're all, they're all looking at some statements. A lot of them are actually blew in statements. I'm just comparables on the areas. So they're just, you know, they're just, you're, you're just looking at the area, the calculated area of the rectangles. So using get area of the respective um the respective rectangles and you're comparing them and you're returning certain things depending on, okay. So I apologize if some of this was a bit abrupt, but it really it really it's really a lot of work and I hope, I hope you got something out of it, but thank you very much and uh have a nice day. And again, a lot of it comes through, you know, looking at this diagram and trying to, you know, figure out logic statements for the points and stuff. So it is it is it is an involved problem, but thank you very much and have a nice day trump.