Code covered by the BSD License  

Highlights from
Fireworks GUI

image thumbnail

Fireworks GUI

by

 

30 Dec 2010 (Updated )

Enjoy some fireworks (with sound) with this interactive GUI

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

helper.createFireworks(FWAxes, varargin)
function obj = createFireworks(FWAxes, varargin)
% createFireworks  Fireworks constructor factory
%   FWOBJ = createFireworks(FWAXOBJ) creates and returns a Fireworks (or
%   FireworksGlitter) object. The default (random) properties are selected
%   for the fireworks.
%
%   FWOBJ = createFireworks(FWAXOBJ, PROP1, VAL1, ...) creates a Fireworks
%   (or FireworksGlitter) object with specified properties.
%
%   Valid Properties:
%       FireworksType - Type of fireworks
%           String value that specifies the type of fireworks. To see a
%           complete list of valid types, type
%               <a href="matlab:helper.Fireworks.ValidFireworksType">helper.Fireworks.ValidFireworksType</a>
%           'symmetric' and 'asymmetric' are the standard-looking
%           fireworks, and one of the two is selected by default.
%
%       NumPetals - Number of petals after explosion
%           A scalar number indicating the number of spreading lines
%           (petals) after the explosion at the top. The default is an
%           integer randomly selected between 8 and 12.
%
%       LaunchPos - Horizontal launch location
%           A scalar number indicating the horizontal location of the
%           launch. Default is a random value within the x limits of the
%           axes.
%
%       LaunchVel - Launch velocity (horizontal and vertical)
%           A two-element row vector indicating the horizontal and vertical
%           velocity. Default is a pair of random values within a specific
%           range.
%
%       SpeedFactor - Speed of the fireworks
%           A scalar number indicating the speed factor. The value must be
%           between 0.5 and 1.25. A small value corresponds to faster
%           flight. Default is a random value between 0.5 and 1.25.
%
%       ThinLines - Whether the fireworks are displayed with thin lines
%           true/false. Default is false. This indicates whether to draw
%           the fireworks with thin lines or not. Typically, this should be
%           false. If you're displaying this in a very small axes, you may
%           want to set this to true.
%
%       FinishedFcn - Callback that gets called after fireworks completion
%           A function handle to a callback function that gets called after
%           the fireworks has completed. The callback should not take any
%           input arguments and produce no output arguments.

% Author: Jiro Doke
% Date: Jan 29, 2011
% Copyright 2010-2012 The MathWorks, Inc.

error(nargchk(1, inf, nargin, 'struct'))

p = inputParser;
p.addRequired('FWAxes', ...
    @(x) validateattributes(x, {'FireworksAxes'}, {'scalar'}));
p.addParamValue('FireworksType', helper.Fireworks.ValidFireworksType{round(rand)+1}, ...
    @(x) ismember(x, [{'plain', 'glitter'}, helper.Fireworks.ValidFireworksType]));
p.addParamValue('SpeedFactor', 0.75*rand + 0.5, ...
    @(x) isnumeric(x) && isscalar(x) && x >= 0.5 && x <= 1.25);
p.addParamValue('NumPetals', 8 + round(4*rand), ...
    @(x) isnumeric(x) && isscalar(x) && fix(x)==x && x>=8 && x<=12);
p.addParamValue('LaunchPos', -inf, ...
    @(x) isnumeric(x) && isscalar(x) && x >= -inf && x <= inf);
p.addParamValue('LaunchVel', [80*rand-40, 200+(60*rand-30)], ...
    @(x) isnumeric(x) && isequal(size(x), [1 2]) && ...
    all(x >= -1000) && all(x <= 1000));
p.addParamValue('HaveSound', true, ...
    @(x) islogical(x) && isscalar(x));
p.addParamValue('ThinLines', false, ...
    @(x) islogical(x) && isscalar(x));
p.addParamValue('FinishedFcn', '', ...
    @(x) validateattributes(x, {'function_handle'}, ...
    {'scalar'}));

p.FunctionName = mfilename;
p.parse(FWAxes, varargin{:});
pr = p.Results;

xl = FWAxes.XLim;
xl80 = FWAxes.XLim80;
if isinf(pr.LaunchPos)
    pr.LaunchPos = diff(xl80)*rand+xl80(1);
else
    assert(pr.LaunchPos >= xl(1) && pr.LaunchPos <= xl(2), ...
        'Fireworks:Constructor:InvalidLaunchPos', ...
        'Launch Position must be between %d and %d', round(xl(1)), round(xl(2)));
end

switch pr.FireworksType
    case 'plain'
        % Create plain fireworks and randomly choose symmetric or asymmetric shape
        if round(rand)
            pr.FireworksType = 'symmetric';
        else
            pr.FireworksType = 'asymmetric';
        end
        obj = helper.Fireworks(FWAxes, pr);
        
    case 'glitter'
        % Create glitter fireworks and randomly choose symmetric or asymmetric
        % shape
        if round(rand)
            pr.FireworksType = 'symmetric';
        else
            pr.FireworksType = 'asymmetric';
        end
        obj = helper.FireworksGlitter(FWAxes, pr);
        
    case {'symmetric', 'asymmetric'}
        % Create symmetric or asymmetric and randomly choose plain or glitter
        if round(rand)
            obj = helper.Fireworks(FWAxes, pr);
        else
            obj = helper.FireworksGlitter(FWAxes, pr);
        end
        
    otherwise
        % Create custom fireworks (glitter)
        obj = helper.FireworksGlitter(FWAxes, pr);
end

end

Contact us