OSDN Git Service

first
[psychlops/cpp.git] / psychlops / extension / standard / figure / psychlops_figure_visualization.cpp
1 /*
2  *  psychlops_figure_visualization.h
3  *  Psychlops Standard Library (Universal)
4  *
5  *  Last Modified 2009/10/05 by Kenchi HOSOKAWA
6  *  (C) 2009 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO
7  */
8
9 #include "psychlops_figure_visualization.h"
10
11
12 namespace Psychlops {
13 namespace Figures {
14
15         XYPlot::XYPlot() : x_max(0), x_min(0), y_max(0), y_min(0) { fill=Color(.2,.2,.6,.35); stroke=Stroke(Color::white, 3, Stroke::SOLID); }
16         XYPlot::XYPlot(double wid, double hei) : Rectangle(wid, hei), x_max(wid/2), x_min(-wid/2), y_max(hei/2), y_min(-hei/2) { fill=Color(.2,.2,.6,.35); stroke=Stroke(Color::white, 3, Stroke::SOLID); }
17         XYPlot::XYPlot(double wid, double hei, Interval x_a, Interval y_a) : Rectangle(wid, hei), x_max(x_a.end.value), x_min(x_a.begin.value), y_max(y_a.end.value), y_min(y_a.begin.value) { fill=Color(.2,.2,.6,.35); stroke=Stroke(Color::white, 3, Stroke::SOLID); }\r
18         XYPlot& XYPlot::set(double wid, double hei) {
19                 x_max = wid/2;
20                 x_min = -wid/2;
21                 y_max = hei/2;
22                 y_min = -hei/2;
23                 Rectangle::set(wid, hei);
24                 return *this;
25         }\r
26         XYPlot& XYPlot::set(double wid, double hei, Interval x_a, Interval y_a) {\r
27                 x_max = x_a.end.value;\r
28                 x_min = y_a.begin.value;\r
29                 y_max = x_a.end.value;\r
30                 y_min = y_a.begin.value;\r
31                 Rectangle::set(wid, hei);\r
32                 return *this;\r
33         }\r
34         XYPlot& XYPlot::setInterval(Interval x_a, Interval y_a) {\r
35                 x_max = x_a.end.value;\r
36                 x_min = y_a.begin.value;\r
37                 y_max = x_a.end.value;\r
38                 y_min = y_a.begin.value;\r
39                 return *this;\r
40         }
41         double XYPlot::Xpix(double x) {
42                 return left + (x-x_min) * getWidth()/(x_max-x_min);
43         }
44         double XYPlot::Ypix(double y) {
45                 return bottom + (y-y_min) * getHeight()/(y_min-y_max);
46         }
47         const Color default_cols[6] = { Color::green, Color::red, Color::blue, Color::magenta, Color::cyan, Color::yellow };
48
49         FunctionalPlot::FunctionalPlot() : XYPlot() { }
50         FunctionalPlot::FunctionalPlot(double wid, double hei) : XYPlot(wid, hei) { }
51         FunctionalPlot& FunctionalPlot::set(double wid, double hei) {
52                 XYPlot::set(wid, hei);
53                 return *this;
54         }
55         FunctionalPlot& FunctionalPlot::draw(Drawable &target) {
56                 target.rect(*this, fill);
57                 target.rect(*this, stroke);
58                 if(functions.size()<1) return *this;
59                 Line elem;
60                 const double x_delta = (x_max-x_min)/getWidth();
61                 int pix_x;
62                 if(x_max>0 && x_min<0) target.line(elem.set(Xpix(0), top, Xpix(0), bottom), Color(1,1,1,0.5));
63                 if(y_max>0 && y_min<0) target.line(elem.set(left, Ypix(0), right, Ypix(0)), Color(1,1,1,0.5));
64                 for(int i=0; i<functions.size(); i++) {
65                         pix_x = getLeft();
66                         for(double x=x_min; x<x_max; x+=x_delta) {
67                                 pix_x++;
68                                 elem.set( pix_x   , Ypix(functions[i](x)),
69                                                  (pix_x+1), Ypix(functions[i](x+x_delta)));
70                                 target.line(elem, (i<6) ? default_cols[i] : Color::green);
71                         }
72                 }
73                 return *this;
74         }
75         FunctionalPlot& FunctionalPlot::append(OneDimFun fun) {
76                 functions.push_back(fun);
77                 return *this;
78         }
79
80
81         LinePlot::LinePlot() : XYPlot() { }
82         LinePlot::LinePlot(double wid, double hei) : XYPlot(wid, hei) { }
83         void LinePlot::setTableSize(int n_seq, int n_elems) {
84                 table.resize(n_seq);
85                 for(int i=0; i<n_seq; ++i) table[i].resize(n_elems, 0);
86         }
87         LinePlot& LinePlot::set(double wid, double hei) {
88                 XYPlot::set(wid, hei);
89                 return *this;
90         }
91         void LinePlot::push_pop(double x, int seq_index) { table[seq_index].push_back(x); table[seq_index].pop_front(); }
92         LinePlot& LinePlot::draw(Drawable &target) {
93                 target.rect(*this, fill);
94                 target.rect(*this, stroke);
95                 const double x_delta = (x_max-x_min)/getWidth();\r
96                 /*\r
97                 Line elem;
98                 for(int j=0; j<table.size(); j++) {
99                         for(int i=1; i<table[j].size(); i++) {
100                                 elem.set(    i*2.0,  Ypix(table[j][i-1]),
101                                                  (i+1)*2.0,  Ypix(table[j][i]));
102                                 target.line(elem, (j<6) ? default_cols[j] : Color::green);
103                         }
104                 }\r
105                 */\r
106                 PolyLine elem;\r
107                 for(int j=0; j<table.size(); j++) {\r
108                         elem.vertices.clear();\r
109                         for(int i=0; i<table[j].size(); i++) {\r
110                                 elem.append(i, Ypix(table[j][i]));\r
111                         }\r
112                         target.polyline(elem, (j<6) ? default_cols[j] : Color::green);\r
113                 }
114                 return *this;
115         }
116
117
118 }       /*      <- namespace Figure     */
119 }       /*      <- namespace Psycholops         */