Advertisement
tattersail

Graph.h

Apr 13th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.98 KB | None | 0 0
  1.  
  2. #ifndef GRAPH_GUARD
  3. #define GRAPH_GUARD 1
  4.  
  5. #include "Point.h"
  6. #include<vector>
  7. #include "std_lib_facilities.h"
  8. //#include<string>
  9. //#include<cmath>
  10. #include "fltk.h"
  11. //#include "std_lib_facilities.h"
  12.  
  13. namespace Graph_lib {
  14. // defense against ill-behaved Linux macros:
  15. #undef major
  16. #undef minor
  17.  
  18. struct Color {
  19. enum Color_type {
  20. red = FL_RED, blue = FL_BLUE, green = FL_GREEN,
  21. yellow = FL_YELLOW, white = FL_WHITE, black = FL_BLACK,
  22. magenta = FL_MAGENTA, cyan = FL_CYAN, dark_red = FL_DARK_RED,
  23. dark_green = FL_DARK_GREEN, dark_yellow = FL_DARK_YELLOW, dark_blue = FL_DARK_BLUE,
  24. dark_magenta = FL_DARK_MAGENTA, dark_cyan = FL_DARK_CYAN
  25. };
  26. enum Transparency { invisible = 0, visible = 255 };
  27.  
  28. Color(Color_type cc) :c(Fl_Color(cc)), v(visible) { }
  29. Color(Color_type cc, Transparency vv) :c(Fl_Color(cc)), v(vv) { }
  30. Color(int cc) :c(Fl_Color(cc)), v(visible) { }
  31. Color(Transparency vv) :c(Fl_Color()), v(vv) { }
  32.  
  33. int as_int() const { return c; }
  34. char visibility() const { return v; }
  35. void set_visibility(Transparency vv) { v = vv; }
  36. private:
  37. unsigned char v; // 0 or 1 for now
  38. Fl_Color c;
  39. };
  40.  
  41. struct Line_style {
  42. enum Line_style_type {
  43. solid = FL_SOLID, // -------
  44. dash = FL_DASH, // - - - -
  45. dot = FL_DOT, // .......
  46. dashdot = FL_DASHDOT, // - . - .
  47. dashdotdot = FL_DASHDOTDOT, // -..-..
  48. };
  49. Line_style(Line_style_type ss) :s(ss), w(0) { }
  50. Line_style(Line_style_type lst, int ww) :s(lst), w(ww) { }
  51. Line_style(int ss) :s(ss), w(0) { }
  52.  
  53. int width() const { return w; }
  54. int style() const { return s; }
  55. private:
  56. int s;
  57. int w;
  58. };
  59.  
  60. class Font {
  61. public:
  62. enum Font_type {
  63. helvetica = FL_HELVETICA,
  64. helvetica_bold = FL_HELVETICA_BOLD,
  65. helvetica_italic = FL_HELVETICA_ITALIC,
  66. helvetica_bold_italic = FL_HELVETICA_BOLD_ITALIC,
  67. courier = FL_COURIER,
  68. courier_bold = FL_COURIER_BOLD,
  69. courier_italic = FL_COURIER_ITALIC,
  70. courier_bold_italic = FL_COURIER_BOLD_ITALIC,
  71. times = FL_TIMES,
  72. times_bold = FL_TIMES_BOLD,
  73. times_italic = FL_TIMES_ITALIC,
  74. times_bold_italic = FL_TIMES_BOLD_ITALIC,
  75. symbol = FL_SYMBOL,
  76. screen = FL_SCREEN,
  77. screen_bold = FL_SCREEN_BOLD,
  78. zapf_dingbats = FL_ZAPF_DINGBATS
  79. };
  80.  
  81. Font(Font_type ff) :f(ff) { }
  82. Font(int ff) :f(ff) { }
  83.  
  84. int as_int() const { return f; }
  85. private:
  86. int f;
  87. };
  88.  
  89. template<class T> class Vector_ref {
  90. vector<T*> v;
  91. vector<T*> owned;
  92. public:
  93. Vector_ref() {}
  94.  
  95. Vector_ref(T* a, T* b = 0, T* c = 0, T* d = 0)
  96. {
  97. if (a) push_back(a);
  98. if (b) push_back(b);
  99. if (c) push_back(c);
  100. if (d) push_back(d);
  101. }
  102.  
  103. ~Vector_ref() { for (int i = 0; i<owned.size(); ++i) delete owned[i]; }
  104.  
  105. void push_back(T& s) { v.push_back(&s); }
  106. void push_back(T* p) { v.push_back(p); owned.push_back(p); }
  107.  
  108. // ???void erase(???)
  109.  
  110. T& operator[](int i) { return *v[i]; }
  111. const T& operator[](int i) const { return *v[i]; }
  112. int size() const { return v.size(); }
  113. };
  114.  
  115. typedef double Fct(double);
  116.  
  117. class Shape { // deals with color and style, and holds sequence of lines
  118. protected:
  119. Shape() { }
  120. Shape(initializer_list<Point> lst); // add() the Points to this Shape
  121.  
  122. // Shape() : lcolor(fl_color()),
  123. // ls(0),
  124. // fcolor(Color::invisible) { }
  125.  
  126. void add(Point p) { points.push_back(p); }
  127. void set_point(int i, Point p) { points[i] = p; }
  128. public:
  129. void draw() const; // deal with color and draw_lines
  130. protected:
  131. virtual void draw_lines() const; // simply draw the appropriate lines
  132. public:
  133. virtual void move(int dx, int dy); // move the shape +=dx and +=dy
  134.  
  135. void set_color(Color col) { lcolor = col; }
  136. Color color() const { return lcolor; }
  137.  
  138. void set_style(Line_style sty) { ls = sty; }
  139. Line_style style() const { return ls; }
  140.  
  141. void set_fill_color(Color col) { fcolor = col; }
  142. Color fill_color() const { return fcolor; }
  143.  
  144. Point point(int i) const { return points[i]; }
  145. int number_of_points() const { return int(points.size()); }
  146.  
  147. virtual ~Shape() { }
  148. /*
  149. struct Window* attached;
  150. Shape(const Shape& a)
  151. :attached(a.attached), points(a.points), line_color(a.line_color), ls(a.ls)
  152. {
  153. if (a.attached)error("attempt to copy attached shape");
  154. }
  155. */
  156. Shape(const Shape&) = delete;
  157. Shape& operator=(const Shape&) = delete;
  158. private:
  159. vector<Point> points; // not used by all shapes
  160. Color lcolor{ fl_color() };
  161. Line_style ls{ 0 };
  162. Color fcolor{ Color::invisible };
  163.  
  164. // Shape(const Shape&);
  165. // Shape& operator=(const Shape&);
  166. };
  167.  
  168. struct Function : Shape {
  169. // the function parameters are not stored
  170. Function(Fct f, double r1, double r2, Point orig, int count = 100, double xscale = 25, double yscale = 25);
  171. //Function(Point orig, Fct f, double r1, double r2, int count, double xscale = 1, double yscale = 1);
  172. };
  173.  
  174. struct Fill {
  175. Fill() :no_fill(true), fcolor(0) { }
  176. Fill(Color c) :no_fill(false), fcolor(c) { }
  177.  
  178. void set_fill_color(Color col) { fcolor = col; }
  179. Color fill_color() { return fcolor; }
  180. protected:
  181. bool no_fill;
  182. Color fcolor;
  183. };
  184.  
  185. struct Line : Shape {
  186. Line(Point p1, Point p2) { add(p1); add(p2); }
  187. };
  188.  
  189. struct Rectangle : Shape {
  190.  
  191. Rectangle(Point xy, int ww, int hh) :w{ ww }, h{ hh }
  192. {
  193. if (h <= 0 || w <= 0) error("Bad rectangle: non-positive side");
  194. add(xy);
  195. }
  196. Rectangle(Point x, Point y) :w{ y.x - x.x }, h{ y.y - x.y }
  197. {
  198. if (h <= 0 || w <= 0) error("Bad rectangle: first point is not top left");
  199. add(x);
  200. }
  201. void draw_lines() const;
  202.  
  203. // void set_fill_color(Color col) { fcolor = col; }
  204. // Color fill_color() { return fcolor; }
  205.  
  206. int height() const { return h; }
  207. int width() const { return w; }
  208. private:
  209. int h; // height
  210. int w; // width
  211. // Color fcolor; // fill color; 0 means "no fill"
  212. };
  213.  
  214. bool intersect(Point p1, Point p2, Point p3, Point p4);
  215.  
  216.  
  217. struct Open_polyline : Shape { // open sequence of lines
  218. using Shape::Shape;
  219. void add(Point p) { Shape::add(p); }
  220. void draw_lines() const;
  221. };
  222.  
  223. struct Closed_polyline : Open_polyline { // closed sequence of lines
  224. using Open_polyline::Open_polyline;
  225. void draw_lines() const;
  226.  
  227. // void add(Point p) { Shape::add(p); }
  228. };
  229.  
  230.  
  231. struct Polygon : Closed_polyline { // closed sequence of non-intersecting lines
  232. using Closed_polyline::Closed_polyline;
  233. void add(Point p);
  234. void draw_lines() const;
  235. };
  236.  
  237. struct Lines : Shape { // indepentdent lines
  238. Lines() {}
  239. Lines(initializer_list<Point> lst) : Shape{ lst } { if (lst.size() % 2) error("odd number of points for Lines"); }
  240. void draw_lines() const;
  241. void add(Point p1, Point p2) { Shape::add(p1); Shape::add(p2); }
  242. };
  243.  
  244. struct Text : Shape {
  245. // the point is the bottom left of the first letter
  246. Text(Point x, const string& s) : lab{ s } { add(x); }
  247.  
  248. void draw_lines() const;
  249.  
  250. void set_label(const string& s) { lab = s; }
  251. string label() const { return lab; }
  252.  
  253. void set_font(Font f) { fnt = f; }
  254. Font font() const { return Font(fnt); }
  255.  
  256. void set_font_size(int s) { fnt_sz = s; }
  257. int font_size() const { return fnt_sz; }
  258. private:
  259. string lab; // label
  260. Font fnt{ fl_font() };
  261. int fnt_sz{ (14<fl_size()) ? fl_size() : 14 }; // at least 14 point
  262. };
  263.  
  264.  
  265. struct Axis : Shape {
  266. // representation left public
  267. enum Orientation { x, y, z };
  268. Axis(Orientation d, Point xy, int length, int nummber_of_notches = 0, string label = "");
  269.  
  270. void draw_lines() const;
  271. void move(int dx, int dy);
  272.  
  273. void set_color(Color c);
  274.  
  275. Text label;
  276. Lines notches;
  277. // Orientation orin;
  278. // int notches;
  279. };
  280.  
  281. struct Circle : Shape {
  282. Circle(Point p, int rr) // center and radius
  283. :r{ rr } {
  284. add(Point{ p.x - r, p.y - r });
  285. }
  286.  
  287. void draw_lines() const;
  288.  
  289. Point center() const { return { point(0).x + r, point(0).y + r }; }
  290.  
  291. void set_radius(int rr) { r = rr; }
  292. int radius() const { return r; }
  293. private:
  294. int r;
  295. };
  296.  
  297.  
  298. struct Ellipse : Shape {
  299. Ellipse(Point p, int ww, int hh) // center, min, and max distance from center
  300. :w{ ww }, h{ hh } {
  301. add(Point{ p.x - ww, p.y - hh });
  302. }
  303.  
  304. void draw_lines() const;
  305.  
  306. Point center() const { return{ point(0).x + w, point(0).y + h }; }
  307. Point focus1() const { return{ center().x + int(sqrt(double(w*w - h * h))), center().y }; }
  308. Point focus2() const { return{ center().x - int(sqrt(double(w*w - h * h))), center().y }; }
  309.  
  310. void set_major(int ww) { w = ww; }
  311. int major() const { return w; }
  312. void set_minor(int hh) { h = hh; }
  313. int minor() const { return h; }
  314. private:
  315. int w;
  316. int h;
  317. };
  318. /*
  319. struct Mark : Text {
  320. static const int dw = 4;
  321. static const int dh = 4;
  322. Mark(Point xy, char c) : Text(Point(xy.x-dw, xy.y+dh),string(1,c)) {}
  323. };
  324. */
  325.  
  326. struct Marked_polyline : Open_polyline {
  327. Marked_polyline(const string& m) :mark(m) { }
  328. void draw_lines() const;
  329. private:
  330. string mark;
  331. };
  332.  
  333. struct Marks : Marked_polyline {
  334. Marks(const string& m) :Marked_polyline(m)
  335. {
  336. set_color(Color(Color::invisible));
  337. }
  338. };
  339.  
  340. struct Mark : Marks {
  341. Mark(Point xy, char c) : Marks(string(1, c)) { add(xy); }
  342. };
  343.  
  344. /*
  345.  
  346. struct Marks : Shape {
  347. Marks(char m) : mark(string(1,m)) { }
  348. void add(Point p) { Shape::add(p); }
  349. void draw_lines() const;
  350. private:
  351. string mark;
  352. };
  353. */
  354.  
  355. struct Bad_image : Fl_Image {
  356. Bad_image(int h, int w) : Fl_Image(h, w, 0) { }
  357. void draw(int x, int y, int, int, int, int) { draw_empty(x, y); }
  358. };
  359.  
  360. struct Suffix {
  361. enum Encoding { none, jpg, gif, bmp };
  362. };
  363.  
  364. Suffix::Encoding get_encoding(const string& s);
  365.  
  366. struct Image : Shape {
  367. Image(Point xy, string s, Suffix::Encoding e = Suffix::none);
  368. ~Image() { delete p; }
  369. void draw_lines() const;
  370. void set_mask(Point xy, int ww, int hh) { w = ww; h = hh; cx = xy.x; cy = xy.y; }
  371. void move(int dx, int dy) { Shape::move(dx, dy); p->draw(point(0).x, point(0).y); }
  372. private:
  373. int w, h, cx, cy; // define "masking box" within image relative to position (cx,cy)
  374. Fl_Image* p;
  375. Text fn;
  376. };
  377.  
  378. }
  379. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement