(*Simulation of motion of Kharitonov rectangle
by Nasser M. Abbasi
version  Nov 28,2014*)

Manipulate[
 tick;
 
 Module[{p},
  
  If[z == 1,
   state = "STOP";
   store = {};
   np = 0
   ,
   p = Graphics[
     {EdgeForm[{Red, Thin}], White, Rectangle[{Re[k1 /. w -> z], Im[k3 /. w -> z]}, {Re[k2 /. w -> z], Im[k4 /. w -> z]}]}, 
     Axes -> True,
     Frame -> True,
     PlotRange -> {{xMin, xMax}, {yMin, yMax}}, AspectRatio -> 1, ImagePadding -> All
     ];
   
   If[buffer,
    np++;
    AppendTo[store, p];
    r = Show[store[[1 ;; np]], ImageSize -> 400, PlotRange -> {{xMin, xMax}, {yMin, yMax}}]
    ,
    r = Show[p, ImageSize -> 400, PlotRange -> {{xMin, xMax}, {yMin, yMax}}]
    ];
   
   Which[state == "RUN" || state == "STEP",
    z = z + delW;
    If[state == "RUN", tick = Not[tick]]
    ]
   ];
  
  Grid[{
    {Row[{"sweep frequency \[Omega] = ", padIt2[z, {3, 2}]}]},
    {currentPoly},
    {r}}, Spacings -> {.1, .5}]
  ],
 
 Text@Grid[{
    {
     Grid[{
       {Button[Text@Style["run", 12], {state = "RUN", If[z == 1, store = {}; np = 0; z = 0];
          tick = Not[tick]}, ImageSize -> {60, 40}],
        
        Button[Text@Style["step", 12], {state = "STEP"; If[z == 1, store = {}; np = 0; z = 0];
          tick = Not[tick]}, ImageSize -> {60, 40}],
        
        Button[Text@Style["stop", 12], {state = "STOP"; tick = Not[tick]}, ImageSize -> {60, 40}],
        
        Button[Text@Style["reset", 12], {state = "RESET";
          store = {}; np = 0; z = 0; tick = Not[tick]}, ImageSize -> {60, 40}]}
       }, Spacings -> {.5, 0}, Frame -> True, FrameStyle -> Gray
      ]
     },
    {
     Grid[
      {
       {Style["select interval polynomial", 12],
        PopupMenu[Dynamic[poly, {poly = #;
            Which[poly == "p1",
             {k1, k2, k3, k4} = {0.25 +  0.75*I*w - 3.25*w^2 - 1.25*I*w^3, 1.25 + 1.25*I*w - 2.75*w^2 -  0.25*I*w^3, 
               1.25 +  0.75*I*w - 2.75*w^2 -  1.25*I*w^3, 0.25 +  1.25*I*w - 3.25*w^2 - 0.25*I*w^3};
             delW = 1./100;
             currentPoly = 
              Style["[0.25,1.25]\!\(\*SuperscriptBox[\(s\), \(3\)]\)+[2.75,3.25]\!\(\*SuperscriptBox[\(s\), \
\(2\)]\)+[0.75,1.25]s+[0.25,1.25]", 12];
             xMax = 1.5; xMin = -3; yMin = -0.6; yMax = 1; store = {}; np = 0; z = 0
             ,
             poly == "p3",
             {k1, k2, k3, k4} = {11 + 9*I*w - 8*w^2 - 6*I*w^3 + 3*w^4 + I*w^5, 12 + 10*I*w - 7*w^2 - 5*I*w^3 + 4*w^4 + 2*I*w^5, 
               12 + 9*I*w - 7*w^2 - 6*I*w^3 + 4*w^4 + I*w^5, 11 + 10*I*w - 8*w^2 - 5*I*w^3 + 3*w^4 + 2*I*w^5};
             currentPoly = 
              Style["[1,2]\!\(\*SuperscriptBox[\(s\), \(5\)]\)+[3,4]\!\(\*SuperscriptBox[\(s\), \(4\)]\)+[5,6]\!\(\*SuperscriptBox[\
\(s\), \(3\)]\)+[7,8]\!\(\*SuperscriptBox[\(s\), \(2\)]\)+[9,10]s+[11,12]", 12];
             delW = 1./100;
             xMin = 6; xMax = 12.5; yMin = 0; yMax = 7; store = {}; np = 0; z = 0
             ,
             poly == 
              "p2", {k1, k2, k3, k4} = {0.45 +  1.95*I*w - 3.05*w^2 - 6.05*I*w^3 + 3.95*w^4 +  3.95*I*w^5 - w^6, 
               0.55 +  2.05*I*w - 2.95*w^2 -  5.95*I*w^3 + 4.05*w^4 + 
                4.05*I*w^5 - w^6, 0.55 +  1.95*I*w - 2.95*w^2 - 6.05*I*w^3 + 4.05*w^4 +
                3.95*I*w^5 - w^6, 0.45 +  2.05*I*w - 3.05*w^2 - 5.95*I*w^3 + 3.95*w^4 + 
                4.05*I*w^5 - w^6};
             delW = 1./100;
             currentPoly = 
              Style["[0.45,0.55]+[1.95,2.05]s+[2.95,3.05]\!\(\*SuperscriptBox[\(s\), \
\(2\)]\)+[5.95,6.05]\!\(\*SuperscriptBox[\(s\), \(3\)]\)+[3.95,4.05]\!\(\*SuperscriptBox[\(s\), \
\(4\)]\)+[3.95,4.05]\!\(\*SuperscriptBox[\(s\), \(5\)]\)+\!\(\*SuperscriptBox[\(s\), \(6\)]\)", 12];
             xMin = -0.3; xMax = 0.6; yMin = -0.4; yMax = 0.55; store = {}; np = 0; z = 0
             ], tick = Not[tick]
            } &],
         {"p1" -> Style["Example 5.7.4", 12],
          "p2" -> Style["Example 5.10.1", 12],
          "p3" -> Style["Example 5.5.2", 12]
          },
         ImageSize -> All], ""
        },
       
       {
        Style["buffer rectangles", 12],
        Checkbox[Dynamic[buffer, {buffer = #; store = {}; np = 0; z = delW; tick = Not[tick]} &]], ""
        }
       }, Frame -> True]
     
     }
    }, Alignment -> Center, Frame -> None
   ],
 
 {{tick, True}, None},
 {{store, {}}, None},(*buffer to save plots*)
 {{np, 0}, None},(*How many in store*)
 {{xMin, -3}, None},
 {{xMax, 1.5}, None},
 {{yMin, -0.6}, None},
 {{yMax, 1}, None},
 {{poly, "p1"}, None},
 {{delW, 1./100}, None},
 {{z, 0}, None},
 {{buffer, True}, None},
 {{state, "STOP"}, None},
 {{r, 0}, None},
 {{currentPoly, Style[
    "[0.25,1.25]\!\(\*SuperscriptBox[\(s\), \(3\)]\)+[2.75,3.25]\!\(\*SuperscriptBox[\(s\), \(2\)]\)+[0.75,1.25]s+[0.25,1.25]", 
    12]}, None},
 
 TrackedSymbols :> {tick},
 Initialization :> (
   {k1, k2, k3, k4} = {0.25 + (0. + 0.75*I)*w - 3.25*w^2 - (0. + 1.25*I)*w^3, 1.25 + 1.25*I*w - 2.75*w^2 -  0.25*I*w^3, 
     1.25 +  0.75*I*w - 2.75*w^2 -  1.25*I*w^3, 0.25 +  1.25*I*w - 3.25*w^2 - 0.25*I*w^3};
   integerStrictPositive = (IntegerQ[#] && # > 0 &);
   integerPositive = (IntegerQ[#] && # >= 0 &);
   numericStrictPositive = (Element[#, Reals] && # > 0 &);
   numericPositive = (Element[#, Reals] && # >= 0 &);
   numericStrictNegative = (Element[#, Reals] && # < 0 &);
   numericNegative = (Element[#, Reals] && # <= 0 &);
   bool = (Element[#, Booleans] &);
   numeric = (Element[#, Reals] &);
   integer = (Element[#, Integers] &);
   (*--------------------------------------------*)
   padIt1[v_?numeric, f_List] := AccountingForm[v, f, NumberSigns -> {"-", "+"}, NumberPadding -> {"0", "0"}, SignPadding -> True];
   (*--------------------------------------------*)
   padIt1[v_?numeric, f_Integer] := 
    AccountingForm[Chop[v], f, NumberSigns -> {"-", "+"}, NumberPadding -> {"0", "0"}, SignPadding -> True];
   (*--------------------------------------------*)
   padIt2[v_?numeric, f_List] := AccountingForm[v, f, NumberSigns -> {"", ""}, NumberPadding -> {"0", "0"}, SignPadding -> True];
   (*--------------------------------------------*)
   padIt2[v_?numeric, f_Integer] := 
    AccountingForm[Chop[v], f, NumberSigns -> {"", ""}, NumberPadding -> {"0", "0"}, SignPadding -> True];
   (*--------------------------------------------*)
   )
 
 
 ]