Skip to content

Commit 284626f

Browse files
committed
Update examples
1 parent 07cfa5e commit 284626f

File tree

7 files changed

+130
-30
lines changed

7 files changed

+130
-30
lines changed

examples/bernoulli_dist.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ int main() {
1212
std::bernoulli_distribution dist(0.10); // 10% White, 90% Black
1313

1414
for (bmp::Pixel &pixel: image) {
15-
bmp::Pixel color = dist(eng) ? bmp::White : bmp::Black;
15+
const bmp::Pixel color = dist(eng) ? bmp::White : bmp::Black;
1616
pixel = color;
1717
}
1818

1919
image.save("bernoulli.bmp");
2020

2121
return EXIT_SUCCESS;
22-
}
23-
catch (const bmp::Exception &e) {
22+
} catch (const bmp::Exception &e) {
2423
std::cerr << "[BMP ERROR]: " << e.what() << '\n';
2524
return EXIT_FAILURE;
2625
}

examples/chess_board.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ int main() {
66
// 8x8 chess board
77
bmp::Bitmap image(640, 640);
88
const std::size_t board_dims = 8;
9-
const std::size_t rect_w = image.width() / board_dims;
10-
const std::size_t rect_h = image.height() / board_dims;
9+
const std::int32_t rect_w = image.width() / board_dims;
10+
const std::int32_t rect_h = image.height() / board_dims;
1111

1212
// Iterate over rects
1313
bool is_white = true;
1414
for (std::size_t x = 0; x < image.width(); x += rect_w) {
1515
for (std::size_t y = 0; y < image.height(); y += rect_h) {
16-
bmp::Pixel color = is_white ? bmp::White : bmp::Black;
16+
const bmp::Pixel color = is_white ? bmp::White : bmp::Black;
1717
// Fill rect
1818
image.fill_rect(x, y, rect_w, rect_h, color);
1919
// Next rect in will be the opposite color
@@ -26,9 +26,8 @@ int main() {
2626
image.save("chess_board.bmp");
2727

2828
return EXIT_SUCCESS;
29-
}
30-
catch (const bmp::Exception &e) {
29+
} catch (const bmp::Exception &e) {
3130
std::cerr << "[BMP ERROR]: " << e.what() << '\n';
3231
return EXIT_FAILURE;
3332
}
34-
}
33+
}

examples/draw_primitives.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ int main() {
3232
image.save("primitives.bmp");
3333

3434
return EXIT_SUCCESS;
35-
}
35+
}

examples/polymorphic_shapes.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22
#include "BitmapPlusPlus.hpp"
33

44
struct Shape {
5+
virtual ~Shape() = default;
6+
57
int x, y;
68
bmp::Pixel color;
79

8-
Shape(int x, int y, bmp::Pixel color) : x(x), y(y), color(color) {}
10+
Shape(int x, int y, bmp::Pixel color) : x(x), y(y), color(color) {
11+
}
912

1013
virtual void draw(bmp::Bitmap &image) = 0;
1114
};
1215

