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();
}
|