|
| 1 | +// src/CFES/fractalAlgorithm.js |
| 2 | + |
| 3 | +const generateUniqueId = () => { |
| 4 | + return 'fractal-' + Math.random().toString(36).substr(2, 9); |
| 5 | +}; |
| 6 | + |
| 7 | +// Function to generate a Mandelbrot set fractal |
| 8 | +const generateMandelbrot = (width, height, maxIterations) => { |
| 9 | + const fractal = { |
| 10 | + id: generateUniqueId(), |
| 11 | + type: 'Mandelbrot', |
| 12 | + pattern: [], |
| 13 | + }; |
| 14 | + |
| 15 | + for (let x = 0; x < width; x++) { |
| 16 | + for (let y = 0; y < height; y++) { |
| 17 | + let zx = 0; |
| 18 | + let zy = 0; |
| 19 | + let iteration = 0; |
| 20 | + const cX = (x / width) * 4 - 2; // Scale to [-2, 2] |
| 21 | + const cY = (y / height) * 4 - 2; // Scale to [-2, 2] |
| 22 | + |
| 23 | + while (zx * zx + zy * zy < 4 && iteration < maxIterations) { |
| 24 | + const tmp = zx * zx - zy * zy + cX; |
| 25 | + zy = 2 * zx * zy + cY; |
| 26 | + zx = tmp; |
| 27 | + iteration++; |
| 28 | + } |
| 29 | + |
| 30 | + // Color based on the number of iterations |
| 31 | + const color = iteration === maxIterations ? 0 : (iteration / maxIterations) * 255; |
| 32 | + fractal.pattern.push(color); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return fractal; |
| 37 | +}; |
| 38 | + |
| 39 | +// Function to generate a Julia set fractal |
| 40 | +const generateJulia = (width, height, maxIterations, cX, cY) => { |
| 41 | + const fractal = { |
| 42 | + id: generateUniqueId(), |
| 43 | + type: 'Julia', |
| 44 | + pattern: [], |
| 45 | + }; |
| 46 | + |
| 47 | + for (let x = 0; x < width; x++) { |
| 48 | + for (let y = 0; y < height; y++) { |
| 49 | + let zx = (x / width) * 4 - 2; // Scale to [-2, 2] |
| 50 | + let zy = (y / height) * 4 - 2; // Scale to [-2, 2] |
| 51 | + let iteration = 0; |
| 52 | + |
| 53 | + while (zx * zx + zy * zy < 4 && iteration < maxIterations) { |
| 54 | + const tmp = zx * zx - zy * zy + cX; |
| 55 | + zy = 2 * zx * zy + cY; |
| 56 | + zx = tmp; |
| 57 | + iteration++; |
| 58 | + } |
| 59 | + |
| 60 | + // Color based on the number of iterations |
| 61 | + const color = iteration === maxIterations ? 0 : (iteration / maxIterations) * 255; |
| 62 | + fractal.pattern.push(color); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return fractal; |
| 67 | +}; |
| 68 | + |
| 69 | +// Function to generate a fractal based on user choice |
| 70 | +const generateFractal = (type, width = 800, height = 800, maxIterations = 100, cX = -0.7, cY = 0.27015) => { |
| 71 | + if (type === 'Mandelbrot') { |
| 72 | + return generateMandelbrot(width, height, maxIterations); |
| 73 | + } else if (type === 'Julia') { |
| 74 | + return generateJulia(width, height, maxIterations, cX, cY); |
| 75 | + } else { |
| 76 | + throw new Error('Unknown fractal type'); |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +// Evolve fractal based on user feedback |
| 81 | +const evolveFractal = (fractalId, userFeedback) => { |
| 82 | + // Logic to evolve the fractal based on user feedback |
| 83 | + // This is a placeholder for actual evolution logic |
| 84 | + const evolvedFractal = { |
| 85 | + id: fractalId, |
| 86 | + pattern: [], // New evolved pattern |
| 87 | + }; |
| 88 | + |
| 89 | + // Modify the fractal pattern based on feedback (this is a simplified example) |
| 90 | + for (let i = 0; i < 100; i++) { |
| 91 | + evolvedFractal.pattern.push(Math.random() + parseFloat(userFeedback)); // Simulated evolution |
| 92 | + } |
| 93 | + |
| 94 | + return evolvedFractal; |
| 95 | +}; |
| 96 | + |
| 97 | +module.exports = { generateFractal, evolveFractal }; |
0 commit comments