Advertisement
grumblesnake

Graph.h

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