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
|
// A straight Dart transliteration of the V kernel in src/vflutter.v.
//
// It exists purely so the example can render the *same* image both ways and
// put a number on the difference. Keep it in sync with the V version — if the
// two drift, the comparison stops being honest.
import 'dart:math' as math;
import 'dart:typed_data';
import 'package:vflutter_ffi/vflutter_ffi.dart' as v;
Uint8List renderDart(v.FractalView view) {
final out = Uint8List(view.width * view.height * 4);
final aspect = view.width / view.height;
final invW = 1.0 / view.width;
final invH = 1.0 / view.height;
final ln2 = math.log(2.0);
for (var y = 0; y < view.height; y++) {
final im = view.centerY + (y * invH - 0.5) * view.scale;
final row = y * view.width * 4;
for (var x = 0; x < view.width; x++) {
final re = view.centerX + (x * invW - 0.5) * view.scale * aspect;
var zr = 0.0, zi = 0.0, zr2 = 0.0, zi2 = 0.0;
var i = 0;
while (i < view.maxIter) {
zr2 = zr * zr;
zi2 = zi * zi;
if (zr2 + zi2 > 4.0) break;
zi = 2.0 * zr * zi + im;
zr = zr2 - zi2 + re;
i++;
}
final idx = row + x * 4;
if (i >= view.maxIter) {
out[idx + 3] = 255;
continue;
}
final mag = math.sqrt(zr2 + zi2);
var nu = i.toDouble();
if (mag > 1.0) {
nu = i + 1.0 - math.log(math.log(mag) / ln2) / ln2;
}
final t = nu / view.maxIter;
out[idx] = (255.0 * (0.5 + 0.5 * math.sin(3.0 + t * 18.0))).toInt();
out[idx + 1] = (255.0 * (0.5 + 0.5 * math.sin(3.6 + t * 18.0))).toInt();
out[idx + 2] = (255.0 * (0.5 + 0.5 * math.sin(4.2 + t * 18.0))).toInt();
out[idx + 3] = 255;
}
}
return out;
}
|