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 92 93 94 95 96 97 98 99
|
// Osman Zakir
// 6 / 4 / 2017
// Bjarne Stroutrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 14 Exercise 5
// chapter14ex5.vcxproj -> main.cpp
// Exercise Specifications:
/**
* Define a Striped_rectangle where instead of fill, the rectangle is “filled”
* by drawing one-pixel-wide horizontal lines across the inside of the rectangle
* (say, draw every second line like that). You may have to play with the
* width of lines and the line spacing to get a pattern you like.
*/
#include <iostream>
#include <vector>
#include "../../Graph.h"
#include "../../Simple_window.h"
namespace Graph_lib
{
class Striped_rectangle : public Shape
{
public:
Striped_rectangle(const Point xy, const int width, const int height,
const int stripe_width, const Color &stripe_color);
void draw_lines() const override;
void set_stripe_color(const Color &color) { m_stripe_color = color; }
void set_stripe_width(int width) { m_stripe_width = width; }
const Color stripe_color() const { return m_stripe_color; }
int stripe_width() const { return m_stripe_width; }
private:
Color m_stripe_color = Color::invisible;
Lines m_striped_lines;
int m_stripe_width;
int m_width;
int m_height;
};
}
int main()
{
using namespace Graph_lib;
constexpr int win_x{ 100 }, win_y{ 100 };
const Point win_tl{ win_x, win_y };
constexpr int win_width{ 600 }, win_height{ 400 };
Simple_window win{ win_tl, win_width, win_height, "Striped Rectangle Class" };
constexpr int rect_x{ (win_width / 2) - 100 }, rect_y{ (win_height / 2) - 45 };
const Point rect_tl{ rect_x, rect_y };
constexpr int rect_width{ 200 }, rect_height{ 100 };
Striped_rectangle striped_rect{ rect_tl, rect_width, rect_height, 2, Color::blue };
striped_rect.set_color(Color::black);
win.attach(striped_rect);
win.wait_for_button();
}
Striped_rectangle::Striped_rectangle(const Graph_lib::Point xy,
const int width, const int height, const int stripe_width, const Color &stripe_color)
: m_width{width}, m_height{height},
m_stripe_width{stripe_width}, m_stripe_color{stripe_color}
{
if (m_width <= 0 || m_height <= 0)
{
error("Bad rectangle: non-positive side");
}
if (m_stripe_width <= 0)
{
error("Invalid stripe width");
}
add(xy);
m_striped_lines.set_color(stripe_color);
for (int i = 1; i < m_height / m_stripe_width; ++i)
{
m_striped_lines.add(Point{ point(0).x + 1, point(0).y + i * m_stripe_width },
Point{ point(0).x + m_width - 2, point(0).y + i * m_stripe_width });
}
}
void Striped_rectangle::draw_lines() const
{
if (fill_color().visibility())
{ // fill
fl_color(fill_color().as_int());
fl_rectf(point(0).x, point(0).y, m_width, m_height);
fl_color(color().as_int()); // reset color
}
if (color().visibility())
{ // edge on top of fill
fl_color(color().as_int());
fl_rect(point(0).x, point(0).y, m_width, m_height);
}
m_striped_lines.draw_lines();
}
|