Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.80 KB | None | 0 0
  1. #define _win32_winnt 0x500
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <iostream>
  4. #include <conio.h>
  5. #include <windows.h>
  6. using namespace std;
  7. int menu();
  8. void drawGraph();
  9. void drawImage();
  10.  
  11. HANDLE hStdOut;
  12. HWND hwnd;
  13. HDC hdc;
  14. POINT pointTop;
  15. POINT pointBottom;
  16. POINT pointLeft;
  17. POINT pointRight;
  18.  
  19.  
  20. int main()
  21. {
  22.     SetConsoleTitleA("Lab Graphics");
  23.     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  24.     SetConsoleTextAttribute(hStdOut, BACKGROUND_BLUE | FOREGROUND_RED);
  25.     system("CLS");
  26.  
  27.     int answer = menu();
  28.     switch (answer)
  29.     {
  30.     case 0: drawImage(); break;
  31.     case 1: cout << "2" << endl; break;
  32.     case 2: cout << "3" << endl; break;
  33.     case 3: cout << "4" << endl; return 0;
  34.     }
  35.     system("pause");
  36.     return 0;
  37. }
  38.  
  39. int menu() {
  40.     int key = 0;
  41.     int code;
  42.  
  43.     do {
  44.         system("cls");
  45.         key = (key + 4) % 4;
  46.         if (key == 0)
  47.         {
  48.             HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  49.             SetConsoleTextAttribute(hStdOut, 228);
  50.             cout << "-> Draw a picture" << endl;
  51.         }
  52.         else
  53.         {
  54.             HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  55.             SetConsoleTextAttribute(hStdOut, 28);
  56.             cout << "   Draw a picture" << endl;
  57.         }
  58.  
  59.         if (key == 1)
  60.         {
  61.             HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  62.             SetConsoleTextAttribute(hStdOut, 228);
  63.             cout << "-> Draw a graph" << endl;
  64.         }
  65.         else
  66.         {
  67.             HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  68.             SetConsoleTextAttribute(hStdOut, 28);
  69.             cout << "   Draw a graph" << endl;
  70.         }
  71.  
  72.         if (key == 2)
  73.         {
  74.             HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  75.             SetConsoleTextAttribute(hStdOut, 228);
  76.             cout << "-> Draw a picture step-by-step" << endl;
  77.         }
  78.         else
  79.         {
  80.             HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  81.             SetConsoleTextAttribute(hStdOut, 28);
  82.             cout << "   Draw a picture step-by-step" << endl;
  83.         }
  84.  
  85.         if (key == 3)
  86.         {
  87.             HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  88.             SetConsoleTextAttribute(hStdOut, 228);
  89.             cout << "-> Exit" << endl;
  90.         }
  91.         else
  92.         {
  93.             HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  94.             SetConsoleTextAttribute(hStdOut, 28);
  95.             cout << "   Exit" << endl;
  96.         }
  97.  
  98.         code = _getch();
  99.         if (code == 80) key++;
  100.         if (code == 72) key--;
  101.  
  102.     } while (code != 13);
  103.  
  104.     system("cls");
  105.     return key;
  106. }
  107.  
  108. void drawImage() {
  109.     pointTop.x = 500;
  110.     pointTop.y = 35;
  111.     pointRight.x = 580;
  112.     pointRight.y = 315;
  113.     pointBottom.x = 500;
  114.     pointBottom.y = 570;
  115.     pointLeft.x = 420;
  116.     pointLeft.y = 315;
  117.  
  118.     POINT *rhomb = new POINT[4];
  119.     rhomb[0] = pointTop;
  120.     rhomb[1] = pointRight;
  121.     rhomb[2] = pointBottom;
  122.     rhomb[3] = pointLeft;
  123.  
  124.     HWND hwnd = GetConsoleWindow();
  125.     HDC hdc = GetDC(hwnd); //получаем DC(контекст устройства) для рисования
  126.     HPEN hpen1; //объявляем объект перо
  127.     HGDIOBJ hpenOld, hbrushOld;
  128.     HBRUSH hbrush; //объявляем кисть
  129.  
  130.     hpen1 = CreatePen(PS_SOLID, 2, RGB(0, 0, 255)); //логическое перо с заданным стилем, шириной и цветом
  131.     hpenOld = (HPEN)SelectObject(hdc, hpen1);
  132.  
  133.     MoveToEx(hdc, 500, 15, NULL);//устанавливает текущей позицией указанную точку
  134.     LineTo(hdc, 590, 315);
  135.     LineTo(hdc, 500, 585);
  136.     LineTo(hdc, 410, 315);
  137.     LineTo(hdc, 500, 15);
  138.  
  139.     hpen1 = CreatePen(PS_SOLID, 7, RGB(255, 255, 255)); //логическое перо с заданным стилем, шириной и цветом
  140.     hpenOld = (HPEN)SelectObject(hdc, hpen1);
  141.  
  142.     hbrush = CreateSolidBrush(RGB(0, 0, 255));
  143.     hbrushOld = (HBRUSH)SelectObject(hdc, hbrush);
  144.     Polygon(hdc, rhomb, 4);
  145.  
  146.     MoveToEx(hdc, 495, 240, NULL);
  147.     hpen1 = CreatePen(PS_SOLID, 8, RGB(255, 255, 255)); //логическое перо с заданным стилем, шириной и цветом
  148.     hpenOld = (HPEN)SelectObject(hdc, hpen1);
  149.     LineTo(hdc, 495, 345);//вертикальная вниз
  150.  
  151.     LineTo(hdc, 482, 355);//петля(влево вниз)
  152.     LineTo(hdc, 470, 345);//петля(влево вверх)
  153.     LineTo(hdc, 482, 335);//петля(вправо вверх)
  154.     LineTo(hdc, 508, 355);//петля(вправо вниз)
  155.  
  156.     LineTo(hdc, 515, 355);
  157.     LineTo(hdc, 523, 352);//ду спра
  158.     LineTo(hdc, 541, 335);//га ва
  159.  
  160.     LineTo(hdc, 547, 325);
  161.     LineTo(hdc, 549, 315);
  162.     LineTo(hdc, 550, 290);//pryamaya na duge
  163.     LineTo(hdc, 550, 276);
  164.     LineTo(hdc, 536, 255);//вертикальная вверх
  165.  
  166.     LineTo(hdc, 525, 245);//дуга
  167.     LineTo(hdc, 510, 239);//вверху
  168.  
  169.     LineTo(hdc, 480, 242);//горизонтальная влево
  170.  
  171.     LineTo(hdc, 471, 253);//конечная дуга
  172.     LineTo(hdc, 464, 267);//справа налево
  173.     LineTo(hdc, 464, 275);
  174.  
  175.     DeleteObject(hpen1);
  176.     getchar();
  177.  
  178. }
  179.  
  180. void strelka(double x, double y, double size, bool scale)
  181. {
  182.     if (scale == 0) {
  183.         MoveToEx(hdc, x, y + size, NULL);
  184.         LineTo(hdc, x, y - size);
  185.         LineTo(hdc, x + size, y);
  186.         LineTo(hdc, x, y + size);
  187.     }
  188.     else {
  189.         MoveToEx(hdc, x - size, y, NULL);
  190.         LineTo(hdc, x, y - size);
  191.         LineTo(hdc, x + size, y);
  192.         LineTo(hdc, x - size, y);
  193.     }
  194. }
  195.  
  196.  
  197. WORD colorConvertTo(int ForgC, int BackC)
  198. {
  199.     return ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
  200. }
  201. WORD workWindowAttributes = colorConvertTo(15, 9); //158
  202.  
  203. void drawGraph() {
  204.     double x, y;
  205.     HDC dc = GetDC(hwnd);
  206.     RECT clientRect; // координаты прямоугольника
  207.     GetClientRect(hwnd, &clientRect);
  208.  
  209.     HPEN Pen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0));
  210.     HPEN PenCross = CreatePen(PS_SOLID, 3, RGB(255, 255, 255));
  211.     SelectObject(hdc, Pen);
  212.     int xScale = 50, yScale = 50;
  213.     double sizeW = 900;
  214.     double sizeH = 600;
  215.     char *number = new char[15];
  216.     double sizeStrelka = 6;
  217.     SelectObject(hdc, PenCross);
  218.     MoveToEx(hdc, 30, sizeH / 2, NULL);
  219.     LineTo(hdc, sizeW - 30, sizeH / 2);
  220.     strelka(sizeW - 30, sizeH / 2, 5, 0);
  221.     MoveToEx(hdc, sizeW / 2, 40, NULL);
  222.     LineTo(hdc, sizeW / 2, sizeH);
  223.     strelka(sizeW / 2, 40, 5, 1);
  224.     int xO = sizeW / 2, yO = sizeH / 2;
  225.     //metki
  226.     HDC THDC = GetDC(GetConsoleWindow());
  227.     SetBkMode(THDC, TRANSPARENT);
  228.     SetTextColor(THDC, RGB(255, 255, 255));
  229.     SetConsoleTextAttribute(hStdOut, workWindowAttributes);
  230.     strcpy(number, "X");
  231.     TextOut(THDC, sizeW - 10, sizeH / 2, number, strlen(number));
  232.     strcpy(number, "Y");
  233.     TextOut(THDC, sizeW / 2 + 10, 20, number, strlen(number));
  234.     strcpy(number, "y=e^x");
  235.     TextOut(THDC, sizeW / 2 + 60, 20, number, strlen(number));
  236.     for (int i = 50; i <= sizeH; i += yScale)
  237.     {
  238.         MoveToEx(hdc, sizeW / 2 - 10, i, NULL);
  239.         LineTo(hdc, sizeW / 2 + 10, i);
  240.         _itoa(-(i / 50) + 6, number, 10);
  241.         TextOut(THDC, sizeW / 2 + 15, i, number, strlen(number));
  242.     }
  243.     for (int i = 50; i <= sizeW - 30; i += xScale)
  244.     {
  245.         MoveToEx(hdc, i, sizeH / 2 - 10, NULL);
  246.         LineTo(hdc, i, sizeH / 2 + 10);
  247.         _itoa((i / 50) - 9, number, 10);
  248.         TextOut(THDC, i + 5, sizeH / 2 - 25, number, strlen(number));
  249.     }
  250.  
  251.     SelectObject(hdc, Pen);
  252.     float xBeg = -9, xEnd = 1.6;
  253.     while ((exp(xBeg))*yScale + yO <= 40)
  254.     {
  255.         xBeg += 0.01;
  256.     }
  257.     MoveToEx(hdc, xO + xScale * xBeg, yO + yScale * -exp(xBeg), NULL);
  258.  
  259.     for (x = xBeg; x <= xEnd; x += 0.01)
  260.     {
  261.         LineTo(hdc, xO + xScale * x, yO + yScale * -exp(x));
  262.  
  263.     }
  264.     DeleteObject(Pen);
  265.     DeleteObject(PenCross);
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement