Question: Rectangle Class Design a 2D rectangle class called Rect. Use the Vec.h class. Your class Rect must contain the following: - 4 floats to represent

Rectangle Class

Design a 2D rectangle class called Rect. Use the "Vec.h" class.

Your class Rect must contain the following: - 4 floats to represent the rectangle, where two are the coordinates (x,y) of the upper-left corner of the rectangle, and the other two are the dimensions (width and height) of the rectangle - at least one constructor that receives the four floats defining the rectangle - a method called "contains" which receives a Vec as parameter and returns true if the Vec is inside the rectangle, and false if the Vec is outside the rectangle

Your "Rect.h" class should allow the "rectangles.cpp" file to run without generating any error messages.

NOTE: Because the (x,y) coordinates specify the UPPER-LEFT corner of the rectangle, the rectangle's "height" actually goes down!

THE OUTPUT YOU ARE LOOKING FOR: Test 1 passed! Test 2 passed! Test 3 passed! Test 4 passed! Test 5 passed! Test 6 passed! Finished!

rectangles.cpp

#include

#include "Rect.h"

using namespace std;

Vec nullvec( 0.0f, 0.0f );

int main( int argc, const char* argv[] )

{

Rect a( -1.0f, 1.0f, 3.0f, 4.0f );

Rect b( -5.0f, -5.0f, 2.0f, 2.0f );

Rect c( 5.0f, 8.0f, 2.0f, 2.0f );

if( a.contains( nullvec ) == true ) cout << "Test 1 passed! ";

else cout << "error1 ";

if( b.contains( nullvec ) == false ) cout << "Test 2 passed! ";

else cout << "error2 ";

if( c.contains( nullvec ) == false ) cout << "Test 3 passed! ";

else cout << "error3 ";

if( a.contains( Vec( 0.0f, 3.0f ) ) == false ) cout << "Test 4 passed! ";

else cout << "error4 ";

if( b.contains( Vec( -4.0f, -6.0f ) ) == true ) cout << "Test 5 passed! ";

else cout << "error5 ";

if( c.contains( Vec( 6.0f, 9.0f ) ) == false ) cout << "Test 6 passed! ";

else cout << "error6 ";

cout << "Finished! ";

return 0;

}

Vec.h

#ifndef VEC_H

#define VEC_H

using namespace std;

class Vec {

public:

float x, y;

Vec() : x(0.0f), y(0.0f) {}

Vec(float x_, float y_) : x(x_), y(y_) {}

void set(float x_, float y_) {

x = x_;

y = y_;

}

void add(const Vec& other) {

x += other.x;

y += other.y;

}

void print() const {

cout << "(" << x << ", " << y << ") ";

}

};

#endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!