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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
extern crate drawille;
pub mod utils;
use drawille::{Canvas as BrailleCanvas};
use std::cmp;
use std::default::Default;
pub struct Chart {
width: u32,
height: u32,
xmin: f32,
xmax: f32,
ymin: f32,
ymax: f32,
canvas: BrailleCanvas,
}
pub trait Plot {
fn lineplot(&mut self, func: impl Fn(f32) -> f32) -> &mut Chart;
}
impl Default for Chart {
fn default() -> Self {
Self::new(120, 60, -10.0, 10.0)
}
}
impl Chart {
pub fn new(width: u32, height: u32, xmin: f32, xmax: f32) -> Self {
if width < 32 {
panic!("width should be more then 32, {} is provided", width);
}
if height < 32 {
panic!("height should be more then 32, {} is provided", height);
}
Self {
xmin,
xmax,
ymin: 0.0,
ymax: 0.0,
width,
height,
canvas: BrailleCanvas::new(width, height),
}
}
fn borders(&mut self) {
let w = self.width;
let h = self.height;
self.canvas.line(0, 0, 0, h);
self.canvas.line(0, 0, w, 0);
self.canvas.line(0, h, w, h);
self.canvas.line(w, 0, w, h);
}
fn vline(&mut self, i: u32) {
if i > 0 && i < self.width {
for j in 0..self.height {
if j % 3 == 0 {
self.canvas.set(i, j);
}
}
}
}
fn hline(&mut self, j: u32) {
if j > 0 && j < self.height {
for i in 0..self.width {
if i % 3 == 0 {
self.canvas.set(i, self.height - j);
}
}
}
}
pub fn display(&self) {
let frame = self.canvas.frame();
let rows = frame.split('\n').into_iter().count();
for (i, row) in frame.split('\n').into_iter().enumerate() {
if i == 1 {
println!("{0} {1:.1}", row, self.ymax);
} else if i == (rows - 2) {
println!("{0} {1:.1}", row, self.ymin);
} else {
println!("{}", row);
}
}
println!("{0: <width$.1}{1:.1}", self.xmin, self.xmax, width=(self.width as usize) / 2 - 3);
}
}
impl Plot for Chart {
fn lineplot(&mut self, func: impl Fn(f32) -> f32) -> &mut Chart {
self.borders();
let xrange = (self.xmax - self.xmin).abs();
let xstep = xrange / self.width as f32;
let ys: Vec<_> = (0..self.width)
.into_iter()
.map(|i| func(self.xmin + (i as f32) * xstep) )
.collect();
let mut ymax = *ys.iter().max_by( |x, y| x.partial_cmp(y).unwrap_or(cmp::Ordering::Equal) ).unwrap_or(&0.0);
let mut ymin = *ys.iter().min_by( |x, y| x.partial_cmp(y).unwrap_or(cmp::Ordering::Equal) ).unwrap_or(&0.0);
self.ymin = f32::min(self.ymin, ymin);
self.ymax = f32::max(self.ymax, ymax);
let margin = (self.ymax - self.ymin) * 0.05;
ymin = self.ymin - margin;
ymax = self.ymax + margin;
let yrange = ymax - ymin;
let i_center = ((xrange - self.xmax) / xrange) * self.width as f32;
self.vline(i_center as u32);
let j_center = ((yrange - ymax) / yrange) * self.height as f32;
self.hline(j_center as u32);
let points: Vec<_> = (0..self.width)
.into_iter()
.filter_map(|i| {
let x = self.xmin + (i as f32) * xstep;
let y = func(x);
if y.is_normal() {
let j = (((y - ymin) / yrange) * self.height as f32) as i32;
let j = j.max(0) as u32;
let j = j.min(self.width);
Some((i, self.height - j))
} else {
None
}
}).collect();
for pair in points.windows(2) {
let (x1, y1) = pair[0];
let (x2, y2) = pair[1];
self.canvas.line(x1, y1, x2, y2);
}
self
}
}