TX Library Help – Version: 00173a, Revision: 174
 ALL  Windows graphics in a sandbox

MathBeauty.cpp

См. документацию.
00001 //{===========================================================================
00010 //          (C) Филипп Куликов, 7 класс, 2006
00011 //}===========================================================================
00012 
00013 #include "FilippLib.h"
00014 
00015 //----------------------------------------------------------------------------------------------------------------------
00016 
00017 struct Button
00018     {
00019     int x, y;
00020     int lengh, hight;
00021 
00022     COLORREF setColor, fillColor;
00023 
00024     const char* text;
00025     COLORREF setTextColor;
00026 
00027     int number;
00028     int hotKey;
00029     double (*grafic) (double x, const double kF[]);
00030     char yFormula [125];
00031 
00032     void drawButton();
00033     };
00034 
00035 //----------------------------------------------------------------------------------------------------------------------
00036 
00037 void buildGrafics      (HDC desk, HDC book, HDC voprosImage);
00038 int  controlGrafics    (double kF[]);
00039 void deskKF            (const double kF[], int n);
00040 void DrawButtons       (Button object[], int size);
00041 int  CheckButtons      (Button object[], int size);
00042 int  CheckMouseButtons (Button object[], int size);
00043 
00044 int fIn (double xMin, double x, double xMax, double yMin, double y, double yMax);
00045 void formula (double kF[], char yFormula[]);
00046 void Lines (bool grid);
00047 void signingOfTheAxes();
00048 void InformationDesk();
00049 void ShadowRect (int x1, int y1, int x2, int y2, COLORREF fillColor, COLORREF countourColor);
00050 void ShadowText (int x1, int y1, int x2, int y2, COLORREF color, const char text[]);
00051 
00052 //----------------------------------------------------------------------------------------------------------------------
00053 
00054 int main()
00055     {
00056     HDC desk        = txLoadImage ("Desk.bmp");
00057     HDC book        = txLoadImage ("Book.bmp");
00058     HDC voprosImage = txLoadImage ("Vopros.bmp");
00059 
00060     txCreateWindow (1300, 700);
00061     txTextCursor (false);
00062     txDisableAutoPause();
00063 
00064     buildGrafics (desk, book, voprosImage);
00065 
00066     txDeleteDC (desk);
00067     txDeleteDC (book);
00068     txDeleteDC (voprosImage);
00069 
00070     while (_kbhit()) _getch();
00071 
00072     return 0;
00073     }
00074 
00075 //----------------------------------------------------------------------------------------------------------------------
00076 
00077 void buildGrafics (HDC desk, HDC book, HDC voprosImage)
00078     {
00079     //                   x    y    lengh hight setColor       fillColor    name text           setTextColor  number  function     formula text
00080     Button object[] = { {975, 110, 300,  60,   CountourColor, ButtonColor, "Линейная функция", TextColor,    0, '1', liner,       "y = %.1f + x * %.1f"              },
00081                         {975, 210, 300,  60,   CountourColor, ButtonColor, "Парабола",         TextColor,    1, '2', parabola,    "y = %.1f + %.1f * x + %.1f * x^2" },
00082                         {975, 310, 300,  60,   CountourColor, ButtonColor, "Радиоволны",       TextColor,    2, '3', radio_waves, "y = %.1f + %.1f*x + %.1f * sin (x) / tan (x) * x^3 / 10" },
00083                         {975, 410, 300,  60,   CountourColor, ButtonColor, "Большая Арка!!",   TextColor,    3, '4', big_arc,     "y = %.1f + x*x/100 / %.1f * %.1f" },
00084                         {975, 510, 300,  60,   CountourColor, ButtonColor, "Синус",            TextColor,    4, '5', sinus,       "y = %.1f + sin (x * %.1f) * %.1f" } };
00085 
00086     int size = sizeof (object) / sizeof (object [0]);
00087 
00088     int oldMousePressed  = 0;
00089     int mousePressed = 0;
00090 
00091     int drawButtons = 0;
00092     int vopros = 0;
00093 
00094     const int n = 3; // c  b  k
00095     double kF [n] =  {  0, 1, 0.5 };
00096 
00097     txBegin();
00098 
00099     int numberOfGrafic = 0;
00100 
00101     while (!GetAsyncKeyState (VK_ESCAPE))
00102         {
00103         int button = CheckButtons (object, size);
00104         if (button != -1) numberOfGrafic = button;
00105 
00106         controlGrafics (kF);
00107 
00108         txSetFillColor (TX_BLACK);
00109         txClear();
00110 
00111         alphaBlend (0, 0, LenghDesk, HightDesk, desk, 0, 0, (txGetExtentX (desk)), (txGetExtentY (desk)), 1, 2);
00112 
00113         Lines (true);
00114 
00115         const COLORREF veryGray = RGB (15, 15, 15);
00116         txSetColor     (veryGray);
00117         txSetFillColor (veryGray);
00118 
00119         txdrawGrafics (-50, 50, object [numberOfGrafic].grafic, WHITE, 0.1, 5, kF);
00120 
00121         signingOfTheAxes();
00122 
00123         formula (kF, object[numberOfGrafic].yFormula);
00124 
00125         txTransparentBlt (txDC(), 1240,  45, 55, 47, book);
00126         txTransparentBlt (txDC(), 1170,  45, 50, 50, voprosImage);
00127 
00128         double x = txMousePos().x, y = txMousePos().y;
00129 
00130         oldMousePressed = mousePressed;
00131         mousePressed = txMouseButtons() & 1;
00132 
00133         if (!oldMousePressed && mousePressed)
00134             {
00135             if (fIn (1240, x, 1295, 45, y, 92)) drawButtons = !drawButtons;
00136             if (fIn (1170, x, 1220, 45, y, 90)) vopros      = !vopros;
00137             }
00138 
00139         if (vopros || GetAsyncKeyState (VK_F1))
00140             InformationDesk();
00141 
00142         if (drawButtons || GetAsyncKeyState (VK_F2))
00143             DrawButtons (object, size);
00144 
00145         txSleep();
00146         }
00147 
00148     txEnd();
00149     }
00150 
00151 //----------------------------------------------------------------------------------------------------------------------
00152 
00153 int controlGrafics (double kF[])
00154     {
00155     int difference = 0;
00156 
00157     if (GetAsyncKeyState ('C'))
00158         {
00159         if (GetAsyncKeyState (VK_UP))    kF [0] += 0.08;
00160         if (GetAsyncKeyState (VK_DOWN))  kF [0] -= 0.08;
00161         if (GetAsyncKeyState (VK_SPACE)) kF [0]  = 1;
00162 
00163         difference++;
00164         }
00165 
00166     if (GetAsyncKeyState ('B'))
00167         {
00168         if (GetAsyncKeyState (VK_UP))    kF [1] += 0.1;
00169         if (GetAsyncKeyState (VK_DOWN))  kF [1] -= 0.1;
00170         if (GetAsyncKeyState (VK_SPACE)) kF [1]  = 1;
00171 
00172         difference++;
00173         }
00174 
00175     if (GetAsyncKeyState ('K'))
00176         {
00177         if (GetAsyncKeyState (VK_UP))    kF [2] += 0.2;
00178         if (GetAsyncKeyState (VK_DOWN))  kF [2] -= 0.2;
00179         if (GetAsyncKeyState (VK_SPACE)) kF [2]  = 1;
00180 
00181         difference++;
00182         }
00183 
00184     if (difference == 0 && GetAsyncKeyState (VK_SPACE))
00185         {
00186         kF [0] = 0;
00187         kF [1] = 0;
00188         kF [2] = 1;
00189 
00190         difference++;
00191         }
00192 
00193     return difference;
00194     }
00195 
00196 //----------------------------------------------------------------------------------------------------------------------
00197 
00198 void formula (double kF[], char yFormula[])
00199     {
00200     char str [100] = "";
00201     sprintf (str, yFormula, kF [0], kF [1], kF [2]);
00202 
00203     txSelectFont ("Comic Sans MS", 40);
00204     txSetTextAlign (TA_LEFT);
00205 
00206     ShadowRect (LeftOfTheGrafic + 3, TopOfTheGrafic + 10,
00207                 LeftOfTheGrafic + 30 + txGetTextExtentX (str),
00208                 TopOfTheGrafic  + 25 + txGetTextExtentY (str),
00209                 ButtonColor, CountourColor);
00210 
00211     ShadowText (LeftOfTheGrafic + 15, TopOfTheGrafic + 15, 0, 0, TX_YELLOW, str);
00212     }
00213 
00214 //----------------------------------------------------------------------------------------------------------------------
00215 
00216 void Button::drawButton()
00217     {
00218     txSelectFont ("Comic Sans MS", 40);
00219 
00220     ShadowRect (x, y, x + lengh, y + hight, fillColor, setColor);
00221     ShadowText (x, y, x + lengh, y + hight, setTextColor, text);
00222     }
00223 
00224 //----------------------------------------------------------------------------------------------------------------------
00225 
00226 void DrawButtons (Button object[], int size)
00227     {
00228     int i = 0;
00229     while (i < size)
00230         {
00231         object [i].drawButton();
00232         i++;
00233         }
00234     }
00235 
00236 //----------------------------------------------------------------------------------------------------------------------
00237 
00238 int CheckMouseButtons (Button object[], int size)
00239     {
00240     if (!txMouseButtons() & 1) return -1;
00241 
00242     int i = 0;
00243     while (i < size)
00244         {
00245         double xMin = object [i].x, xMax = object [i].x + object [i].lengh;
00246         double yMin = object [i].y, yMax = object [i].y + object [i].hight;
00247         double x = txMousePos().x, y = txMousePos().y;
00248 
00249         if (fIn (xMin, x, xMax, yMin, y, yMax))
00250             {
00251             return i;
00252             }
00253 
00254         i++;
00255         }
00256 
00257     return -1;
00258     }
00259 
00260 //----------------------------------------------------------------------------------------------------------------------
00261 
00262 int CheckButtons (Button object[], int size)
00263     {
00264     int i = 0;
00265     while (i < size)
00266         {
00267         if (GetAsyncKeyState (object [i].hotKey) ||
00268            (fIn (object [i].x, txMouseX(), object [i].x + object [i].lengh,
00269                  object [i].y, txMouseY(), object [i].y + object [i].hight) && (txMouseButtons() & 1)))
00270             {
00271             return i;
00272             }
00273 
00274         i++;
00275         }
00276 
00277     return -1;
00278     }
00279 
00280 //----------------------------------------------------------------------------------------------------------------------
00281 
00282 int fIn (double xMin, double x, double xMax,
00283          double yMin, double y, double yMax)
00284     {
00285     return (xMin <= x && x <= xMax) &&
00286            (yMin <= y && y <= yMax);
00287     }
00288 
00289 //----------------------------------------------------------------------------------------------------------------------
00290 
00291 void Lines (bool grid)
00292     {
00293     txSetColor (TX_RED, 2);
00294     txLine (Xcentre+1, TopOfTheGrafic + 7, Xcentre+1, BottomOfTheGrafic - 7);
00295     txLine (LeftOfTheGrafic + 10, Ycentre+1, RightOfTheGrafic - 10, Ycentre+1);
00296     txLine (Xcentre-10, TopOfTheGrafic+20, Xcentre+1, TopOfTheGrafic+7);
00297     txLine (Xcentre+11, TopOfTheGrafic+20, Xcentre+1, TopOfTheGrafic+7);
00298     txLine (RightOfTheGrafic-23, Ycentre+11, RightOfTheGrafic-10, Ycentre+1);
00299     txLine (RightOfTheGrafic-23, Ycentre-11, RightOfTheGrafic-10, Ycentre+1);
00300 
00301     txSelectFont ("Comic Sans MS", 25);
00302     txSetTextAlign (TA_LEFT);
00303 
00304     txTextOut (txGetExtentX()/2 - 25, TopOfTheGrafic + 13,   "Y");
00305     txTextOut (RightOfTheGrafic - 25, txGetExtentY()/2 + 15, "X");
00306 
00307     if (grid)
00308         {
00309         Pen();
00310 
00311         int Xnum = LeftOfTheGrafic;
00312         while (Xnum < RightOfTheGrafic-20)
00313             {
00314             Xnum += 20;
00315             txLine (Xnum, TopOfTheGrafic, Xnum, BottomOfTheGrafic);
00316             }
00317 
00318         int Ynum = TopOfTheGrafic-10;
00319         while (Ynum < BottomOfTheGrafic-20)
00320             {
00321             Ynum += 20;
00322             txLine (LeftOfTheGrafic, Ynum, RightOfTheGrafic, Ynum);
00323             }
00324         }
00325     }
00326 
00327 //----------------------------------------------------------------------------------------------------------------------
00328 
00329 void signingOfTheAxes()
00330     {
00331     txSetColor (TX_RED);
00332     txSelectFont ("Comic Sans MS", 25);
00333     txSetTextAlign (TA_RIGHT);
00334 
00335     int i = 1;
00336     int y = Ycentre-90;
00337     while (y >= TopOfTheGrafic - 40)
00338         {
00339         char str [20]= "";
00340         sprintf (str, "%d", i);
00341 
00342         txTextOut (Xcentre-10, TopOfTheGrafic  + y, str);
00343 
00344         i++;
00345         y -= 2*Step;
00346         }
00347 
00348     int i2 = 1;
00349     int y2 = Ycentre-10;
00350     while (y2 <= BottomOfTheGrafic - 80)
00351         {
00352         char str [20]= "";
00353         sprintf (str, "-%d", i2);
00354 
00355         txTextOut (Xcentre-10, TopOfTheGrafic  + y2, str);
00356 
00357         i2++;
00358         y2 += 2*Step;
00359         }
00360 
00361     txSetTextAlign (TA_CENTER);
00362 
00363     int i3 = -1;
00364     int x = Xcentre-90;
00365     while (x >= LeftOfTheGrafic - 60)
00366         {
00367         char str [20]= "";
00368         sprintf (str, "%d", i3);
00369 
00370         txTextOut (LeftOfTheGrafic + x+5, Ycentre+5, str);
00371 
00372         i3--;
00373         x -= 2*Step;
00374         }
00375 
00376     int i4 = 1;
00377     int x2 = Xcentre-5;
00378     while (x2 <= RightOfTheGrafic - 60)
00379         {
00380         char str [20]= "";
00381         sprintf (str, "%d", i4);
00382 
00383         txTextOut (LeftOfTheGrafic + x2+5, Ycentre+5, str);
00384 
00385         i4++;
00386         x2 += 2*Step;
00387         }
00388 
00389     txSelectFont ("Comic Sans MS", 25);
00390     txSetTextAlign (TA_TOP + TA_RIGHT);
00391 
00392     txTextOut (Xcentre-7, Ycentre+4, "0");
00393     }
00394 
00395 //----------------------------------------------------------------------------------------------------------------------
00396 
00397 void InformationDesk()
00398     {
00399     ShadowRect (30, 530, 885, 680, ButtonColor, CountourColor);
00400 
00401     txSelectFont ("Comic Sans MS", 25);
00402     txSetTextAlign (TA_LEFT);
00403 
00404     ShadowText (50, 540, 0, 0, TX_WHITE, "Выбор графика, включение и выключение меню - значок книги в правом верхнем углу или F2");
00405     ShadowText (50, 570, 0, 0, TX_WHITE, "Увеличение k -- K + UP,  уменьшение -- K + DOWN,      k = 0 -- K + SPACE");
00406     ShadowText (50, 590, 0, 0, TX_WHITE, "Увеличение b -- B + UP,  уменьшение -- B + DOWN,      b = 0 -- B + SPACE           Выход -- ESC");
00407     ShadowText (50, 610, 0, 0, TX_WHITE, "Увеличение c -- C + UP,  уменьшение -- C + DOWN,      c  = 0 -- C + SPACE");
00408     ShadowText (50, 640, 0, 0, TX_WHITE, "Copyright (C) Филипп Куликов, 2018, 7 класс                 k = b = c = 0 -- SPACE");
00409     }
00410 
00411 //----------------------------------------------------------------------------------------------------------------------
00412 
00413 void ShadowRect (int x1, int y1, int x2, int y2, COLORREF fillColor, COLORREF countourColor)
00414     {
00415     txSetColor       (ShadowColor);
00416     txSetFillColor   (ShadowColor);
00417     Win32::RoundRect (txDC(), x1+3, y1+3, x2+3, y2+3, 15, 15);
00418 
00419     txSetColor       (countourColor);
00420     txSetFillColor   (fillColor);
00421     Win32::RoundRect (txDC(), x1, y1, x2, y2, 15, 15);
00422     }
00423 
00424 //----------------------------------------------------------------------------------------------------------------------
00425 
00426 void ShadowText (int x1, int y1, int x2, int y2, COLORREF color, const char text[])
00427     {
00428     txSetColor (ShadowColor);
00429     if (x2 && y2) txDrawText (x1+2, y1+2, x2+2, y2+2, text);
00430     else          txTextOut  (x1+2, y1+2,             text);
00431 
00432     txSetColor (color);
00433     if (x2 && y2) txDrawText (x1, y1, x2, y2, text);
00434     else          txTextOut  (x1, y1,         text);
00435     }