- /
-
Pumpkin Pyre: Turning the Ship of Flames (Green version)
on 22 Oct 2024
- 11
- 170
- 0
- 0
- 1506
Cite your audio source here (if applicable): John Carpenter - Halloween (2018) (20th Anniversary Edition) - Main Theme
drawframe(1);
Write your drawframe function below
function drawframe(f)
% Couldn't resist adding the greenish color version, for that extra Halloween vibe. 🎃
% ____________________________________________________________________________
% Inspired by and incorporating elements of the code from entry's
% by Mr. Eric Ludlam (MathWorks) and Mr. Simon Thor.
% https://www.mathworks.com/matlabcentral/communitycontests/contests/4/entries/6893
% https://www.mathworks.com/matlabcentral/communitycontests/contests/4/entries/4726
persistent p x0 y0 r k x y X Y Z m pumpkin_X pumpkin_Y pumpkin_Z;
persistent pumpkin_surf stem_surf;
if isempty(p)
% Initialize parameters for the Burning Ship fractal
p = 300; % Reduced resolution for quicker computation
x0 = -1.75; % Center x-coordinate
y0 = -0.03; % Center y-coordinate
r = 0.05; % Radius for the grid
k = 100; % Reduced iterations for quicker computation
% Precompute linspace results to avoid recomputation
x = linspace(x0 - r, x0 + r, p);
y = linspace(y0 - r, y0 + r, p);
[X, Y] = meshgrid(x, y);
% Preallocate arrays for fractal computation
Z = X + 1i * Y; % Complex grid
m = k * ones(size(Z)); % Iteration count initialization
% Vectorized fractal computation
z = Z; % Start values for iteration
for n = 1:k
absZ = abs(z);
escaped = absZ > 2; % Identify escaped points
m(escaped) = min(m(escaped), n); % Update the iteration count for escaped points
z(escaped) = NaN; % Avoid further computation for escaped points
z(~escaped) = (abs(real(z(~escaped))) + 1i * abs(imag(z(~escaped)))).^2 + Z(~escaped); % Only compute non-escaped points
end
% Create colormap with witchy green tones
cmap = [linspace(0, 0, k)', linspace(0.5, 1, k)', linspace(0, 0, k)'];
% Store the fractal image for reuse
imagesc([-3, 2], [-2, 2], m, 'AlphaData', 0.7);
colormap(cmap);
set(gca, 'Color', 'none');
axis equal off;
hold on;
% Set pumpkin position and render shape
pumpkin_x_shift = -2;
pumpkin_y_shift = 0.5;
% Precompute pumpkin sphere values if not already done
[pumpkin_X, pumpkin_Y, pumpkin_Z] = sphere(200); % Ensure high resolution
% Generate slightly irregular shape for the pumpkin
R = 1 + (-(1 - mod(0:0.1:20, 2)).^2) / 15;
% Transform to create vertical stripes
pumpkin_Y = pumpkin_Y .* R;
pumpkin_Z = pumpkin_Z .* R;
% Store the pumpkin surface
pumpkin_surf = surf(pumpkin_x_shift + pumpkin_X, pumpkin_y_shift + pumpkin_Y, ...
(.8 + (0 - (1:-0.01:-1)'.^4) * 0.2) .* pumpkin_Z, ...
'FaceColor', '#ff8c00', 'EdgeColor', 'none');
% Store the stem surface
stem_surf = surf(pumpkin_x_shift + pumpkin_X / 12, pumpkin_y_shift + pumpkin_Y / 12, ...
pumpkin_Z / 2 + 0.6, 'FaceColor', '#008000', 'EdgeColor', 'none');
end
% Rotate the pumpkin around its axis based on f
rotate(pumpkin_surf, [0, 0, 1], f);
rotate(stem_surf, [0, 0, 1], f);
% Add lighting from the right side
lighting gouraud;
camlight('right');
% Adjust view settings
axis equal off;
set(gcf, 'Color', 'black');
xlim([-3, 3]);
ylim([-2.5, 2.5]);
camva(5); % Narrow field of view
% Animate to a final view based on the input parameter f
frames = 96; % Fixed number of frames
start_azimuth = 8;
start_elevation = 48;
final_azimuth = 368;
azimuth = start_azimuth + (final_azimuth - start_azimuth) * (f / frames);
view(azimuth, start_elevation); % Update azimuth based on frame number
drawnow limitrate; % Reduce frame rate to speed up rendering
end