- /
-
Dragon Egg
on 4 Nov 2024
- 46
- 451
- 0
- 1
- 1821
Cite your audio source here (if applicable):
drawframe(1);
Write your drawframe function below
function drawframe(f)
% This is a remix of Ruby by Tim.
% https://www.mathworks.com/matlabcentral/communitycontests/contests/6/entries/13952
% Fire Scene
if f >= 1 && f < 49
fire();
else
clf
set(gcf, 'color', [0 0 0] );
x=linspace(-4, 4, 400);
m=1-(erf(50*(3*x'.^2+x.^2-.5)+max(24*x.^5,-20))+1)/2; % Basic scale shape & mask
m=max(m,circshift(m,[0,5])); % Needs to be a bit longer
% Scales are slightly curved
s=m.*cos(2*x');
s=s-x.*m/3; % Needs change in height so that it overlaps well
k=m.*rescale(1./(abs(x') + 0.01))*0.1.*(min(0,x-0.3).^2); % Add keel
s=s+k+conv2(m.*randn(size(k))/100, ones(1,11)/11, 'same'); % Some textury-noise
% Now the tiling...
% Tiling the scales
so = s;
SS = 40;
for n = 1:9
so = max(so, circshift(s, n * SS));
end
so = max(so, circshift(so, [SS / 2, 40]));
for n = 1:4
so = max(so, circshift(so, [0, 80 * n]));
end
so(so < 0.7) = 0.7;
% Transform to radial distance
so=so*0.4;
so=so'+10;
so = flipud(so);
so = [so, so; so, so];
a = linspace(0, 2 * pi, size(so, 2)) - pi;
% Parametric Equation of 3d egg:(part added by Malik)
% Define u and v ranges
u = linspace(0, 2*pi, length(a));
v = linspace(0, pi, length(a));
% Create meshgrid for u and v
[U, V] = meshgrid(u, v);
% Parametric equations for x, y, and z
x1 = (1 + 0.2*V) .* cos(U) .* sin(V).* so;
y1 = (1 + 0.2*V) .* sin(U) .* sin(V) .* so;
z1 = 2 * cos(V);
if f >= 49 && f < 59
arr = cat(3, 1, 0.2, 0); % red
elseif f >= 59 && f < 69
arr=cat(3, 0.1, 0.8, 0); % green
elseif f >= 69 && f <79
arr = cat(3, 0.4, 0.5, 1); % Lavender
elseif f >= 79 && f < 89
arr = cat(3, 1,0.6,0); %Mint
elseif f >= 89 && f <= 96
arr = cat(3, 0.6,1,0.8); %Orange
end
surf(x1, y1, z1, rescale(so) .* arr, ...
'SpecularStrength', 0.5, 'DiffuseStrength', 1, 'AmbientStrength', 0);
axis off
shading flat
light('position', [0 1 0]);
lighting phong
view(174.589,15.821);
%view(174.589,15.821);
camva(10)
end
end
function fire()
%https://www.mathworks.com/matlabcentral/fileexchange/4776-fire-v1-0-v1-1
set(gcf, 'color', [0 0 0] );
% Fire characteristics
w = 50;
h = 50;
cl = 100;
b = 3;
persistent fb fbh
if isempty(fb)
fb = zeros(h, w); % Fire buffer matrix
axes('Units', 'normalized', ...
'Position', [0.01 0.01 0.98 0.98], ...
'Visible', 'off', ...
'NextPlot', 'add', ...
'YDir', 'reverse');
fbh = image(zeros(h - (2 * b), w - (2 * b)), ...
'Tag', 'FIREBUFFER');
colormap hot;
shading interp;
end
% Blur effect to the fire matrix
for i = 2:(h - 1)
for j = 2:(w - 1)
up = fb(i - 1, j);
down = fb(i + 1, j);
lt = fb(i, j - 1);
rt = fb(i, j + 1);
cell = fb(i, j);
fb(i, j) = (up + down + lt + rt + cell) / 5 - 1;
end
end
fb(1:end-1, :) = fb(2:end, :);
fb(end, :) = rand(1, w) * cl;
set(fbh, 'CData', fb((b + 1):(h - b), ...
(b + 1):(w - b)));
drawnow;
end