Skip to content

Commit 1540369

Browse files
committed
finished exercise09 for chapter12
1 parent 4424f4a commit 1540369

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

Chapter12/exercises/09/main.cpp

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
11
#include "PPP/Simple_window.h"
22
#include "PPP/Graph.h"
33

4-
class Rounded : public Rectangle {
4+
class Rounded : public Lines {
55
public:
6-
Rounded(Point p, int w, int h)
7-
: Rectangle{p, w, h}
8-
{
9-
create_corners(w / 4, h / 4);
10-
}
11-
Rounded(Point p1, Point p2)
12-
: Rectangle{p1, p2}
6+
Rounded(Point p, int w, int h, int r)
7+
: Lines{
8+
Point{p.x, p.y + r}, Point{p.x, p.y + h - r},
9+
Point{p.x + r, p.y + h}, Point{p.x + w - r, p.y + h},
10+
Point{p.x + w, p.y + h - r}, Point{p.x + w, p.y + r},
11+
Point{p.x + w - r, p.y}, Point{p.x + r, p.y}
12+
}
1313
{
14-
create_corners(p2.x - p1.x, p2.y - p1.y);
14+
if (std::min(w, h) / 2 < r)
15+
throw std::runtime_error{"invalid radius"};
16+
create_corners(r);
1517
}
18+
Rounded(Point p1, Point p2, int r)
19+
: Rounded(p1, p2.x - p1.x, p2.y - p1.y, r)
20+
{}
21+
1622
void draw_specifics(Painter& painter) const override {
17-
Rectangle::draw_specifics(painter);
23+
Lines::draw_specifics(painter);
1824
for (Arc* p : c)
1925
p->draw_specifics(painter);
2026
}
27+
2128
private:
2229
std::unique_ptr<Arc> create_corner
23-
(Point p, int w, int h, double st, double rot) const {
24-
return std::make_unique<Arc>(p, w, h, st, rot);
30+
(Point p, int r, double st, double rot) const {
31+
return std::make_unique<Arc>(p, r, r, st, rot);
2532
}
26-
void create_corners(int w, int h) {
33+
34+
void create_corners(int r) {
2735
for (int i = 0; i < 4; ++i) {
28-
double angle = M_PI / 2 * i;
29-
Point p{100, 100};
30-
c.push_back(create_corner(p, w, h, angle, 90));
36+
int curr = (i * 2) % number_of_points();
37+
int prev = curr > 0 ? curr - 1 : number_of_points() - 1;
38+
if (i && i % 2)
39+
swap(prev, curr);
40+
c.push_back(create_corner(Point{point(prev).x, point(curr).y}, r, (i + 1) * 90, 90));
3141
}
3242
}
3343
Vector_ref<Arc> c;
@@ -49,8 +59,14 @@ int main(int /*argc*/, char * /*argv*/[])
4959
const std::string label = "Rounded";
5060
Simple_window win{Point{win_x, win_y}, win_width, win_height, label};
5161

52-
Rounded r{Point{100, 100}, 200, 200};
53-
win.attach(r);
62+
constexpr Point center{win_width / 2, win_height / 2};
63+
constexpr int r_width = 200;
64+
constexpr int r_height = 200;
65+
Rounded r1{Point{center.x - r_width / 2, center.y - r_height / 2}, r_width, r_height, 20};
66+
win.attach(r1);
67+
68+
Rounded r2{Point{0,0}, Point{win_width, win_height}, 50};
69+
win.attach(r2);
5470

5571
win.wait_for_button();
5672
}

0 commit comments

Comments
 (0)