Advertisement
DragonOsman

Graph.h

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