2 * psychlops_figure_visualization.h
3 * Psychlops Standard Library (Universal)
5 * Last Modified 2009/10/05 by Kenchi HOSOKAWA
6 * (C) 2009 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO
9 #include "psychlops_figure_visualization.h"
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) {
23 Rectangle::set(wid, hei);
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
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
41 double XYPlot::Xpix(double x) {
42 return left + (x-x_min) * getWidth()/(x_max-x_min);
44 double XYPlot::Ypix(double y) {
45 return bottom + (y-y_min) * getHeight()/(y_min-y_max);
47 const Color default_cols[6] = { Color::green, Color::red, Color::blue, Color::magenta, Color::cyan, Color::yellow };
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);
55 FunctionalPlot& FunctionalPlot::draw(Drawable &target) {
56 target.rect(*this, fill);
57 target.rect(*this, stroke);
58 if(functions.size()<1) return *this;
60 const double x_delta = (x_max-x_min)/getWidth();
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++) {
66 for(double x=x_min; x<x_max; x+=x_delta) {
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);
75 FunctionalPlot& FunctionalPlot::append(OneDimFun fun) {
76 functions.push_back(fun);
81 LinePlot::LinePlot() : XYPlot() { }
82 LinePlot::LinePlot(double wid, double hei) : XYPlot(wid, hei) { }
83 void LinePlot::setTableSize(int n_seq, int n_elems) {
85 for(int i=0; i<n_seq; ++i) table[i].resize(n_elems, 0);
87 LinePlot& LinePlot::set(double wid, double hei) {
88 XYPlot::set(wid, hei);
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
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);
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
112 target.polyline(elem, (j<6) ? default_cols[j] : Color::green);
\r
118 } /* <- namespace Figure */
119 } /* <- namespace Psycholops */