[Fart-Annoyed] RectF question

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Lividpayment
Posts: 10
Joined: April 13th, 2020, 8:47 pm

[Fart-Annoyed] RectF question

Post by Lividpayment » July 23rd, 2020, 9:13 pm

Code: Select all

class RectF
{
public:
	RectF() = default;
	RectF(float left_in, float right_in, float top_in, float bottom_in);
	RectF(const Vec2& topleft, const Vec2& bottomright);
	RectF(const Vec2& topleft, float width, float height);
	static RectF FromCentre(const Vec2& centre, float halfwidth, float halfheight);
	bool IsOverLappingWidth(const RectF& other)const;
public:
	float left;
	float right;
	float top;
	float bottom;
};
-----------

Hi all! Can someone help me understand this class a little better? I don't quite understand the need for three constructors ..Nor the static function? I understand everything else in the lesson. But this section had me puzzled. Thanks!

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: [Fart-Annoyed] RectF question

Post by albinopapa » July 24th, 2020, 5:17 am

Well, there are 4 constructors so I'll go over them and their importance.

RectF() = default;
This constructor is called the default constructor. It is the one that allows you to create a RectF without passing any parameters.
example: RectF bounds;

RectF(float left_in, float right_in, float top_in, float bottom_in);
This user defined constructor allows you to construct a RectF by passing in all the values used to initialize the 4 values that make up a rectangle: left, right, top and bottom.

RectF(const Vec2& topleft, const Vec2& bottomright);
This user defined constructor pretty much does the same as the previous, but allows you to construct one using two points. Especially useful if the two points are already defined.

RectF(const Vec2& topleft, float width, float height);
This user defined constructor allows you to construct a RectF if you know the position of the top-left corner and the size if the rectangle you want to create.

static RectF FromCentre(const Vec2& centre, float halfwidth, float halfheight);
Chili explains the purpose of this static function in the video. There is already a constructor with a Vec2, float, float signature, so the compiler wouldn't know which function to use...this one or the one creating a rectangle from top-left, width, height. The reason for static is because you might not want to have to create a rect, then do the math to figure center and extents.

The point is convenience and flexibility. Without any constructors, you'd always have to fill out the four members manually.
Example:
RectF bounds = { 10.f, 10.f, 110.f, 110.f };

Now, let's say you wanted to create a RectF at ( 10, 10 ) with size ( 100, 100 ).
Example:
RectF bounds = {10.f, 10.f, 10.f + 100.f, 10.f + 100.f };
or
RectF bounds;
bounds.left = 10.f;
bounds.top = 10.f;
bounds.right = bounds.left + 100.f;
bounds.bottom = bounds.top + 100.f;

Now, let's say you want to have a RectF that surrounds two points, like clicking at one point and releasing at another point
Example: without constructors
Vec2 pointA = { 10.f, 10.f };
RectF pointB = { 20.f, 20.f };
RectF bounds = { pointA.x, pointA.y, pointB.x, pointB.y };
or
RectF boundsC;
boundsC.left = pointA.x;
boundsC.right = pointA.y;
boundsC.top = pointB.x;
boundsC.bottom = pointB.y;

Example: with constructors
Vec2 pointA = { 10.f, 10.f };
RectF pointB = { 20.f, 20.f };
RectF bounds = { pointA, pointB };

The only reason that you must specify RectF() = default is because there are custom constructors. With custom constructors, the compiler doesn't create a default constructor on it's own, so you must tell the compiler to do so. If you didn't have any custom constructors, then you wouldn't even need the RectF() = default;.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply