Advertisement
quangsoibok27a

coccoctest

Oct 6th, 2017
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. //
  2. // Created on 06/09/2017.
  3. //
  4. // You are given a graphic application using FLTK 1.3.4 (​http://www.fltk.org)
  5. // There are some errors, which were made specifically for you. Please do not change the main() function,
  6. // find and fix all the errors to get the program compiled and linked. after that, please run and check
  7. // the result, it must look similar to TestTask.png"
  8. // Please send your corrected code with a build instruction and a CV in one zip file to training@coccoc.com
  9. //
  10. // Enjoy and good luck!
  11. //
  12.  
  13. #include <iostream>
  14.  
  15. #include <FL/Fl.H>
  16. #include <FL/fl_draw.H>
  17. #include <FL/Fl_Window.H>
  18. #include <initializer_list>
  19. #include <vector>
  20. #include <functional>
  21. #include <math.h>
  22.  
  23. struct Point {                                                                      //điểm
  24.     int x,y;
  25.     Point(int xx, int yy) : x(xx), y(yy) { }
  26. };
  27.  
  28.  
  29. typedef double Fct(double);
  30.  
  31. class Shape {                                                                       // hình dạng  
  32. public:
  33.     Point point(int idx) const {
  34.         return points[idx];
  35.     }
  36.  
  37.     virtual void draw() {                                                           // bỏ const
  38.         draw_lines();
  39.     }
  40.  
  41.     std::vector<Point> get_points() { return points; }
  42.  
  43.     void add(Point p){ points.push_back(p); }
  44.  
  45. protected:
  46.     void draw_lines() const {
  47.         for (unsigned int i=1; i<points.size(); ++i)
  48.             fl_line(points[i-1].x, points[i-1].y, points[i].x, points[i].y);
  49.     }
  50.  
  51. private:
  52.     std::vector<Point> points;
  53. };
  54.  
  55. class ClosedPolyline: public Shape {
  56. public:
  57.     ClosedPolyline(std::initializer_list<Point> pp) {
  58.         if (pp.size() > 0) {
  59.             for (auto &p: pp){
  60.                 add(p);}
  61.         }
  62.     }
  63.    
  64. protected:
  65.     void draw() {
  66.         Shape::draw();
  67.         std::vector<Point> _points = Shape::get_points();
  68.         fl_line(_points[_points.size() - 1].x, _points[_points.size() - 1].y,_points[0].x, _points[0].y);
  69.   }
  70. };
  71.  
  72. class MyWindow: public Fl_Window {
  73.  public:
  74.     MyWindow(int x, int y, int w, int h, const char* title = 0)
  75.             : Fl_Window(x, y, w, h, title) {}
  76.     void Attach(Shape& s) {
  77.         shapes.push_back(&s);
  78.     }
  79.  
  80.  protected:
  81.     void draw() override {
  82.         for (auto s: shapes) {
  83.             s->draw();
  84.         }
  85.     }
  86.  
  87.  private:
  88.     std::vector<Shape*> shapes;
  89. };
  90.  
  91. class Circle: public Shape {
  92.  public:
  93.     Circle(Point p, double r): radius(r) {
  94.         add(Point{ int(p.x - r), int(p.y - r)});
  95.     }
  96.  
  97.  protected:
  98.     void draw() {                                                                                       //draw_line -> draw
  99.         fl_arc(point(0).x, point(0).y, radius + radius,radius + radius, 0, 360);
  100.     }
  101.  
  102.  private:
  103.     double radius;
  104. };
  105.  
  106.  
  107. typedef double Fct(double);
  108.  
  109.  
  110. struct Function: Shape{
  111.     Function(Fct f, double r1, double r2, Point xy, int count = 100, double xscale = 25, double yscale = 25)        // T -> Fct
  112.     {
  113.         double dist = (r2-r1)/count;
  114.         double r = r1;
  115.         for (int i = 0; i<count; ++i) {
  116.             add(Point(xy.x+int(r*xscale), xy.y-int(f(r)*yscale)));
  117.             r += dist;
  118.         }
  119.     }
  120. };
  121.  
  122. // class CosFunction: public Shape {
  123. //  public:
  124. //     double operator() (double x) {
  125. //         return cos(x * M_PI / 180);
  126. //     }
  127. // };
  128.  
  129.  
  130. int main() {
  131.     MyWindow win(100, 100, 600, 400, "C++ Test task");  // Create a simple window
  132.  
  133.     ClosedPolyline p{Point{100, 100}, Point{100, 200}, Point{500, 100}, Point{500, 200}};                      
  134.     Function f1 {[] (double x) -> double { return x * x; }, -100, 100, Point {300, 300}, 100, 20, 5};                   //remove
  135.     Function f2 {sin, -360, 360, Point{300, 300}, 200, 1, 25};
  136.     Function f3{[] (double x) -> double { return cos(x * M_PI / 180); }, -360, 360, Point{300, 300}, 200, 1, 25};
  137.     Circle c1{Point{300, 50}, 30};
  138.  
  139.     win.Attach(p);
  140.     win.Attach(f1);
  141.     win.Attach(f2);
  142.     win.Attach(f3);
  143.     win.Attach(c1);
  144.     win.end();
  145.  
  146.     win.show();
  147.     return (Fl::run());
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement