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
|
// Osman Zakir
// 5 / 23 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 13 Exercise 8
// Exercise Specifications:
/**
* Define a class Regular_hexagon (a regular hexagon is a six-sided polygon
* with all sides of equal length). Use the center and the distance from the
* center to a corner point as constructor arguments.
*/
#include <iostream>
#include "../../Graph.h"
#include "../../Simple_window.h"
struct Regular_hexagon : Graph_lib::Shape
{
Regular_hexagon::Regular_hexagon(Graph_lib::Point center, int distance)
: m_center{ center }, m_distance{ distance } {}
Regular_hexagon::Regular_hexagon()
: m_center{}, m_distance{} {}
Graph_lib::Point get_center() const { return m_center; }
int get_distance() const { return m_distance; }
int side_length() const { return m_side_length; }
void draw_lines() const;
private:
Graph_lib::Point m_center;
int m_distance; // distance from center to a corner point
int m_side_length;
};
int main()
{
using namespace Graph_lib;
using namespace std;
constexpr int win_width = 600, win_height = 400;
const Point win_tl{ 200, 200 };
Simple_window win{ win_tl, win_width, win_height, "Regular hexagons" };
try
{
Regular_hexagon reg_hex{ {win_width / 2, win_height / 2}, 5 };
reg_hex.set_color(Color::black);
win.attach(reg_hex);
}
catch (const exception &e)
{
Text err_msg_start{ {(win_width / 2), (win_height / 2)}, "Exception: " };
Text err_msg{ {(win_width / 2) + 10, (win_height / 2)}, e.what() };
err_msg_start.set_color(Color::black);
err_msg.set_color(Color::black);
win.attach(err_msg_start);
win.attach(err_msg);
}
catch (const runtime_error &e)
{
Text err_msg_start{ { (win_width / 2), (win_height / 2) },
"Runtime_error: " };
Text err_msg{ { (win_width / 2) + 10, (win_height / 2) }, e.what() };
err_msg_start.set_color(Color::black);
err_msg.set_color(Color::black);
win.attach(err_msg_start);
win.attach(err_msg);
}
catch (...)
{
Text err_msg{ { (win_width / 2), (win_height / 2) },
"Unknown exception: " };
err_msg.set_color(Color::black);
win.attach(err_msg);
}
win.wait_for_button();
}
void Regular_hexagon::draw_lines() const
{
if (number_of_points() < 6)
{
error("Can't have a hexagon with less than 6 points");
}
Closed_polyline cpl;
cpl.draw_lines();
}
|