1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include "Simple_window.h"
#include "Graph.h"
#include <iostream>
double curve(double x) {return x*x; }
//---------------------------------------
class FCT:public Shape {
public:
FCT (Fct _f, double _r1, double _r2, Point _xy, int _count = 100,
double _xscale = 25, double _yscale = 25): f(_f), r1(_r1), r2(_r2),
xy(_xy), count(_count), xscale(_xscale), yscale(_yscale)
{
if(r2-r1 <= 0) error("Bad graphing range");
if(count <= 0) error("Non-positive graphing count");
dist = (r2-r1)/count;
r = r1;
for(int i=0; i<count; i++) {
add(Point(xy.x+int(r*xscale), xy.y-int(f(r)*yscale)));
r += dist;
}
}
void reset_fct(Fct _f) { reset(_f); }
void reset_orig(Point _xy) { xy = _xy; reset(); }
void reset_count(int c) { count = c; reset(); }
void reset_r1(double _r1) { r1 = _r1; reset(); }
void reset_r2(double _r2) { r2 = _r2; reset(); }
void reset_xscale(double xs) { xscale = xs; reset(); }
void reset_yscale(double ys) { yscale = ys; reset(); }
void reset(Fct f = 0) {
for(int i=0; i<count; i++) {
set_point(i,(Point(xy.x+int(r*xscale), xy.y-int(f(r)*yscale))));
r += dist;
}
}
private:
Fct f;
int count;
double r, dist, r1, r2, xscale, yscale;
Point xy;
};
//-----------------------------------------------
int main()
try
{
Simple_window win(Point(100,100), 600, 400, "test");
FCT g(curve, -10, 10, Point(200,200), 100, 25, 25);
g.reset_orig(Point(300,300));
win.attach(g);
win.wait_for_button();
return 0;
}
catch(exception& e) {
// some error reporting
return 1;
}
catch(...) {
// some more error reporting
return 2;
}
//------------------------------------------------------------------------------
|