1316
struct Rectangle : Shape {
1417
int width, height;
1518

16-
Rectangle(int x, int y, int w, int h, bmp::Pixel color) : width(w), height(h), Shape(x, y, color) {}
19+
Rectangle(int x, int y, int w, int h, bmp::Pixel color) : width(w), height(h), Shape(x, y, color) {
20+
}
1721

1822
void draw(bmp::Bitmap &image) override {
1923
image.fill_rect(x, y, width, height, color);
@@ -24,7 +28,8 @@ struct Triangle : Shape {
2428
int x2, y2, x3, y3;
2529

2630
Triangle(int x1, int y1, int x2, int y2, int x3, int y3, bmp::Pixel color) : x2(x2), y2(y2), x3(x3), y3(y3),
27-
Shape(x1, y1, color) {}
31+
Shape(x1, y1, color) {
32+
}
2833

2934
void draw(bmp::Bitmap &image) override {
3035
image.fill_triangle(x, y, x2, y2, x3, y3, color);
@@ -34,7 +39,8 @@ struct Triangle : Shape {
3439
struct Circle : Shape {
3540
int radius;
3641

37-
Circle(int x, int y, int radius, bmp::Pixel color) : radius(radius), Shape(x, y, color) {}
42+
Circle(int x, int y, int radius, bmp::Pixel color) : radius(radius), Shape(x, y, color) {
43+
}
3844

3945
void draw(bmp::Bitmap &image) override {
4046
image.fill_circle(x, y, radius, color);
@@ -48,22 +54,21 @@ int main() {
4854
image.clear(background_color);
4955

5056
std::vector<Shape *> shapes
51-
{
52-
new Rectangle(20, 20, 180, 180, bmp::Pixel(0xa31d3a)),
53-
new Triangle(310, 20, 230, 200, 400, 200, bmp::Pixel(0x1a5096)),
54-
new Circle(500, 110, 90, bmp::Pixel(0x228035))
55-
};
57+
{
58+
new Rectangle(20, 20, 180, 180, bmp::Pixel(0xa31d3a)),
59+
new Triangle(310, 20, 230, 200, 400, 200, bmp::Pixel(0x1a5096)),
60+
new Circle(500, 110, 90, bmp::Pixel(0x228035))
61+
};
5662

5763
for (Shape *shape: shapes) {
5864
shape->draw(image);
5965
delete shape;
6066
}
61-
image.save("shapes.bmp");
67+
image.save("polymorphic_shapes.bmp");
6268

6369
return EXIT_SUCCESS;
64-
}
65-
catch (const bmp::Exception &e) {
70+
} catch (const bmp::Exception &e) {
6671
std::cerr << "[BMP ERROR]: " << e.what() << '\n';
6772
return EXIT_FAILURE;
6873
}
69-
}
74+
}

examples/read_bitmap.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ int main(void) {
1818
image.save("modified-penguin.bmp");
1919

2020
return EXIT_SUCCESS;
21-
}
22-
catch (const bmp::Exception &e) {
21+
} catch (const bmp::Exception &e) {
2322
return EXIT_FAILURE;
2423
}
25-
}
24+
}

examples/variant_shapes.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include "BitmapPlusPlus.hpp"
2+
#include <iostream>
3+
#include <variant>
4+
5+
struct Rectangle {
6+
int x, y;
7+
int width, height;
8+
bmp::Pixel color;
9+
10+
Rectangle(int x, int y, int w, int h, bmp::Pixel color)
11+
: width(w), height(h), x(x), y(y), color(color) {
12+
}
13+
14+
void draw(bmp::Bitmap &image) const {
15+
image.fill_rect(x, y, width, height, color);
16+
}
17+
};
18+
19+
struct Triangle {
20+
int x1, y1, x2, y2, x3, y3;
21+
bmp::Pixel color;
22+
23+
24+
Triangle(int x1, int y1, int x2, int y2, int x3, int y3, bmp::Pixel color)
25+
: x1(x1), y1(y1), x2(x2), y2(y2), x3(x3),
26+
y3(y3),
27+
color(color) {
28+
}
29+
30+
void draw(bmp::Bitmap &image) const {
31+
image.fill_triangle(x1, y1, x2, y2, x3, y3, color);
32+
}
33+
};
34+
35+
struct Circle {
36+
int x, y;
37+
int radius;
38+
bmp::Pixel color;
39+
40+
Circle(int x, int y, int radius, bmp::Pixel color)
41+
: radius(radius), x(x), y(y), color(color) {
42+
}
43+
44+
void draw(bmp::Bitmap &image) const {
45+
image.fill_circle(x, y, radius, color);
46+
}
47+
};
48+
49+
struct ShapeDrawer {
50+
bmp::Bitmap &bitmap;
51+
52+
explicit ShapeDrawer(bmp::Bitmap &bitmap)
53+
: bitmap(bitmap) {
54+
}
55+
56+
void operator()(const Rectangle &rectangle) const {
57+
const auto &[x, y, width, height, color] = rectangle;
58+
bitmap.fill_rect(x, y, width, height, color);
59+
}
60+
61+
void operator()(const Triangle &triangle) const {
62+
const auto &[x1,y1, x2, y2, x3, y3, color] = triangle;
63+
bitmap.fill_triangle(x1, y1, x2, y2, x3, y3, color);
64+
}
65+
66+
void operator()(const Circle &circle) const {
67+
const auto &[x, y, radius, color] = circle;
68+
bitmap.fill_circle(x, y, radius, color);
69+
}
70+
};
71+
72+
73+
int main() {
74+
try {
75+
bmp::Bitmap image(640, 256);
76+
image.clear(bmp::Teal);
77+
78+
ShapeDrawer drawer{image};
79+
80+
// No OOP polymorphism + faster
81+
// for more details, watch this great talk by Klaus Iglberger: https://www.youtube.com/watch?v=m3UmABVf55g
82+
using Shape = std::variant<Rectangle, Triangle, Circle>;
83+
Shape shapes[] = {
84+
Rectangle(20, 20, 180, 180, bmp::Gold),
85+
Triangle(310, 20, 230, 200, 400, 200, bmp::Violet),
86+
Circle(500, 110, 90, bmp::Coral)
87+
};
88+
89+
for (Shape &shape: shapes) {
90+
std::visit(drawer, shape); // visit will call the appropriate ShapeDrawer::operator() for each shape
91+
}
92+
image.save("variant_shapes.bmp");
93+
94+
return EXIT_SUCCESS;
95+
} catch (const bmp::Exception &e) {
96+
std::cerr << "[BMP ERROR]: " << e.what() << '\n';
97+
return EXIT_FAILURE;
98+
}
99+
}

examples/write_bitmap.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static bmp::Pixel random_color() {
1313
return color;
1414
}
1515

16-
int main(void) {
16+
int main() {
1717
try {
1818
// Create a 512x512 bitmap
1919
bmp::Bitmap image(512, 512);
@@ -28,9 +28,8 @@ int main(void) {
2828

2929
// And Voila!
3030
return EXIT_SUCCESS;
31-
}
32-
catch (const bmp::Exception &e) {
31+
} catch (const bmp::Exception &e) {
3332
std::cerr << "[BMP ERROR]: " << e.what() << std::endl;
3433
return EXIT_FAILURE;
3534
}
36-
}
35+
}

0 commit comments

Comments
 (0)