|
1 | 1 | #include "sample_draw.h" |
2 | 2 | #include <cmath> |
| 3 | +#include <random> |
| 4 | +#include <vector> |
| 5 | +#include <algorithm> |
3 | 6 |
|
4 | 7 | using namespace std; |
5 | 8 | using namespace std::experimental::drawing; |
6 | 9 |
|
7 | | -void sample_draw::operator()(::std::experimental::drawing::context& ctxt, double elapsedTimeInMilliseconds) { |
8 | | - // The is a demonstration of how a raster_source_pattern works. We create one that is 100px x 100px |
9 | | - //auto pattern = raster_source_pattern(nullptr, content::color_alpha, 100, 100); |
10 | | - //pattern.set_acquire( |
11 | | - // [pattern](void*, experimental::drawing::surface& target, const rectangle_int& extents) -> experimental::drawing::surface |
12 | | - //{ |
13 | | - // auto result = experimental::drawing::image_surface(target, format::rgb24, extents.width - extents.x, extents.height - extents.y); |
14 | | - // vector<unsigned char> data; |
15 | | - // const auto dataSize = result.get_stride() * result.get_height(); |
16 | | - // data.resize(dataSize); |
17 | | - // for (auto i = 0; i < dataSize; i += 4) { |
18 | | - // data[i + 0] = 255ui8; |
19 | | - // data[i + 1] = 0ui8; |
20 | | - // data[i + 2] = 0ui8; |
21 | | - // data[i + 3] = 0ui8; |
22 | | - // } |
23 | | - // result.set_data(data); |
24 | | - // return result; |
25 | | - //}, |
26 | | - // nullptr |
27 | | - // ); |
28 | | - //pattern.set_extend(extend::repeat); |
29 | | - //context.set_source(pattern); |
30 | | - //context.paint(); |
31 | | - |
32 | | - static double timer = 0.0; |
33 | | - timer += elapsedTimeInMilliseconds; |
34 | | - |
35 | | - ctxt.save(); |
36 | | - auto scp = solid_color_pattern(0.0, 0.0, 1.0); |
37 | | - ctxt.set_source(scp); |
38 | | - ctxt.paint(); |
39 | | - ctxt.restore(); |
40 | | - |
41 | | - ctxt.save(); |
42 | | - const int width = 100; |
43 | | - const int height = 100; |
44 | | - const format fmt = format::rgb24; |
45 | | - const int stride = format_stride_for_width(fmt, width); |
46 | | - vector<unsigned char> data; |
47 | | - for (int y = 0; y < height; y++) { |
48 | | - for (int x = 0; x < stride; x++) { |
49 | | - auto byte = x % 4; |
50 | | - switch (byte) |
51 | | - { |
52 | | - case 0: |
53 | | - data.push_back(0x7Fui8); |
54 | | - break; |
55 | | - case 1: |
56 | | - data.push_back(0xFFui8); |
57 | | - break; |
58 | | - case 2: |
59 | | - data.push_back(0ui8); |
60 | | - break; |
61 | | - case 3: |
62 | | - data.push_back(0ui8); |
63 | | - break; |
64 | | - default: |
65 | | - throw logic_error("We're MODing by 4, how do we have a value outside of [0,3]?"); |
| 10 | +vector<vector<int>> init_sort_steps(int count) { |
| 11 | + vector<vector<int>> result; |
| 12 | + result.push_back([count]() { |
| 13 | + vector<int> init; |
| 14 | + for (int i = 0; i < count; ++i) { |
| 15 | + init.push_back(i); |
| 16 | + } |
| 17 | + mt19937 rng(100); |
| 18 | + shuffle(begin(init), end(init), rng); |
| 19 | + return init; |
| 20 | + }()); |
| 21 | + bool notSorted = true; |
| 22 | + while (notSorted) { |
| 23 | + vector<int> curr(result.back()); |
| 24 | + const auto size = curr.size(); |
| 25 | + notSorted = false; |
| 26 | + for (auto i = 0U; i < size - 1; ++i) { |
| 27 | + if (curr[i] > curr[i + 1]) { |
| 28 | + notSorted = true; |
| 29 | + auto temp = curr[i + 1]; |
| 30 | + curr[i + 1] = curr[i]; |
| 31 | + curr[i] = temp; |
66 | 32 | } |
67 | 33 | } |
| 34 | + if (notSorted) { |
| 35 | + result.push_back(curr); |
| 36 | + } |
68 | 37 | } |
69 | | - auto imageSurfaceFromData = image_surface(data, fmt, width, height, stride); |
70 | | - ctxt.set_source_surface(imageSurfaceFromData, 400.0, 400.0); |
71 | | - ctxt.move_to(400.0, 400.0); |
72 | | - ctxt.rel_line_to(100.0, 0.0); |
73 | | - ctxt.rel_line_to(0.0, 100.0); |
74 | | - ctxt.rel_line_to(-100.0, 0.0); |
75 | | - ctxt.close_path(); |
76 | | - ctxt.fill(); |
77 | | - imageSurfaceFromData.finish(); |
78 | | - ctxt.restore(); |
79 | | - |
80 | | - ctxt.save(); |
81 | | - auto translucentSurface = image_surface(format::argb32, 100, 100); |
82 | | - auto ctxtForTranslucentSurface = context(translucentSurface); |
83 | | - ctxtForTranslucentSurface.set_source_rgba(0.5, 0.5, 0.5, 0.5); |
84 | | - ctxtForTranslucentSurface.set_compositing_operator(compositing_operator::clear); |
85 | | - ctxtForTranslucentSurface.paint_with_alpha(0.0); |
86 | | - ctxtForTranslucentSurface.set_source_rgba(0.5, 0.5, 0.5, 0.5); |
87 | | - ctxtForTranslucentSurface.set_compositing_operator(compositing_operator::add); |
88 | | - ctxtForTranslucentSurface.paint(); |
89 | | - translucentSurface.flush(); |
90 | | - ctxt.set_source_surface(translucentSurface, 600.0, 400.0); |
91 | | - ctxt.move_to(600.0, 400.0); |
92 | | - ctxt.rel_line_to(100.0, 0.0); |
93 | | - ctxt.rel_line_to(0.0, 100.0); |
94 | | - ctxt.rel_line_to(-100.0, 0.0); |
95 | | - ctxt.close_path(); |
96 | | - ctxt.fill(); |
97 | | - ctxt.set_source_surface(translucentSurface, 650.0, 400.0); |
98 | | - ctxt.move_to(650.0, 400.0); |
99 | | - ctxt.rel_line_to(100.0, 0.0); |
100 | | - ctxt.rel_line_to(0.0, 100.0); |
101 | | - ctxt.rel_line_to(-100.0, 0.0); |
102 | | - ctxt.close_path(); |
103 | | - ctxt.fill(); |
104 | | - ctxt.restore(); |
105 | | - |
106 | | - ctxt.save(); |
107 | | - auto ctxtTarget = ctxt.get_target(); |
108 | | - auto subsurface = surface(ctxtTarget, 10.5, 11.0, 50.0, 50.0); |
109 | | - auto subcontext = context(subsurface); |
110 | | - subcontext.set_source_rgb(0.0, 0.0, 0.0); |
111 | | - subcontext.move_to(2.0, 2.0); |
112 | | - subcontext.rel_line_to(48.0, 0.0); |
113 | | - subcontext.set_line_width(3.0); |
114 | | - subcontext.set_line_cap(line_cap::butt); |
115 | | - subcontext.stroke(); |
116 | | - ctxt.restore(); |
| 38 | + return result; |
| 39 | +} |
117 | 40 |
|
118 | | - ctxt.save(); |
119 | | - matrix m; |
120 | | - m.init_translate(300.0, 400.0); |
| 41 | +void sample_draw::operator()(::std::experimental::drawing::context& ctxt, double /*elapsedTimeInMilliseconds*/) { |
| 42 | + const int elementCount = 10; // height |
| 43 | + static vector<vector<int>> vec = init_sort_steps(elementCount); |
| 44 | + int stepCount = vec.size(); // width |
| 45 | + ctxt.set_source_rgb(0.392156899, 0.5843137503, 0.9294118285); |
| 46 | + ctxt.paint(); |
| 47 | + double left, top, right, bottom, unitWidth, unitHeight, beginX, beginY; |
| 48 | + ctxt.clip_extents(left, top, right, bottom); |
| 49 | + unitWidth = trunc(((right - left) * 0.8) / stepCount); |
| 50 | + unitHeight = trunc(((bottom - top) * 0.8) / elementCount); |
| 51 | + beginX = trunc((right - left) * 0.05); |
| 52 | + beginY = trunc((bottom - top) * 0.05); |
121 | 53 | const double two_pi = 3.1415926535897932 * 2.0; |
122 | | - m.rotate(two_pi * (fmod(timer, 4000.0) / 4000.0)); |
123 | | - ctxt.set_matrix(m); |
124 | | - ctxt.new_path(); |
125 | | - ctxt.move_to(-100.0, 0.0); |
126 | | - ctxt.line_to(100.0, 0.0); |
127 | | - ctxt.line_to(0.0, 200.0); |
128 | | - ctxt.close_path(); |
129 | | - ctxt.set_line_width(3.0); |
130 | | - ctxt.set_dash({ 0.0, 10.0 }, 0.0); |
131 | | - ctxt.set_line_cap(line_cap::round); |
132 | | - |
133 | | - ctxt.set_source_rgb(1.0, 0.0, 0.0); |
134 | | - ctxt.fill_preserve(); |
135 | | - ctxt.set_source_rgb(0.0, 0.0, 0.0); |
136 | | - ctxt.stroke(); |
137 | | - ctxt.restore(); |
138 | | - |
139 | | - ctxt.set_source_rgb(1.0, 1.0, 1.0); |
140 | | - ctxt.move_to(100.0, 100.0); |
141 | | - ctxt.select_font_face("Segoe UI", font_slant::normal, font_weight::normal); |
142 | | - ctxt.set_font_size(30.0); |
143 | | - ctxt.show_text("Hello C++!"); |
| 54 | + for (int x = 0; x < stepCount; ++x) { |
| 55 | + for (int y = 0; y < elementCount; ++y) { |
| 56 | + //ctxt.move_to(beginX * x, beginY * y); |
| 57 | + const auto radius = trunc(min(unitWidth, unitHeight) / 2.0); |
| 58 | + ctxt.arc(radius * x * 2.0 + radius + beginX + (4.0 * x), radius * y * 2.0 + radius + beginY + (4.0 * y), radius, 0.0, two_pi); |
| 59 | + double greyColor = 1.0 - (vec[x][y] / (elementCount - 1.0)); |
| 60 | + ctxt.set_source_rgb(greyColor, greyColor, greyColor); |
| 61 | + ctxt.fill(); |
| 62 | + } |
| 63 | + } |
144 | 64 | } |
0 commit comments