Mathematica creating an ellipse

how to add a KKT Symbolic Minimization Using the Karush-Kuhn-Tucker Conditions
to my app with conditions to an ellipse and as a second choice hyperbolas

really need help with this because trying a couple of days and does not go to me

how to add KKT conditions to make ellipse and hyperbolas to this:

q = {Off[NMinimize::eit]; Off[NMinimize::lstol];
   DynamicModule[{rng, ptk, szumx, szumy, final}, 
    Panel[Column[{"Theta z przedzia?u od 0 do:", 
       RadioButtonBar[
        Dynamic[rng], {2 Pi -> "2[Pi]", Pi -> "[Pi]", 
         Pi/2 -> "[Pi]/2", Pi/8 -> "[Pi]/8"}], , 
       "Ilo?? generowanych punktów:", 
       RadioButtonBar[
        Dynamic[ptk], {5 -> "5", 20 -> "20", 50 -> "50", 
         100 -> "100"}], , "Warto?ci wektora przesuni?cia", 
       "Warto?? xi:" RadioButtonBar[
         Dynamic[szumx], {-0.1 -> "-0.1", -.5 -> "-0.5", -.9 -> 
           "-0.9", -3 -> "-3"}], 
       "Warto?? yj:" RadioButtonBar[
         Dynamic[szumy], {0.1 -> "0.1", .5 -> "0.5", .9 -> "0.9", 
          3 -> "3"}], 
       Button["Generuj Elipse", 
        While[True,(*losowo wybra? akceptowalne wspó?czynniki*){a, b, 
           c, d, f, g} = RandomReal[{-10, 10}, 6];
         [CapitalDelta] = -c d^2 + 2 b d f - a f^2 - b^2 g + a c g;
         j = -b^2 + a c; i = a + c;
         If[[CapitalDelta] != 0 && j > 0 && [CapitalDelta]/i < 0, 
          Break[]]];
        ellipse = a*x^2 + 2*b*x*y + c*y^2 + 2*d*x + 2*f*y + g;
        (*?rodek elipsy w ogólnej formie {(c d-b f)/(b^2-a c),(a f-
        b d)/(b^2-a c)}*)points = Table[theta = RandomReal[{0, rng}];

          ksol = FindRoot[(ellipse /. {x -> 
                 k*Cos[theta] + (c d - b f)/(b^2 - a c), 
                y -> k*Sin[theta] + (a f - b d)/(b^2 - a c)}) == 
             0, {k, 1.}];

          Point[{x, y}] /. {x -> 
              k*Cos[theta] + (c d - b f)/(b^2 - a c), 
             y -> k*Sin[theta] + (a f - b d)/(b^2 - a c)} /. 
           ksol, {ptk}];
        nearpoints = 
         points /. 
          Point[{x_, y_}] :> 
           Point[{x + RandomReal[{szumx, szumy}], 
             y + RandomReal[{szumx, szumy}]}];
        (*minimalne i maksymalne warto?ci x i y dla elipsy*)
        yplotrange = 
         Flatten[{y, 
           Sort[{(2*b*d - 2*a*f + 
                Sqrt[(2*b*d - 2*a*f)^2 - 
                  4*(b^2 - a*c)*(d^2 - a*g)])/(2*(-b^2 + a*c)), (-2*b*
                 d + 2*a*f + 
                Sqrt[(2*b*d - 2*a*f)^2 - 
                  4*(b^2 - a*c)*(d^2 - a*g)])/(2*(b^2 - a*c))}]}];
        xplotrange = 
         Flatten[{x, 
           Sort[{(2*c*d - 2*b*f + 
                Sqrt[(-2*c*d + 2*b*f)^2 - 
                  4*(b^2 - a*c)*(f^2 - c*g)])/(2*(b^2 - a*c)), (-2*c*
                 d + 2*b*f + 
                Sqrt[(-2*c*d + 2*b*f)^2 - 
                  4*(b^2 - a*c)*(f^2 - c*g)])/(2*(-b^2 + a*c))}]}];
        (*zminimalizowa? dystans pobliskich punktów dla nowej ogólnej \n    elpisy*)nearCoords = nearpoints[[All, 1]];
        {xs, ys} = Transpose[nearCoords];
        newellipse = aa*x^2 + 2*bb*x*y + cc*y^2 + 2*dd*x + 2*ff*y + gg;
        distance = Plus @@ (newellipse^2 /. {x -> xs, y -> ys});
        {res, coes} = NMinimize[distance, {aa, bb, cc, dd, ff, gg}];
        scaleup = 
         FromDigits[{{1}, Last@RealDigits[1/(gg /. coes)] + 1}];
        esolve = Expand[scaleup*(newellipse /. coes)];
        final = 
         Show[ContourPlot[{ellipse == 0, esolve == 0}, 
           Evaluate[xplotrange], Evaluate[yplotrange]], 
          Graphics[points], Graphics[{Red, nearpoints}], 
          ImageSize -> {500, Automatic}];], Dynamic@final
       (*,Dynamic@Grid[Join[{{"points","nearby points"}},
       Transpose[{points,nearpoints}][[All,All,1]]],Frame->All,
       Alignment->Left]*)}]]]};
Grid[{{Labeled["(A)", q], Labeled["(B)", q]}, {Labeled["(C)", q], 
   Labeled["(D)", q]}}]