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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//! Based on a type of BestFit this module will calculate the valid data points for the given axes/canvas size
use std::{
collections::HashMap,
f32::consts::{E, PI},
};
use serde::Deserialize;
use tracing::{error, trace};
use crate::{
canvas::plot::{DataPoint, DataSymbol},
colours::Colour,
};
/// Types of curve that can be fitted to a graph
#[derive(Debug, Deserialize, Clone)]
pub enum BestFit {
/// Equation of a straight line, `y = mx + c`
Linear {
/// Incline of the line
gradient: f32,
/// The point of y-axis interception
y_intercept: f32,
/// The colour of the best fit curve
colour: Colour,
},
/// Equation of form `y = a + bx + cx^2`
Quadratic {
/// Point of interception when `x = 0`
intercept: f32,
/// Coefficient of the respective base
linear_coeff: f32,
/// Coefficient of the respective base
quadratic_coeff: f32,
/// The colour of the best fit curve
colour: Colour,
},
/// Equation of form `y = a + bx + cx^2 + dx^3`
Cubic {
/// Point of interception when `x = 0`
intercept: f32,
/// Coefficient of the respective base
linear_coeff: f32,
/// Coefficient of the respective base
quadratic_coeff: f32,
/// Coefficient of the respective base
cubic_coeff: f32,
/// The colour of the best fit curve
colour: Colour,
},
/// Equation of form `y = a + bx + cx^2 + dx^3....n`
///
/// Each `HashMap<u32, f32>` key corresponds to an `nth` order power while the value is the coefficient.
/// ```txt
/// let mut y = 0.0;
/// for (k, v) in coefficients.iter() {
/// y += v * x.powf(k as f32);
/// }
/// ```
///
/// For instance a Quartic (4th power) polynomial could be represented in `.ron` as
/// `Some(GenericPolynomial(coefficients: {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0, 4: -1.0}, colour: Black))`
GenericPolynomial {
/// Keys are powers `x` will be raised by and values are the coefficient
coefficients: HashMap<u32, f32>,
/// The colour of the best fit curve
colour: Colour,
},
/// Equation of form `y = an^(bx) + c`
Exponential {
/// Coefficient/amplitude/size of the exponential
constant: f32,
/// The base
base: f32,
/// Value exponent is multiplied by, effectively the power the exponential is raised by
power: f32,
/// Vertical offset from the origin
vertical_shift: f32,
/// The colour of the best fit curve
colour: Colour,
},
/// Probability distribution of the form `y = (o*sqrt(2pi))^-1 * e^(-(x -u)^2/2o^2)`
///
/// `y = (variance * (2.0 * PI).sqrt()).powf(-1.0) * E.powf(-(x - expected_value).powf(2.0) / (2.0 * variance.powf(2.0)))`
Gaussian {
/// Weighted average
expected_value: f32,
/// Deviation
variance: f32,
/// The colour of the best fit curve
colour: Colour,
},
// /// Equation of form `y = a(1 - n^(-bx)) + c`
// ExponentialApproach {
// constant: f32,
// base: f32,
// power: f32,
// vertical_shift: f32,
// colour: Colour,
// },
/// Equation of form `y = a * sin(bx + c) + d`
///
/// `y = amplitude * sin( period * x + phase_shift) + vertical_shift`
Sine {
/// Max size of a periodic quantity
amplitude: f32,
/// Factor indicating the amount of time to oscillaite through one period
period: f32,
/// Angle-like quantity to modify a cycle by
phase_shift: f32,
/// Vertical offset from the origin
vertical_shift: f32,
/// The colour of the best fit curve
colour: Colour,
},
/// Equation of form `y = a * cos(bx + c) + d`
///
/// `y = amplitude * cos( period * x + phase_shift) + vertical_shift`
Cosine {
/// Max size of a periodic quantity
amplitude: f32,
/// Factor indicating the amount of time to oscillaite through one period
period: f32,
/// Angle-like quantity to modify a cycle by
phase_shift: f32,
/// Vertical offset from the origin
vertical_shift: f32,
/// The colour of the best fit curve
colour: Colour,
},
}
impl BestFit {
/// Based on the type of `BestFit` curve generate its coordinates within the given bounds and a scale factor is used to create a seamless curve, i.e a large number of tightly knit points to create the illusion of a line
pub fn find_coordinates(
&self,
x_min: i32,
x_max: i32,
y_min: i32,
y_max: i32,
scale_factor: i32,
) -> Vec<DataPoint> {
match self {
BestFit::Linear {
gradient,
y_intercept,
colour,
} => {
trace!("Finding coordinates for Linear best fit line with gradient {}, y_intercept {} and between ({}, {}) and ({}, {})", gradient, y_intercept, x_min, y_min, x_max, y_max);
let mut points: Vec<DataPoint> = Vec::new();
for scaled_x in (x_min * scale_factor)..=(x_max * scale_factor) {
let x = scaled_x as f32 / scale_factor as f32;
let y = (*gradient * x) + *y_intercept;
if y > y_min as f32 && y < y_max as f32 {
points.push(DataPoint {
x,
ux: None,
y,
uy: None,
colour: *colour,
symbol: DataSymbol::Point,
symbol_radius: 1,
symbol_thickness: 1,
});
}
}
points
}
BestFit::Quadratic {
intercept,
linear_coeff,
quadratic_coeff,
colour,
} => {
trace!("Finding coordinates for Quadratic best fit line with intercept {}, linear coefficient {} and quadratic coefficient {}", intercept, linear_coeff, quadratic_coeff);
let mut points: Vec<DataPoint> = Vec::new();
for scaled_x in (x_min * scale_factor)..=(x_max * scale_factor) {
let x = scaled_x as f32 / scale_factor as f32;
let y = intercept + (linear_coeff * x) + (quadratic_coeff * x.powf(2.0));
if y > y_min as f32 && y < y_max as f32 {
points.push(DataPoint {
x,
ux: None,
y,
uy: None,
colour: *colour,
symbol: DataSymbol::Point,
symbol_radius: 1,
symbol_thickness: 1,
});
}
}
points
}
BestFit::Cubic {
intercept,
linear_coeff,
quadratic_coeff,
cubic_coeff,
colour,
} => {
trace!("Finding coordinates for Cubic best fit line with intercept {}, linear coefficient {}, quadratic coefficient {} and cubic coefficient {}", intercept, linear_coeff, quadratic_coeff, cubic_coeff);
let mut points: Vec<DataPoint> = Vec::new();
for scaled_x in (x_min * scale_factor)..=(x_max * scale_factor) {
let x = scaled_x as f32 / scale_factor as f32;
let y = intercept
+ (linear_coeff * x) + (quadratic_coeff * x.powf(2.0))
+ (cubic_coeff * x.powf(3.0));
if y > y_min as f32 && y < y_max as f32 {
points.push(DataPoint {
x,
ux: None,
y,
uy: None,
colour: *colour,
symbol: DataSymbol::Point,
symbol_radius: 1,
symbol_thickness: 1,
});
}
}
points
}
BestFit::GenericPolynomial {
coefficients,
colour,
} => {
trace!("Finding coordinates for GenericPolynomial best fit line");
let mut points: Vec<DataPoint> = Vec::new();
for scaled_x in (x_min * scale_factor)..=(x_max * scale_factor) {
let x = scaled_x as f32 / scale_factor as f32;
let mut y = 0.0;
for (k, v) in coefficients.iter() {
y += v * x.powf(*k as f32);
}
if y > y_min as f32 && y < y_max as f32 {
points.push(DataPoint {
x,
ux: None,
y,
uy: None,
colour: *colour,
symbol: DataSymbol::Point,
symbol_radius: 1,
symbol_thickness: 1,
});
}
}
points
}
BestFit::Exponential {
constant,
base,
power,
vertical_shift,
colour,
} => {
trace!("Finding coordinates for Exponential best fit line with constant {}, base {}, power {} and vertica shift {}", constant, base, power, vertical_shift);
if *base <= 0.0 {
error!("The base used in an exponential best fit must be greater than zero, you specified {}", base);
std::process::exit(1);
}
let mut points: Vec<DataPoint> = Vec::new();
for scaled_x in (x_min * scale_factor)..=(x_max * scale_factor) {
let x = scaled_x as f32 / scale_factor as f32;
let y = (constant * base.powf(power * x)) + vertical_shift;
if y > y_min as f32 && y < y_max as f32 {
points.push(DataPoint {
x,
ux: None,
y,
uy: None,
colour: *colour,
symbol: DataSymbol::Point,
symbol_radius: 1,
symbol_thickness: 1,
});
}
}
points
}
BestFit::Gaussian {
expected_value,
variance,
colour,
} => {
trace!("Finding coordinates for Gaussian best fit line with expected_value {} and varience {}", expected_value, variance);
// prevvent dividing by zero
if !variance.is_normal() {
error!("Variance cannot be zero, infinite, subnormal or NaN");
std::process::exit(1)
}
let mut points: Vec<DataPoint> = Vec::new();
for scaled_x in (x_min * scale_factor)..=(x_max * scale_factor) {
let x = scaled_x as f32 / scale_factor as f32;
let y = (variance * (2.0 * PI).sqrt()).powf(-1.0)
* E.powf(-(x - expected_value).powf(2.0) / (2.0 * variance.powf(2.0)));
if y > y_min as f32 && y < y_max as f32 {
points.push(DataPoint {
x,
ux: None,
y,
uy: None,
colour: *colour,
symbol: DataSymbol::Point,
symbol_radius: 1,
symbol_thickness: 1,
});
}
}
points
}
// BestFit::ExponentialApproach { constant, base, power, vertical_shift, colour } => {
// trace!("Finding coordinates for ExponentialApproach best fit line with constant {}, base {}, power {} and vertica shift {}", constant, base, power, vertical_shift);
// if *base <= 0.0 {
// error!("The base used in an exponential best fit must be greater than zero, you specified {}", base);
// std::process::exit(1);
// }
// let mut points: Vec<DataPoint> = Vec::new();
// for scaled_x in x_min..=(x_max * scale_factor) {
// let x = scaled_x as f32 / scale_factor as f32;
// let y = constant * (1.0 - base.powf(-power * x)) + vertical_shift;
// if y > y_min as f32 && y < y_max as f32 {
// points.push(DataPoint {
// x: x,
// ux: None,
// y: y,
// uy: None,
// colour: *colour,
// symbol: DataSymbol::Point,
// symbol_radius: 1,
// symbol_thickness: 1,
// });
// }
// }
// return points
// },
BestFit::Sine {
amplitude,
period,
phase_shift,
vertical_shift,
colour,
} => {
trace!("Finding coordinates for Sinusoidal best fit line with amplitude {}, period {}, phase shift {} and vertical shift {}", amplitude, period, phase_shift, vertical_shift);
let mut points: Vec<DataPoint> = Vec::new();
for scaled_x in (x_min * scale_factor)..=(x_max * scale_factor) {
let x = scaled_x as f32 / scale_factor as f32;
let y = amplitude * ((period * x) + phase_shift).sin() + vertical_shift;
if y > y_min as f32 && y < y_max as f32 {
points.push(DataPoint {
x,
ux: None,
y,
uy: None,
colour: *colour,
symbol: DataSymbol::Point,
symbol_radius: 1,
symbol_thickness: 1,
});
}
}
points
}
BestFit::Cosine {
amplitude,
period,
phase_shift,
vertical_shift,
colour,
} => {
trace!("Finding coordinates for Cosinusoidal best fit line with amplitude {}, period {}, phase shift {} and vertical shift {}", amplitude, period, phase_shift, vertical_shift);
let mut points: Vec<DataPoint> = Vec::new();
for scaled_x in (x_min * scale_factor)..=(x_max * scale_factor) {
let x = scaled_x as f32 / scale_factor as f32;
let y = amplitude * ((period * x) + phase_shift).cos() + vertical_shift;
if y > y_min as f32 && y < y_max as f32 {
points.push(DataPoint {
x,
ux: None,
y,
uy: None,
colour: *colour,
symbol: DataSymbol::Point,
symbol_radius: 1,
symbol_thickness: 1,
});
}
}
points
}
}
}
}