PPP2 Chapter 13 Exercise 2

I need help with Chapter 13 Exercise 2 in PPP2.

Exercise specifications:

Draw a box with rounded corners. Define a class Box , consisting of four
lines and four arcs.


My code:
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Osman Zakir
// 5 / 21 / 2017
// Bjarne Stroustrup: Programming: Principles and Programming Using C++ 2nd Edition
// Chapter 13 Exercise 2
// Exercise Specifications:
/**
 * Draw a box with rounded corners. Define a class Box, consisting of four
 * lines and four arcs.
 */

#include <iostream>
#include "../../Graph.h"
#include "../../Simple_window.h"

struct Box : Graph_lib::Shape
{
    Box::Box(Graph_lib::Point tl, int side)
         : m_tl{tl}, m_side{side} {}
    void draw_lines() const;
    int get_side() const { return m_side; };
private:
    Graph_lib::Point m_tl;
    int m_side;
};

int main()
{
    using namespace std;
    using namespace Graph_lib;

    const Point tl{ 100, 100 };
    constexpr int win_width = 600, win_height = 400;
    Simple_window win(tl, win_width, win_height, "Rounded-Corner Boxes");

    Point box_tl{ 250, 400 };
    int side = 100;
    Box box1{ box_tl, side };
    box1.set_color(Color::black);
    win.attach(box1);

    win.wait_for_button();
}

void Box::draw_lines() const
{
    // make the points
    Graph_lib::Point tr = m_tl;
    tr.x += get_side(); // top-right corner

    Graph_lib::Point br = tr;
    br.y += get_side(); // bottom-right corner

    Graph_lib::Point bl = br;
    bl.x -= get_side(); // bottom-left corner

    // draw the lines
    const int space = get_side() / 10; // leave space for arcs
    fl_line(m_tl.x + space, m_tl.y, tr.x - space, tr.y);
    fl_line(tr.x, tr.y + space, br.x, br.y - space);
    fl_line(br.x - space, br.y, bl.x + space, bl.y);
    fl_line(bl.x, bl.y - space, m_tl.x, m_tl.y + space);

    // draw the arcs
    const double x1 = static_cast<double>(m_tl.x);
    const double y1 = static_cast<double>(m_tl.y);
    const double radius = static_cast<double>(get_side());
    const double start_deg = 0;
    const double end_deg = 90;
    fl_arc(x1, y1, radius, start_deg, end_deg);

    const double x2 = static_cast<double>(tr.x);
    const double y2 = static_cast<double>(tr.y);
    fl_arc(x2, y2, radius, start_deg, end_deg);

    const double x3 = static_cast<double>(br.x);
    const double y3 = static_cast<double>(br.y);
    fl_arc(x3, y3, radius, start_deg, end_deg);

    const double x4 = static_cast<double>(bl.x);
    const double y4 = static_cast<double>(bl.y);
    fl_arc(x4, y4, radius, start_deg, end_deg);
}


The main problem right now is that the program crashes when I run it, though I do need to get some help with the constructor and the implementation of the draw_lines() function. I saw some code on the Google Group for the book that I thought I'd try out (which is what the code I have now is basically), but I want to know the best way to do it while specifying a length and width of the box and to also somehow let the user decide the radius of the arcs and the size of the box's sides. Right now I have to hard-code those in the constructor.

Last edited on
Update2: Alright, now I just need to draw the arcs correctly (for the rounded corners). Here's my code:
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Osman Zakir
// 5 / 21 / 2017
// Bjarne Stroustrup: Programming: Principles and Programming Using C++ 2nd Edition
// Chapter 13 Exercise 2
// Exercise Specifications:
/**
 * Draw a box with rounded corners. Define a class Box, consisting of four
 * lines and four arcs.
 */

#include <iostream>
#include "../../Graph.h"
#include "../../Simple_window.h"

struct Box : Graph_lib::Shape
{
	Box::Box(Graph_lib::Point tl, int width, int height, double radius)
		: m_tl{ tl }, m_width{ width }, m_height{ height }, m_radius{ radius } 
	{ }
	void draw_lines() const;
private:
	Graph_lib::Point m_tl;
	int m_width;
	int m_height;
	double m_start_deg;
	double m_end_deg;
	double m_radius;
};

int main()
{
	using namespace Graph_lib;

	const Point tl{ 100, 100 };
	constexpr int win_width = 600, win_height = 400;
	Simple_window win{ tl, win_width, win_height, "Rounded-Corner Boxes" };

	Point box_tl{ 250, 100 };
	constexpr int width = 100, height = 50;
	constexpr double radius = 100;
	Box box1{ box_tl, width, height, radius };
	box1.set_color(Color::black);
	win.attach(box1);

	win.wait_for_button();
}

void Box::draw_lines() const
{
	// make the points
	Graph_lib::Point tr = m_tl;
	tr.x += m_width; // top-right corner

	Graph_lib::Point br = tr;
	br.y += m_height; // bottom-right corner

	Graph_lib::Point bl = br;
	bl.x -= m_width; // bottom-left corner

	// draw the lines
	const int space = m_height / 10; // leave space for arcs
	fl_line(m_tl.x + space, m_tl.y, tr.x - space, tr.y);
	fl_line(tr.x, tr.y + space, br.x, br.y - space);
	fl_line(br.x - space, br.y, bl.x + space, bl.y);
	fl_line(bl.x, bl.y - space, m_tl.x, m_tl.y + space);

	// draw the arcs
	const double x1 = static_cast<double>(m_tl.x + 82);
	const double y1 = static_cast<double>(m_tl.y - 130 + 72);
	fl_begin_line();
	fl_arc(x1, y1, m_radius, m_start_deg, m_end_deg);
	fl_end_line();

	const double x2 = static_cast<double>(tr.x + 81);
	const double y2 = static_cast<double>(tr.y - 130 + 72);
	fl_begin_line();
	fl_arc(x2, y2, m_radius, m_start_deg, m_end_deg);
	fl_end_line();

	const double x3 = static_cast<double>(br.x + 81);
	const double y3 = static_cast<double>(br.y - 130 + 72);
	fl_begin_line();
	fl_arc(x3, y3, m_radius, m_start_deg, m_end_deg);
	fl_end_line();

	const double x4 = static_cast<double>(bl.x + 82);
	const double y4 = static_cast<double>(bl.y - 130 + 72);
	fl_begin_line();
	fl_arc(x4, y4, m_radius, m_start_deg, m_end_deg);
	fl_end_line();
}


And here's the link to the output: http://i197.photobucket.com/albums/aa143/Osman456/gui_output_zpsnf1fmbki.png

Please help me out if possible, anyone. Thanks in advance.
Last edited on
Topic archived. No new replies allowed.