Graphic Calculator: FIDocalcus
Проект трёх первокурсников (по инженерному практикуму в первом семестре) по созданию графического калькулятора на FLTK C++
Loading...
Searching...
No Matches
Shapes.h
Go to the documentation of this file.
1#pragma once
2
3// std libs
4#include <cmath>
5#include <functional>
6#include <stdexcept>
7#include <string>
8#include <vector>
9
10// Graph_lib
11#include "Point.h"
12#include "fltk.h"
13
14namespace Graph_lib {
15
16// (стандарт C++ не определяет никаких математических констант)
17const double pi{std::acos(-1.0)};
18
19//-------------------------------------------------------------------------------------------------------------------------------
20
21/// @brief Цвет, с учетом прозрачности
22// (пока что только полная не/прозрачность)
23struct Color {
25 red = FL_RED,
26 blue = FL_BLUE,
27 green = FL_GREEN,
28 yellow = FL_YELLOW,
29 white = FL_WHITE,
30 black = FL_BLACK,
31 magenta = FL_MAGENTA,
32 cyan = FL_CYAN,
33 dark_red = FL_DARK_RED,
34 dark_green = FL_DARK_GREEN,
35 dark_yellow = FL_DARK_YELLOW,
36 dark_blue = FL_DARK_BLUE,
37 dark_magenta = FL_DARK_MAGENTA,
38 dark_cyan = FL_DARK_CYAN
39 };
40
41 enum Transparency { invisible = 0, visible = 255 };
42
43 inline Color(Color_type _c) : c{Fl_Color(_c)}, v{visible} {}
44
46 : c{Fl_Color(_c)}, v{static_cast<unsigned char>(_v)} {}
47
48 inline Color(unsigned int _c) : c{Fl_Color(_c)}, v{visible} {}
49
51 : c{Fl_Color()}, v{static_cast<unsigned char>(_v)} {}
52
53 inline ~Color() = default;
54
55 // methods
56
57 inline unsigned int as_uint() const { return c; }
58
59 inline char visibility() const { return v; }
60
61 inline void set_visibility(Transparency _v) { v = _v; }
62
63 // ~methods
64
65 private:
66 // vars
67
68 Fl_Color c;
69 unsigned char v; // (только 0 или 1 на данный момент)
70
71 // ~vars
72};
73
74//-------------------------------------------------------------------------------------------------------------------------------
75
76struct Line_style {
78 solid = FL_SOLID, // -------
79 dash = FL_DASH, // - - - -
80 dot = FL_DOT, // .......
81 dashdot = FL_DASHDOT, // - . - .
82 dashdotdot = FL_DASHDOTDOT, // -..-..
83 };
84
85 inline Line_style(Line_style_type _s) : s{_s}, width{0} {}
86
87 inline Line_style(Line_style_type _points, pix_amount _width)
88 : s{_points}, width{_width} {}
89
90 inline Line_style(unsigned int _s) : s{_s}, width{0} {}
91
92 // methods
93
94 inline pix_amount w() const { return width; }
95
96 inline unsigned int style() const { return s; }
97
98 // ~methods
99
100 private:
101 // vars
102
103 unsigned int s;
105
106 // ~vars
107};
108
109//-------------------------------------------------------------------------------------------------------------------------------
110
111class Font {
112 public:
114 helvetica = FL_HELVETICA,
115 helvetica_bold = FL_HELVETICA_BOLD,
116 helvetica_italic = FL_HELVETICA_ITALIC,
117 helvetica_bold_italic = FL_HELVETICA_BOLD_ITALIC,
118 courier = FL_COURIER,
119 courier_bold = FL_COURIER_BOLD,
120 courier_italic = FL_COURIER_ITALIC,
121 courier_bold_italic = FL_COURIER_BOLD_ITALIC,
122 times = FL_TIMES,
123 times_bold = FL_TIMES_BOLD,
124 times_italic = FL_TIMES_ITALIC,
125 times_bold_italic = FL_TIMES_BOLD_ITALIC,
126 symbol = FL_SYMBOL,
127 screen = FL_SCREEN,
128 screen_bold = FL_SCREEN_BOLD,
129 zapf_dingbats = FL_ZAPF_DINGBATS
130 };
131
132 inline Font(Font_type _f) : f{_f} {}
133
134 inline Font(unsigned int _f) : f{_f} {}
135
136 // methods
137
138 inline unsigned int as_uint() const { return f; }
139
140 // ~methods
141
142 private:
143 // vars
144
145 unsigned int f;
146
147 // ~vars
148};
149
150//-------------------------------------------------------------------------------------------------------------------------------
151
152/// @brief Абстрактная фигура
153// (имеет дело с цветом и стилем и сохраняет последовательность строк)
154class Shape {
155 public:
156 inline Shape() = default;
157
158 inline Shape(std::initializer_list<Point> _points) {
159 for (Point point : _points) add(point);
160 }
161
162 inline Shape(const Shape&) = delete; // (предотвращение копирования)
163
164 // methods
165
166 inline Shape& operator=(const Shape&) =
167 delete; // (предотвращение копирования)
168
169 void draw() const; // (имеет дело с цветом и draw_lines)
170
171 // ~methods
172
173 protected:
174 // methods
175
176 inline void add(Point p) { points.push_back(p); }
177
178 inline void set_point(int i, Point p) { points[i] = p; }
179
180 virtual void draw_lines() const; // (просто рисует соответствующие линии)
181
182 // ~methods
183
184 public:
185 // methods
186
187 virtual void move(int dx, int dy);
188
189 inline void set_color(Color _color) { c = _color; }
190
191 inline Color color() const { return c; }
192
193 inline void set_style(Line_style sty) { ls = sty; }
194
195 inline Line_style get_style() const { return ls; }
196
197 inline void set_fill_color(Color _color) { f_c = _color; }
198
199 inline Color fill_color() const { return f_c; }
200
201 inline Point point(int i) const { return points[i]; }
202
203 inline size_t number_of_points() const { return points.size(); }
204
205 // ~methods
206
207 virtual ~Shape() = 0;
208
209 private:
210 // vars
211
212 std::vector<Point> points; // (не используется ни одним из наследников)
213 Color c{static_cast<Color>(fl_color())};
216
217 // ~vars
218};
219
220//-------------------------------------------------------------------------------------------------------------------------------
221
222/// @brief Простая линяя, сост. из двух точек
223class Line : public Shape {
224 public:
225 inline Line(Point point_1, Point point_2) {
226 add(point_1);
227 add(point_2);
228 }
229};
230
231//-------------------------------------------------------------------------------------------------------------------------------
232
233/// @brief Открытая последовательность линий
234class Open_polyline : public Shape {
235 public:
236 using Shape::Shape;
237
238 // methods
239
240 inline void add(Point p) { Shape::add(p); }
241
242 void draw_lines() const override;
243
244 // ~methods
245};
246
247//-------------------------------------------------------------------------------------------------------------------------------
248
249/// @brief Закрытая последовательность линий
251 public:
252 using Open_polyline::Open_polyline;
253
254 // methods
255
256 void draw_lines() const override;
257
258 // ~methods
259};
260
261//-------------------------------------------------------------------------------------------------------------------------------
262
263/// @brief Независимые линии
264class Lines : public Shape {
265 public:
266 inline Lines() = default;
267
268 inline Lines(std::initializer_list<Point> _points) : Shape{_points} {
269 if (_points.size() % 2)
270 throw std::invalid_argument("odd number of points for Lines");
271 }
272
273 // methods
274
275 void draw_lines() const override;
276
277 void add(Point point_1, Point point_2) {
278 Shape::add(point_1);
279 Shape::add(point_2);
280 }
281
282 // ~methods
283};
284
285//-------------------------------------------------------------------------------------------------------------------------------
286
287class Text : public Shape {
288 public:
289 // (точка находится внизу слева от первой буквы)
290 inline Text(Point _loc, const std::string& s) : lab{s} { add(_loc); }
291
292 // methods
293
294 void draw_lines() const override;
295
296 inline void set_label(const std::string& s) { lab = s; }
297
298 inline std::string label() const { return lab; }
299
300 inline void set_font(Font f) { fnt = f; }
301
302 inline Font font() const { return Font{fnt}; }
303
304 inline void set_font_size(unsigned int s) { fnt_sz = s; }
305
306 inline unsigned int font_size() const { return fnt_sz; }
307
308 // ~methods
309
310 private:
311 // vars
312
313 std::string lab;
314 Font fnt{static_cast<unsigned int>(fl_font())};
315 unsigned int fnt_sz{(14 < fl_size()) ? static_cast<unsigned int>(fl_size())
316 : 14}; // минимум 14
317
318 // ~vars
319};
320
321//-------------------------------------------------------------------------------------------------------------------------------
322
324 public:
325 inline Marked_polyline(const std::string& _mark) : mark{_mark} {}
326
327 inline Marked_polyline(const std::string& _mark,
328 std::initializer_list<Point> _points)
329 : Open_polyline{_points}, mark{_mark} {}
330
331 inline Marked_polyline(std::initializer_list<Point> _points)
332 : Open_polyline{_points} {}
333
334 // methods
335
336 void draw_lines() const override;
337
338 // ~methods
339
340 private:
341 // vars
342
343 std::string mark{"*"};
344
345 // ~vars
346};
347
348//-------------------------------------------------------------------------------------------------------------------------------
349
350class Marks : public Marked_polyline {
351 public:
352 inline Marks(const std::string& m) : Marked_polyline{m} {
354 }
355};
356
357//-------------------------------------------------------------------------------------------------------------------------------
358
359class Mark : public Marks {
360 public:
361 inline Mark(Point _loc, char c) : Marks{std::string(1, c)} { add(_loc); }
362};
363
364class Bad_image : public Fl_Image {
365 public:
366 inline Bad_image(int h, int w) : Fl_Image{h, w, 0} {}
367
368 // methods
369
370 inline void draw(int x, int y, int, int, int, int) override {
371 draw_empty(x, y);
372 }
373
374 // ~methods
375};
376
377//-------------------------------------------------------------------------------------------------------------------------------
378
379struct Suffix {
380 enum Encoding { none, png, jpg, gif, bmp };
381};
382
383Suffix::Encoding get_encoding(const std::string& s);
384
385//-------------------------------------------------------------------------------------------------------------------------------
386
387class Image : public Shape {
388 public:
389 Image(Point _loc, const std::string& s, Suffix::Encoding e = Suffix::none);
390
391 inline ~Image() { delete img_ptr; }
392
393 // methods
394
395 void draw_lines() const override;
396
397 inline void set_mask(Point _loc, pix_amount _width, pix_amount _height) {
398 width = _width;
399 height = _height;
400 cx = _loc.x;
401 cy = _loc.y;
402 }
403
404 void move(int dx, int dy) override;
405
406 // ~methods
407
408 private:
409 // vars
410
413 cy; // define "masking box" within image relative to position (cx,cy)
414 Fl_Image* img_ptr;
416
417 // ~vars
418};
419
420} // namespace Graph_lib
unsigned int pix_amount
Definition Point.h:3
Definition Shapes.h:364
Bad_image(int h, int w)
Definition Shapes.h:366
void draw(int x, int y, int, int, int, int) override
Definition Shapes.h:370
Definition Shapes.h:111
unsigned int as_uint() const
Definition Shapes.h:138
Font_type
Definition Shapes.h:113
@ times_bold_italic
Definition Shapes.h:125
@ zapf_dingbats
Definition Shapes.h:129
@ times_italic
Definition Shapes.h:124
@ courier_bold_italic
Definition Shapes.h:121
@ courier_italic
Definition Shapes.h:120
@ times
Definition Shapes.h:122
@ screen
Definition Shapes.h:127
@ helvetica_italic
Definition Shapes.h:116
@ helvetica
Definition Shapes.h:114
@ helvetica_bold
Definition Shapes.h:115
@ symbol
Definition Shapes.h:126
@ courier_bold
Definition Shapes.h:119
@ times_bold
Definition Shapes.h:123
@ courier
Definition Shapes.h:118
@ screen_bold
Definition Shapes.h:128
@ helvetica_bold_italic
Definition Shapes.h:117
Font(unsigned int _f)
Definition Shapes.h:134
Font(Font_type _f)
Definition Shapes.h:132
unsigned int f
Definition Shapes.h:145
Definition Shapes.h:387
void set_mask(Point _loc, pix_amount _width, pix_amount _height)
Definition Shapes.h:397
Text text
Definition Shapes.h:415
~Image()
Definition Shapes.h:391
pix_amount width
Definition Shapes.h:411
Image(Point _loc, const std::string &s, Suffix::Encoding e=Suffix::none)
Definition Shapes.cpp:140
void move(int dx, int dy) override
Definition Shapes.cpp:177
pix_amount height
Definition Shapes.h:411
Fl_Image * img_ptr
Definition Shapes.h:414
void draw_lines() const override
Definition Shapes.cpp:168
pix_amount cy
Definition Shapes.h:413
pix_amount cx
Definition Shapes.h:412
Простая линяя, сост. из двух точек
Definition Shapes.h:223
Line(Point point_1, Point point_2)
Definition Shapes.h:225
Независимые линии
Definition Shapes.h:264
Lines(std::initializer_list< Point > _points)
Definition Shapes.h:268
void add(Point point_1, Point point_2)
Definition Shapes.h:277
void draw_lines() const override
Definition Shapes.cpp:63
Definition Shapes.h:359
Mark(Point _loc, char c)
Definition Shapes.h:361
Definition Shapes.h:323
void draw_lines() const override
Definition Shapes.cpp:93
Marked_polyline(const std::string &_mark, std::initializer_list< Point > _points)
Definition Shapes.h:327
Marked_polyline(std::initializer_list< Point > _points)
Definition Shapes.h:331
Marked_polyline(const std::string &_mark)
Definition Shapes.h:325
std::string mark
Definition Shapes.h:343
Definition Shapes.h:350
Marks(const std::string &m)
Definition Shapes.h:352
Открытая последовательность линий
Definition Shapes.h:234
void draw_lines() const override
Definition Shapes.cpp:37
void add(Point p)
Definition Shapes.h:240
Абстрактная фигура
Definition Shapes.h:154
Line_style ls
Definition Shapes.h:214
size_t number_of_points() const
Definition Shapes.h:203
Color color() const
Definition Shapes.h:191
void set_point(int i, Point p)
Definition Shapes.h:178
void draw() const
Definition Shapes.cpp:10
Shape(std::initializer_list< Point > _points)
Definition Shapes.h:158
Shape & operator=(const Shape &)=delete
Line_style get_style() const
Definition Shapes.h:195
void set_color(Color _color)
Definition Shapes.h:189
virtual void move(int dx, int dy)
Definition Shapes.cpp:28
void add(Point p)
Definition Shapes.h:176
Color c
Definition Shapes.h:213
Shape(const Shape &)=delete
Color f_c
Definition Shapes.h:215
void set_fill_color(Color _color)
Definition Shapes.h:197
std::vector< Point > points
Definition Shapes.h:212
void set_style(Line_style sty)
Definition Shapes.h:193
virtual ~Shape()=0
Definition Shapes.cpp:35
virtual void draw_lines() const
Definition Shapes.cpp:20
Point point(int i) const
Definition Shapes.h:201
Color fill_color() const
Definition Shapes.h:199
Definition Shapes.h:287
unsigned int font_size() const
Definition Shapes.h:306
void draw_lines() const override
Definition Shapes.cpp:71
Text(Point _loc, const std::string &s)
Definition Shapes.h:290
void set_label(const std::string &s)
Definition Shapes.h:296
Font font() const
Definition Shapes.h:302
unsigned int fnt_sz
Definition Shapes.h:315
std::string lab
Definition Shapes.h:313
void set_font_size(unsigned int s)
Definition Shapes.h:304
Font fnt
Definition Shapes.h:314
std::string label() const
Definition Shapes.h:298
void set_font(Font f)
Definition Shapes.h:300
Definition Point.h:5
const double pi
Definition Shapes.h:17
Suffix::Encoding get_encoding(const std::string &s)
Definition Shapes.cpp:117
Закрытая последовательность линий
Definition Shapes.h:250
void draw_lines() const override
Definition Shapes.cpp:53
Цвет, с учетом прозрачности
Definition Shapes.h:23
unsigned int as_uint() const
Definition Shapes.h:57
Color_type
Definition Shapes.h:24
@ magenta
Definition Shapes.h:31
@ dark_cyan
Definition Shapes.h:38
@ dark_magenta
Definition Shapes.h:37
@ dark_blue
Definition Shapes.h:36
@ dark_red
Definition Shapes.h:33
@ white
Definition Shapes.h:29
@ black
Definition Shapes.h:30
@ dark_green
Definition Shapes.h:34
@ yellow
Definition Shapes.h:28
@ cyan
Definition Shapes.h:32
@ green
Definition Shapes.h:27
@ blue
Definition Shapes.h:26
@ dark_yellow
Definition Shapes.h:35
@ red
Definition Shapes.h:25
Color(Transparency _v)
Definition Shapes.h:50
Color(Color_type _c)
Definition Shapes.h:43
unsigned char v
Definition Shapes.h:69
Fl_Color c
Definition Shapes.h:68
Transparency
Definition Shapes.h:41
@ invisible
Definition Shapes.h:41
@ visible
Definition Shapes.h:41
Color(unsigned int _c)
Definition Shapes.h:48
void set_visibility(Transparency _v)
Definition Shapes.h:61
~Color()=default
Color(Color_type _c, Transparency _v)
Definition Shapes.h:45
char visibility() const
Definition Shapes.h:59
Definition Shapes.h:76
Line_style(unsigned int _s)
Definition Shapes.h:90
pix_amount w() const
Definition Shapes.h:94
Line_style(Line_style_type _points, pix_amount _width)
Definition Shapes.h:87
pix_amount width
Definition Shapes.h:104
unsigned int style() const
Definition Shapes.h:96
Line_style_type
Definition Shapes.h:77
@ dashdotdot
Definition Shapes.h:82
@ dash
Definition Shapes.h:79
@ dot
Definition Shapes.h:80
@ solid
Definition Shapes.h:78
@ dashdot
Definition Shapes.h:81
Line_style(Line_style_type _s)
Definition Shapes.h:85
unsigned int s
Definition Shapes.h:103
Definition Point.h:7
pix_amount y
Definition Point.h:30
pix_amount x
Definition Point.h:30
Definition Shapes.h:379
Encoding
Definition Shapes.h:380
@ png
Definition Shapes.h:380
@ none
Definition Shapes.h:380
@ bmp
Definition Shapes.h:380
@ gif
Definition Shapes.h:380
@ jpg
Definition Shapes.h:380