// 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 #include #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 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, 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(m_tl.x + 82); const double y1 = static_cast(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(tr.x + 81); const double y2 = static_cast(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(br.x + 81); const double y3 = static_cast(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(bl.x + 82); const double y4 = static_cast(bl.y - 130 + 72); fl_begin_line(); fl_arc(x4, y4, m_radius, m_start_deg, m_end_deg); fl_end_line(); }