PPP2 Chapter 12 Exercise 1

I need to draw two rectangles. One as a Rectangle (Rectangle class) and the other as a Polygon. I drew them, but I had to do trial and error as I wasn't able to figure it out correctly right away. I guess I'm not good with defining and plotting points in GUI code. I wanted to make the two rectangles appear side by side and be of the same size, but I couldn't do it. Right now, this is what the code looks like (Stroustrup encourages putting exception handling in main, so that's why I did it like that):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Osman Zakir
// 3 / 20 / 2017
// Bjarne Stroustrup: Programming: Priniples and Practice Using C++ 2nd Edition
// Chapter 12 Exercise 1
// File chapter12ex1.cpp
// Exercise specifications:
/**
 * Draw a rectangle as a Rectangle and as a Polygon. Make the lines of the
 * Polygon red and the lines of the Rectangle blue.
 */

#include "../../Simple_window.h"
#include "../../Graph.h"

int main()
{
	using namespace Graph_lib;

	Point tl{ 100, 100 };

	Simple_window s_window{ tl, 600, 400, "Rectangle exercise" };

	try
	{
		Graph_lib::Rectangle rect{ Point{35, 140}, 200, 100 };
		rect.set_color(Color::blue);
		s_window.attach(rect);

		Graph_lib::Polygon poly_rect;
		poly_rect.add(Point{ 400, 50 });
		poly_rect.add(Point{ 500, 50 });
		poly_rect.add(Point{ 500, 100 });
		poly_rect.add(Point{ 400, 100 });
		poly_rect.set_color(Color::red);
		s_window.attach(poly_rect);

		s_window.wait_for_button();
	}
	catch (runtime_error &rte)
	{
		Text message_start{ Point{100, 200}, "Runtime_error: " };
		Text message{ Point{200, 200}, rte.what() };
		message_start.set_color(Color::black);
		message.set_color(Color::black);
		s_window.attach(message_start);
		s_window.attach(message);

		s_window.wait_for_button();
		return 1;
	}
}


To see the output, click this link (there's a screenshot of the window that the code above created): https://1drv.ms/i/s!As6LkLqTe7Ps7XuB7Gtkzlp0gytN.
Last edited on
The right xpos of your first rect is at 235, so your polygon should start at xpos 236.
Also it would be easier if you use constants for the width and height of the rectangle.

BTW. The screenshot url does not work.
How can I tell where the first rectangle starts? And you're saying to put the x-axis of the first point of the second rectangle as 236, right?

And what should I do to make the link work? I shared a link from OneDrive for it, so maybe there's something with that.
How can I tell where the first rectangle starts?
Graph_lib::Rectangle rect{ Point{35, 140}, 200, 100 };
Top left corner X=35, Y=140, width=200, height=100

I don't FLTK installed so I can't test it.
Try this:
1
2
3
4
5
Graph_lib::Polygon poly_rect;
poly_rect.add(Point{ 236, 140 }); // top left
poly_rect.add(Point{ 436, 140 }); // top right
poly_rect.add(Point{ 436, 240 }); // bottom right
poly_rect.add(Point{ 236, 240 }); // bottom lerft 
This is what I have right now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Osman Zakir
// 3 / 20 / 2017
// Bjarne Stroustrup: Programming: Priniples and Practice Using C++ 2nd Edition
// Chapter 12 Exercise 1
// File chapter12ex1.cpp
// Exercise specifications:
/**
 * Draw a rectangle as a Rectangle and as a Polygon. Make the lines of the
 * Polygon red and the lines of the Rectangle blue.
 */

#include "../../Simple_window.h"
#include "../../Graph.h"

int main()
{
	using namespace Graph_lib;

	Point tl{ 100, 100 };

	Simple_window s_window{ tl, 600, 400, "Rectangle exercise" };

	try
	{
		constexpr int rect_width = 100;
		constexpr int rect_height = 50;
		Graph_lib::Rectangle rect{ Point{100, 140}, rect_width, rect_height };
		rect.set_color(Color::blue);
		s_window.attach(rect);

		Graph_lib::Polygon poly_rect;
		poly_rect.add(Point{ 388, 140 });
		poly_rect.add(Point{ 488, 140 });
		poly_rect.add(Point{ 488, 190 });
		poly_rect.add(Point{ 388, 190 });
		poly_rect.set_color(Color::red);
		s_window.attach(poly_rect);

		s_window.wait_for_button();
	}
	catch (runtime_error &rte)
	{
		Text message_start{ Point{100, 200}, "Runtime_error: " };
		Text message{ Point{200, 200}, rte.what() };
		message_start.set_color(Color::black);
		message.set_color(Color::black);
		s_window.attach(message_start);
		s_window.attach(message);

		s_window.wait_for_button();
		return 1;
	}
}


I really need to figure out how to tell where the points need to be. The way it is now is almost right. Want to try getting them in the middle of the window somehow.

And are you not at Chapter 12 yet? Or did you just not do the GUI chapters?

What should I do to show the output of the code here?
Actually I don't have the book. A while ago I borrowed it from a public library.
What should I do to show the output of the code here?

With GUI the only option is a screenshot.
You can upload your screen shots here: http://pasteboard.co/
If you had the book, doesn't that you read it before? If you read all of it, does that mean you had FLTK before but don't anymore?

Here's a link to the image on Pasteboard: http://pasteboard.co/LQMYcgAsQ.png. I hope this one works.

Edit: Not working. I don't know why.
Last edited on
I had to the book only for 3 weeks and didn't get to chapter 12
I actually downloaded the book from the Internet as a PDF. You could try doing that. But I guess that's a bad idea (pirating).

Anyway, there are also these two exercises from the chapter:

4. Draw a 3-by-3 tic-tac-toe board of alternating white and red squares.
5. Draw a red ΒΌ-inch frame around a rectangle that is three-quarters the
height of your screen and two-thirds the width.


For #5, do you think it'd be good to make the outline of the Rectangle 1/4th inches thick? I'm not sure how else I could make a frame. And my computer screen's resolution is 1366 x 768. Does that mean I should draw a rectangle that's three-quarters of 768 pixels and two-third of 1,366 pixels? How big should the window be for that, though?

And for #4, I'm guessing it should be 9 squares inside one big square.
And for #4, I'm guessing it should be 9 squares inside one big square.

The board will look like one big rectangle but actually you will have 9 independent rectangles
So I don't need the outer square and can just make the other 9?
Osman, your links aren't working because you keep putting a full stop immediately after the URL, not separated from it by a space. This causes the forum software to include the stop in the URL. If you want to put punctuation after your URL, you need to separate them with whitespace.

For reference, the URL's you've posted, without the full stops, are:

https://1drv.ms/i/s!As6LkLqTe7Ps7XuB7Gtkzlp0gytN

http://pasteboard.co/LQMYcgAsQ.png

Those links should work.
Last edited on
Oh, okay, thanks.
Topic archived. No new replies allowed.