00:02
Okay, good day, ladies and gentlemen.
00:07
We are looking live at problem number 819 from the wonderful python book, and it is the rectangle 2d class, and this one is a doozy.
00:22
Okay, so what does it all entail? well, to begin with, it's all of this stuff.
00:30
So we got to create this class, rectangle 2b.
00:35
We have four private data fields, x, y, and the width and height.
00:46
We had to create their respective get set methods.
00:51
And the constructor creates a rectangle with a specified center, height, and width, and all the default value should be zero.
01:03
The message should have a get area method, which calculates the area.
01:11
Get perimeter, of course, which calculates the perimeter.
01:16
It contains point method, which is true if x, y is in the rectangle.
01:25
And this contains rectangle 2d.
01:28
True.
01:29
If it's, specified rectangle is in this rectangle, and then you have the overlap one.
01:36
Then you have this other one, this other method, which is kind of operator overloading, which it calls contains also rectangle 2d.
01:48
My only guess here is that he wants to sort of get you to practice this operator overloading thing here.
01:58
I don't know because it doesn't make any sense to me why you name it the same thing both ways.
02:04
But this one is true if the, how would i say this? if this rectangle is in the specified rectangle.
02:16
So in this case, the rectangle, our rectangle, our constructed rectangle is contained in the rectangle 2d here, whereas in the other one, it's the rectangle.
02:29
2d is contained in the specified one in this one, sorry, rectangle 2d is contained in this rectangle, yeah.
02:39
And then he just for added enjoyment.
02:44
He asked you to write these other methods for area comparisons.
02:51
And yeah, so, boy, all of this is fun, fun, fun, isn't it? so let me give you a brief idea here how i went about, you know, getting the function.
03:09
So this one, again, because it's way, way too much work to try to type it all out live.
03:17
I've done it all already, you know, just talk you through it rather than try to do it because this is way too much work.
03:26
But i want to sort of give you an idea, at least sort of, explain some of the formulas a bit.
03:34
Okay, so for starters, let's just draw.
03:40
So this rectangle here, we'll just draw it like this.
03:45
And i know i'm not a very good artist.
03:48
I apologize.
03:49
I just, i do my best, but i'm not very good.
03:53
Okay.
03:55
And we'll give it the points, the center points.
04:00
Call them x um y here um and of course this is our width so this i'm just going to 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 and now i want to point out now this point here okay is so remember this would be w over two so this this length is the width over two so this point here is x minus w over two comma y plus h over two because of course the height is height over two so it's important to remember that oops maybe i'll put the rest in so we have to go through each of the points and label them so this is x plus w over 2 comma y plus height over 2.
05:43
Okay.
05:46
And the reason i'm doing this is because these points sort of play important role in the whole process here.
05:57
W over 2, y minus height over 2.
06:05
And then similarly, the last point here, this is x plus w over 2, y minus h .o.
06:23
Okay, so if we take a point, okay, so if i just take some point out here, right here, and we'll call it.
06:33
Oops, come on.
06:37
Okay, so this is some point.
06:41
We'll just call, we'll call this point a comma b.
06:47
Okay.
06:48
So for a, b to be in the rectangle, what do we need true? well, we need, if you go through, well, the distance from x to a must be an absolute value less than w over two right so in other words for a b to be in rectangle in rect you must have what who's sorry you must have absolute value of x minus a to be less than or equal to w over 2 and we must have the absolute value of y minus b to be less than or equal to h over 2 basically that's you know for for this point to be in in there it has to the y coordinate has to be the distance from y to this point has to be less than h over 2.
08:12
Well, yeah, an absolute value has to be less than h over 2, and the same with the x cord.
08:21
So both of these statements have to be true.
08:25
Now, if we have another rectangle, so if we have another rectangle where we have four points, whoops, maybe i'll do it over here, sorry.
08:38
Yeah, okay.
08:41
Well, in that case, if we have four points, whoops, okay, this isn't working very well.
08:51
So we have, now we have a, let's call this 2, b2, boy, okay, so we have some rectangle here.
09:08
I want to somehow or another.
09:11
Okay.
09:17
Actually, what i should do is something like this.
09:21
So the x coordinates, let me see.
09:24
Okay, maybe i label it like this.
09:26
A, we'll call this b1, and then this point here is a maybe a 1 b and this point whoops sorry spirit here roughly trying to draw roughly then that point is a 1 b1 okay so the rectangle itself would look something like this it's not well drawn.
10:11
I'm sorry about that.
10:14
But the idea would be is if we want this rectangle to be contained in the larger rectangle, then we have to check that the distance from, we have to check.
10:30
Like if we want, we have to check that all four of the corners of this rectangle lie within the larger one.
10:38
And so we basically have to do this kind of statement on each of the corners.
10:47
Another way to do that is to make sure that, well, yeah, basically that's what you have to do, is you have to check that each of the four corners satisfy this statement.
11:02
So if all four of the corners of this smaller rectangle lie within the large, one, then you'll have containment, otherwise you won't.
11:13
And it's very similar if you want to do overlap.
11:16
So that's sort of the idea of what we have to do in the second or the second part of this.
11:31
The second part of this question.
11:35
Okay.
11:36
So with all that being said, let's open.
11:39
Up the console and look at the actual beast.
11:44
Okay.
11:44
And like i said, it's not a very, it's a bit long.
11:52
Okay.
11:53
So it turns out, at least as far as i can remember, i don't actually need to import math.
11:58
I did it sort of stupidly, but i don't need it.
12:03
But again, i create my class rectangle 2d.
12:07
And to do that, i do the initializer.
12:13
And here my variables are x, y, width, and height.
12:18
And again, i'm initializing them to zero or default value zero.
12:25
And their private variables, or at least i'm assuming, they're private variables.
12:30
He doesn't say it explicitly or private data fields.
12:33
He doesn't say it explicitly, but he did say to have to get and set.
12:38
Set methods.
12:40
And so you go through with the underscores.
12:43
Again, we've done this now a couple times.
12:46
So the assessor methods here, again, you're just returning the values.
12:54
So the get x is just returning the x value member.
12:59
Since it's private, you can't see it unless you have a get.
13:04
And then the set is same idea this time.
13:09
We're just setting the values from outside.
13:15
And now we get into a little bit more interesting stuff here with the specified methods.
13:23
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.
13:38
The perimeter, then of course, again, same idea.
13:42
It's just two times the width plus two times the height.
13:47
Okay.
13:48
And now basically i'm using on the contains point.
13:53
Very same idea or very similar.
13:56
This is the idea that i tried to explain.
14:00
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 width over two, and the y minus the y center of the rectangle is less than the height over two, then you return true.
14:33
That's false otherwise.
14:38
Contains now.
14:40
So what i did in contains, i did it a bit different here, and i think i actually did it a bit nicer here.
14:49
So if we look here, what i've done is i formulated before, so i'm given a rectangle rect.
15:00
And what i did here is x1 corresponds to, oops i'm sorry the x1 corresponds to this side here so this is x1 whoops uh no this is x1 so this is x1 wait a minute no i was right the first time this is x1 yes that's x1 this is x2 um this is y1 and this is y2.
15:44
Okay.
15:46
Now, so when i'm looking at contains, what i'm actually looking at, when that statement contains is actually looking at, so the distance, so it's looking at the four statements, x minus, whoops, x minus x1, whoops, so an absolute value here, a, b, s of x minus x1 being less than or equal to w over 2.
16:28
So in particular then what i've done here, so what this is, you might, hopefully you can see that, but the x -1 actually corresponds to that, that lower, this lower line of this guy.
16:48
And then, or i'm sorry, it's this line here.
16:51
And then x2 corresponds to this line here, so on and so forth.
16:56
And let me see.
16:59
Where is it here? here we go.
17:02
And so then each of these, i say each of these are true.
17:09
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 and x1, which is the, like i said, the, what is that, left -hand line is less than this, and this is the right -hand one.
17:42
So if both of these are true, then the x coordinates are inside of it.
17:54
And the height is similarly.
17:57
So in other words, if it's true, so if the wide is true, then you have the wide is true, then you have this relation.
18:12
Which is, okay, you have all the points lie between here and here.
18:23
I'm sorry.
18:29
Yeah, i'm sorry.
18:31
The y, i actually made a mistake.
18:36
If that statement's true, then all the x coordinates lie between here and here.
18:47
Here.
18:48
Now, the y coordinates could be anything, but that's what the second statement is about.
18:54
The second statement is about the y coordinates.
18:58
The second statement says that the y coordinates lie between here and here.
19:07
Okay? and if you put all four of those statements together, well, then you end up with it being in that rectangle.
19:13
And that's what this is about.
19:15
It says, so if both of these statements are true, you'll get true, otherwise you'll get false.
19:25
So they're logical statements, basically.
19:29
And similarly, i did the same thing here, but in this case, let me see.
19:35
Now, this is overlaps...