-
-
Notifications
You must be signed in to change notification settings - Fork 174
/
jitter.dart
88 lines (73 loc) · 2.27 KB
/
jitter.dart
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
import 'dart:math';
import 'dart:ui';
import 'package:graphic/src/algebra/varset.dart';
import 'package:graphic/src/dataflow/tuple.dart';
import 'package:graphic/src/scale/discrete.dart';
import 'package:graphic/src/scale/scale.dart';
import 'modifier.dart';
/// The specification of a jitter modifier.
///
/// The jitter mothed moves objects randomly in their local neighborhood. The random
/// distribution is uniform.
class JitterModifier extends Modifier {
/// Creates a jitter modifier.
JitterModifier({
this.ratio,
});
/// Ratio of the local neighborhood to the discrete band for each group.
///
/// If null, a default 0.5 is set.
double? ratio;
@override
bool operator ==(Object other) =>
other is JitterModifier && super == other && ratio == other.ratio;
@override
JitterGeomModifierOp toGeomModifierOp(ToGeomModifierOpParams params) {
return JitterGeomModifierOp({
'ratio': ratio ?? 0.5,
'form': params.form,
'scales': params.scales,
});
}
}
/// The jitter geometry modifier.
class JitterGeomModifier extends GeomModifier {
JitterGeomModifier(
this.ratio,
this.band,
);
/// Ratio of the local neighborhood to the discrete band for each group.
final double ratio;
/// The band for each discrete x value.
///
/// It is a ratio to the total coordinate width.
final double band;
@override
void modify(AesGroups value) {
final random = Random();
for (var group in value) {
for (var aes in group) {
final oldPosition = aes.position;
final bias = ratio * band * (random.nextDouble() - 0.5);
aes.position = oldPosition
.map(
(point) => Offset(point.dx + bias, point.dy),
)
.toList();
}
}
}
}
/// The jitter geometry modifier operator.
class JitterGeomModifierOp extends GeomModifierOp<JitterGeomModifier> {
JitterGeomModifierOp(Map<String, dynamic> params) : super(params);
@override
JitterGeomModifier evaluate() {
final ratio = params['ratio'] as double;
final form = params['form'] as AlgForm;
final scales = params['scales'] as Map<String, ScaleConv>;
final xField = form.first[0];
final band = (scales[xField] as DiscreteScaleConv).band;
return JitterGeomModifier(ratio, band);
}
}