nandi/vflutter_ffipublic Fork 0
59a731a766d2baec2d522605425d2fce9c6c7209
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.

Add a Mandelbrot example app, and a V kernel worth demonstrating 81540bb · on 59a731a766d2baec2d522605425d2fce9c6c7209 · nandithebull · 20h ago
mandelbrot_dart.dart · 55 lines · 1.7 KBDart Blame HistoryRaw
 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;
}