image thumbnail

Object oriented program demo: animated traffic lights on road intersection

by

 

24 Sep 2012 (Updated )

simple classes and superclass are used to arrange flexible object oriented programming environment

tLamps % trafficLamp
classdef tLamps % trafficLamp
    
    properties
        lampN, lampS, lampW, lampE
    end
    
    methods
        function obj=tLamps(X,Y)
            obj.lampN=trafficLamp(X(1),Y(1));
            obj.lampS=trafficLamp(X(2),Y(2));
            obj.lampW=trafficLamp(X(3),Y(3));
            obj.lampE=trafficLamp(X(4),Y(4));
        end
        function obj=gray2flash(obj)
            set(obj.lampN.Ly,'FaceColor','y');
            set(obj.lampS.Ly,'FaceColor','y');
            set(obj.lampW.Ly,'FaceColor','y');
            set(obj.lampE.Ly,'FaceColor','y');
            pause(2)
            obj=yellow2gray(obj);
            pause(1)
        end
        function obj=yellow2gray(obj)
            set(obj.lampN.Ly,'FaceColor',obj.lampN.g5);
            set(obj.lampS.Ly,'FaceColor',obj.lampS.g5);
            set(obj.lampW.Ly,'FaceColor',obj.lampW.g5);
            set(obj.lampE.Ly,'FaceColor',obj.lampE.g5);
        end
        function obj=gray2all(obj)
            obj.lampN=gray(obj.lampN);obj.lampS=gray(obj.lampS);
            obj.lampE=gray(obj.lampE);obj.lampW=gray(obj.lampW);
        end
        function obj=green2NS(obj)
            obj.lampN=red(obj.lampN);obj.lampS=red(obj.lampS);
            obj.lampE=green(obj.lampE);obj.lampW=green(obj.lampW);
        end
        function obj=green2WE(obj)
            obj.lampN=green(obj.lampN);obj.lampS=green(obj.lampS);
            obj.lampE=red(obj.lampE);obj.lampW=red(obj.lampW);
        end
        function obj=yellow2all(obj)
            obj.lampN=yellow(obj.lampN);obj.lampS=yellow(obj.lampS);
            obj.lampE=yellow(obj.lampE);obj.lampW=yellow(obj.lampW);
        end
        function obj=rgb2all(obj)
            %% green for NS on road crossing
            obj=green2NS(obj);
            pause(3)
            
            %% yellow for all
            obj=yellow2all(obj);
            pause(1)
            
            %% green for WE on road crossing
            obj=green2WE(obj);
            pause(4)
            %% yellow for all
            obj=yellow2all(obj);
            pause(1)
        end
    end
    
end

Contact us