nandi/vflutter_ffipublic Fork 0
f7c3825dc671305345e20a62ea6ac8facbc903bb
Commits
Clone
git clone https://git.rickub.com/nandi/vflutter_ffi.git
git clone ssh://git@rickub.com/nandi/vflutter_ffi.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

mandelbrot_dart.dart · 55 lines · 1.7 KBDart Blame HistoryRaw
Add a Mandelbrot example app, and a V kernel worth demonstrating 81540bb nandithebull 7h ago1// A straight Dart transliteration of the V kernel in src/vflutter.v.
2//
3// It exists purely so the example can render the *same* image both ways and
4// put a number on the difference. Keep it in sync with the V version — if the
5// two drift, the comparison stops being honest.
6import 'dart:math' as math;
7import 'dart:typed_data';
8
9import 'package:vflutter_ffi/vflutter_ffi.dart' as v;
10
11Uint8List renderDart(v.FractalView view) {
12 final out = Uint8List(view.width * view.height * 4);
13 final aspect = view.width / view.height;
14 final invW = 1.0 / view.width;
15 final invH = 1.0 / view.height;
16 final ln2 = math.log(2.0);
17
18 for (var y = 0; y < view.height; y++) {
19 final im = view.centerY + (y * invH - 0.5) * view.scale;
20 final row = y * view.width * 4;
21 for (var x = 0; x < view.width; x++) {
22 final re = view.centerX + (x * invW - 0.5) * view.scale * aspect;
23
24 var zr = 0.0, zi = 0.0, zr2 = 0.0, zi2 = 0.0;
25 var i = 0;
26 while (i < view.maxIter) {
27 zr2 = zr * zr;
28 zi2 = zi * zi;
29 if (zr2 + zi2 > 4.0) break;
30 zi = 2.0 * zr * zi + im;
31 zr = zr2 - zi2 + re;
32 i++;
33 }
34
35 final idx = row + x * 4;
36 if (i >= view.maxIter) {
37 out[idx + 3] = 255;
38 continue;
39 }
40
41 final mag = math.sqrt(zr2 + zi2);
42 var nu = i.toDouble();
43 if (mag > 1.0) {
44 nu = i + 1.0 - math.log(math.log(mag) / ln2) / ln2;
45 }
46 final t = nu / view.maxIter;
47
48 out[idx] = (255.0 * (0.5 + 0.5 * math.sin(3.0 + t * 18.0))).toInt();
49 out[idx + 1] = (255.0 * (0.5 + 0.5 * math.sin(3.6 + t * 18.0))).toInt();
50 out[idx + 2] = (255.0 * (0.5 + 0.5 * math.sin(4.2 + t * 18.0))).toInt();
51 out[idx + 3] = 255;
52 }
53 }
54 return out;
55}