| Add a Mandelbrot example app, and a V kernel worth demonstrating 81540bb nandithebull 21h ago | 1 | // 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. |
| 6 | import 'dart:math' as math; |
| 7 | import 'dart:typed_data'; |
| 8 | |
| 9 | import 'package:vflutter_ffi/vflutter_ffi.dart' as v; |
| 10 | |
| 11 | Uint8List 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 | } |