computer-graphics-0/src/painter.cpp

81 lines
3.6 KiB
C++

#include <tuple>
#include "frame.hpp"
#include "drawing_assets.hpp"
#include "painter.hpp"
class DemoPainter : public Painter {
public:
void draw(PainterState *state, Frame *frame) const override {
int W = frame->width, H = frame->height;
// Размер рисунка возьмём меньше (7 / 8), чтобы он не касался границ экрана
float a = 7.0f / 8 * ((W < H) ? W - 1 : H - 1) / sqrt(2);
if (a < 1) return; // Если окно очень маленькое, то ничего не рисуем
float angle = state->angle; // Угол поворота
a = a / 2;
Point center{W / 2, H / 2};
draw_polyline(
frame, COLOR{200, 30, 45},
center.moved(a, a).rotated_around(center, angle),
center.moved(a, -a).rotated_around(center, angle),
center.moved(-a, -a).rotated_around(center, angle),
center.moved(-a, a).rotated_around(center, angle)
);
// Рисуем описанную окружность
draw_circle(frame, center, int(a * sqrt(2) + 0.5f), COLOR(100, 100, 250));
// Рисуем пиксель, на который кликнул пользователь
if (state->clicked_pixel.x >= 0 && state->clicked_pixel.x < W &&
state->clicked_pixel.y >= 0 && state->clicked_pixel.y < H)
frame->set_pixel(state->clicked_pixel.x, state->clicked_pixel.y, {34, 175, 60}); // Пиксель зелёного цвета
}
};
class Variant3S1Painter : public Painter {
public:
void draw(PainterState *state, Frame *frame) const override {
int W = frame->width, H = frame->height;
// Размер рисунка возьмём меньше (7 / 8), чтобы он не касался границ экрана
float a = 7.0f / 8 * ((W < H) ? W - 1 : H - 1);
if (a < 1) return; // Если окно очень маленькое, то ничего не рисуем
float angle = state->angle; // Угол поворота
a = a / 2;
Point center{W / 2, H / 2};
// Рисуем описанную окружность
draw_circle(frame, center, a, COLOR(0, 0, 0));
draw_polyline(
frame, COLOR{0, 200, 0},
center.moved(0, a).rotated_around(center, 0 + angle),
center.moved(0, a).rotated_around(center, +2.0944 + angle),
center.moved(0, a).rotated_around(center, -2.0944 + angle)
);
draw_circle(frame, center, a / 2, COLOR(0, 200, 0));
draw_circle(frame, center.moved(0, a * 2 / 3).rotated_around(center, angle), a / 6, COLOR(0, 200, 0));
draw_circle(frame, center.moved(0, a * 2 / 3).rotated_around(center, +2.0944 + angle), a / 6, COLOR(0, 200, 0));
draw_circle(frame, center.moved(0, a * 2 / 3).rotated_around(center, -2.0944 + angle), a / 6, COLOR(0, 200, 0));
// Рисуем пиксель, на который кликнул пользователь
if (state->clicked_pixel.x >= 0 && state->clicked_pixel.x < W &&
state->clicked_pixel.y >= 0 && state->clicked_pixel.y < H)
frame->set_pixel(state->clicked_pixel.x, state->clicked_pixel.y, {34, 175, 60}); // Пиксель зелёного цвета
}
};
static auto predefined_painters_ = std::make_tuple(
DemoPainter(),
Variant3S1Painter()
);
Painter const *const predefined_painters[] = {
&std::get<0>(predefined_painters_),
&std::get<1>(predefined_painters_),
nullptr
};