Question: Design a 2D rectangle class called Rect. You are intended to use your Vec class from the previous exercise. Your class Rect must contain the

Design a 2D rectangle class called Rect. You are intended to use your Vec class from the previous exercise.

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 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!

Vech.h

#ifndef VEC_H

#define VEC_H

class Vec {

public:

float x;

float y;

Vec() : x(0), y(0) {}

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

void set(float x_, float y_) {

x = x_;

y = y_;

}

void add(const Vec& v) {

x += v.x;

y += v.y;

}

void print() const {

std::cout << "x: " << x << ", y: " << y << std::endl;

}

};

#endif // VEC_H

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;

}

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!