# Lab2 Solved

35.00 \$

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: . ` zip` solution files instantly, after Payment

## Description

INSTRUCTIONS

————

Compile this code. You should see 3 rectangles, one of which you can move

with the ‘w’, ‘a’, ‘s’, and ‘d’ keys.

Read through this code! Try to understand it before starting the assignment.

Comment confusing lines with what you think code is doing, and experiment

with existing code to test your understanding.

Once you feel comfortable with this code, accomplish each of the following,

and make sure your code compiles and runs after each step is completed.

1) Get and set functions

1. a) In Rect, create a get and set methods for “min” and “max”. Use the

signature “void setMin(Vec2 const & min)” and

“void setMax(Vec2 const & max)”. Use the “this” pointer to disambiguate

“min” and “max”.

1) Refactor userRect to be dynamic

1. a) Make userRect a dynamic object. That means it should be declared as

“Rect * userRect” instead of “Rect userRect”. Use new to dynamically

allocate.

1. b) the member operator ‘.’ will need to be replaced with the

pointer-to-member operator ‘->’

1. c) Don’t forget to delete userRect at the end of the program!

1. a) Overload the += operator for Vec2, and have it do exactly what

1. b) Replace uses of Vec2::add with the += operator. For example, instead of

3) Random rectangles, by reference and by pointer

1. a) create a method with the method signature “void setRandom(Rect & r)”.

This function will give the passed-in Rect object a random location.

The random x should be between 0 and 50 x. The random y should be

between 0 and 20. Limit the possible width and height to a minimum of 2

and a maximum of 10.

1. b) test “void setRandom(Rect & r)” on the local Rect object “rect0”.
2. c) create a method with the method signature

“void setRandomByPointer(Rect * r)”, which functions the same as

“void setRandom(Rect & r)”, except that the argument is

passed-by-pointer.

1. d) test “void setRandomByPointer(Rect * r)” on the local Rect object

“rect1”.

4) Test and show overlap

1. a) Using the existing function “isOverlapping(Rect const &)”, test to see

if userRect collides with any other Rect objects. If userRect is

overlapping, draw it with ‘+’ instead ‘#’.

1. b) Create a Rect * pointer that points to the address if the Rect object

that userRect collides with. It should point at NULL if userRect is

colliding with no other Rect objects.

1. c) Print to the screen the width and height of a Rect object that userRect

collides with. If no collision is happening, print “no collision”

5) Array of objects

1. a) Replace the Rect objects rect0 and rect1 with an array of 2 Rect

objects, “rect[2]”.

1. b) Make sure you replace every remaining “rect0” with “rect[0]”, and every

“rect1” with “rect[1]”.

1. c) Increase the size of the “rect” array to 5. Make sure all 5 Rect

objects are randomized, drawn to the screen, and tested for collision.

1. d) If you have not already done so, replace

duplicate-code-using-array-elements with a for-loop. For example:

If you have:

rect[0].draw(‘0’);

rect[1].draw(‘1’);

rect[2].draw(‘2’);

rect[3].draw(‘3’);

rect[4].draw(‘4’);

Replace it with:

for(int i = 0; i < NUMBER_OF_RECTS; i++)

{

rect[i].draw(‘0’+i);

}

Do this where objects are randomized, drawn, and tested for collision

Achievements: (if you finished, and would like a challenge, try these)

[Union Job] When the userRect collides with another rectangle, instead of

drawing the entire userRect with ‘+’ signs, draw only over the

intersecting area.

[Dat Rectangle] Implement an interface that allows the user to select the

moving rectangle, cycling through all possible rectangles with the tab

key, indicating the selected rectangle by drawing it with ‘#’.

[One Size Fits All] Implement an interface that allows the user to increase

or decrease the size of the selected rectangle. For example, if ‘w’,

‘a’, ‘s’, and ‘d’ is used to move the rectangle, ‘W’, ‘A’, ‘S’, and ‘D’

could move the Rect’s max location up, left, down, and right.

[Add Another] When the user presses space, dynamically resize the rect array

to hold an additional random rectangle.

[One Too Many] When the user presses backspace, dynamically resize the rect

array to hold one less rectangle, removing the last one in the list.

When finished:

1) Make sure your name is at the top of this source file

2) Submit this project online

1. a) Right-click on the .cpp file’s name within visual studio, and select

“Open Containing Folder”

1. b) Close Visual Studio (you may re-open this .cpp file by right-clicking

on it in the file system, and slecting edit)

1. c) Make sure the following files are DELETED from the project’s file

structure:

* Any file with the extension: “.ncb”, “.sdf”

* Any folder named: “Debug” or “ipch”

* If Visual Studio is open, you will not be able to delete some files

* If you do not see file extensions, press Alt in the file explorer,

select “Tools”->”Folder options…”->”View”, and uncheck

“Hide extensions for known file types”.

1. d) zip the file structure (the project), which is now missing the

temporary files

* Select all of the files in the project folder

* If the resulting zip file is more than 1mb, you have included

temporary files mentioned above. Delete temporary files (that don’t

have a .cpp or .h extension) and try again.

• GSP125_Lab2.zip