Educational materials

Supplemental Code for Lecture 10 - EG 10111

Caesar Cipher

Note that the code is a bit different than the one discussed in lecture. Key differences include:

  • Case for handling negative shifts (i.e. shift down by 22)
  • No longer use toASCII, convertASCII but rather use simple ASCII and char manipulations

function result = caesarCipher (Input, Key)
% usage: result = caesarCipher (Input, Key)
% Compute the Caesar cipher of the string
% Input using Key as the amount to shift  the
% string

[R,C] = size(Input);
% R should contain a 1
% C should be the size of the string (for loop bound)

if R ~= 1
  disp('Error: Not an appropriate string');
  result = Input;
  return;
end 

for i=1:C
   % Remember, C contains the string length
   % Convert to the 0 to 25 range
   result(i) = Input(i)-'A';
   %result(i) = convertASCII(Input(i));   % Works regardless of the size of key
   
   result(i) = rem((result(i) + Key),26);
   
   while result(i) < 0
       result(i) = result(i) + 26;
   end
   

   result(i) = result(i)+'A';
end 

% Convert back to a string from the numeric values
result = char(result);

Stick Figure Animation

To run the code, you will need to add in the appropriate addPath statement.

% Test code for animation

initializeWindow(0);

% Head
StickFigure(1) = drawCircle(150,150,10);
% Torso
StickFigure(2) = drawLine(150,150,150,120);
% Arm
StickFigure(3) = drawLine(135,130,165,130);
% Left leg
StickFigure(4) = drawLine(135,110,150,120);
% Right leg
StickFigure(5) = drawLine(165,110,150,120);

% Draw the extra / unseen appendages until we start moving

% Upper left
StickFigure(6) = drawLine(135,130,150,120);

% Horizontal legs in animation
StickFigure(7) = drawLine(135,120,150,120);
StickFigure(8) = drawLine(150,120,165,120);

% Upper right
StickFigure(9) = drawLine(150,120,165,130);

% Full up/down
StickFigure(10) = drawLine(150,120,150,110);

for i=6:10
    hide(StickFigure(i));
end

KeepGoing = true;
InMotion = false;

TimeCount = 0;

while KeepGoing == true
   TimeCount = TimeCount+1;
   if isKeyPressed('upArrow') == 1
      yMove(StickFigure,0.5);
      InMotion = true;
   elseif isKeyPressed('downArrow') == 1
      yMove(StickFigure,-0.5);
      InMotion = true;
   elseif isKeyPressed('leftArrow') == 1
      xMove(StickFigure,-0.5);
      InMotion = true;
   elseif isKeyPressed('rightArrow') == 1
      xMove(StickFigure,0.5);
      InMotion = true;
   else 
      InMotion = false;
   end

   if InMotion == true
     % Hide all legs
     for x=4:10
        hide(StickFigure(x));
     end

     animFrame = rem(TimeCount,4);

     if animFrame == 0
        unhide(StickFigure(6));
        unhide(StickFigure(5));
     elseif animFrame == 1
        unhide(StickFigure(7));
        unhide(StickFigure(8));
     elseif animFrame == 2
        unhide(StickFigure(4));
        unhide(StickFigure(9));
     elseif animFrame == 3
        unhide(StickFigure(10));
     end
   else
     for x=4:10
        hide(StickFigure(x));
     end       
       
      unhide(StickFigure(4));
      unhide(StickFigure(5));
   end

   redraw;  
   pause(0.05);
end
  Attachment Action Size Date Who Comment
else caesarCipher.m props, move 0.8 K 20 Nov 2007 - 21:52 AaronStriegel Caesar cipher example
else stick.m props, move 1.8 K 20 Nov 2007 - 21:45 AaronStriegel Stick figure animation example
r2 - 20 Nov 2007 - 21:52:15 - AaronStriegel
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback
Syndicate this site RSSATOM