Wednesday, 9 September 2020

Chapter 16 // Exercise 4 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and modified versions of the graphics files used throughout the chapters. You can find those versions through the link below.

Chapter 16 // Exercise 4

Make a menu with items that make a circle, a square, an equilateral triangle, and a hexagon, respectively. Make an input box (or two) for giving a coordinate pair, and place the shape made by pressing a menu item at that coordinate. Sorry, no drag and drop.

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2016/Exercise%204


This was tricky because the window constantly calls draw() which appears to be causing problems. When you add a new shape to the window it seems to overwrite the previous one with garbage, causing problems when it comes to redraw the scene.

For some strange reason, I had to make everything a pointer in order for it to work properly. I'm not quite sure why but I have a hunch. Even though I was pushing back instances of Shapes into a vector stored in the window; the previous shapes were going out of scope when redraw() was called. This would explain why the vector suddenly becomes filled with garbage. It knows a Shape used to be there but it doesn't know what. To get round this, I new-ed the shapes up and pushed them into a vector of Shape pointers. This ensures that the shapes cannot go out of scope.

Once again I had one function that handled the button press and used an enum to tell the function what shape to draw.


Chapter 16 // Exercise 4 - Principles & Practice Using C++



No comments:

Post a Comment