Advertisement
Kostiggig

Untitled

Apr 4th, 2023
984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 29.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <SDL.h>
  3. #include <iostream>
  4. #include <ctime>
  5. #include <cstdio>
  6.  
  7. const int SCREEN_WIDTH = 700;
  8. const int SCREEN_HEIGHT = 800;
  9. const int IMAGE_WIDTH = 418;
  10. const int IMAGE_HEIGHT = 418;
  11.  
  12. #define DELAY_BEFORE_EXIT 3000
  13.  
  14. using namespace std;
  15.  
  16. void lab_19_1() {
  17.     SDL_Window* window = NULL;
  18.     SDL_Surface* screenSurface = NULL;
  19.  
  20.     if (SDL_Init(SDL_INIT_VIDEO) < 0)
  21.     {
  22.         printf("SDL не смог запуститься! SDL_Error: %s\n", SDL_GetError());
  23.     }
  24.     else
  25.     {
  26.         window = SDL_CreateWindow(u8"Зиновьев Константин, 8 Вариант", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  27.         if (window == NULL)
  28.         {
  29.             printf("Окно не может быть создано! SDL_Error: %s\n", SDL_GetError());
  30.         }
  31.         else
  32.         {
  33.             screenSurface = SDL_GetWindowSurface(window);
  34.             SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 255, 165, 0));
  35.             SDL_Surface* image = SDL_LoadBMP("image.bmp");
  36.             if (image != NULL) {
  37.  
  38.  
  39.  
  40.                 SDL_Rect rect;
  41.                 rect.x = SCREEN_WIDTH - IMAGE_WIDTH;
  42.                 rect.y = 0;
  43.                 SDL_SetColorKey(image, SDL_TRUE, SDL_MapRGB(image->format, 255, 255, 255));
  44.                 SDL_BlitSurface(image, NULL, screenSurface, &rect);
  45.  
  46.                 SDL_UpdateWindowSurface(window);
  47.  
  48.  
  49.             }
  50.             else {
  51.                 printf("Image error! SDL_Error: %s\n", SDL_GetError());
  52.             }
  53.  
  54.             SDL_Delay(DELAY_BEFORE_EXIT);
  55.         }
  56.     }
  57.  
  58.  
  59.     SDL_DestroyWindow(window);
  60.     SDL_Quit();
  61. }
  62.  
  63. #define LETTER_HEIGHT 50
  64.  
  65.  
  66. int draw_k_and_return_lastx(SDL_Renderer* renderer) {
  67.  
  68.     SDL_Point kletterPoint1;
  69.     SDL_Point kletterPoint2;
  70.  
  71.     SDL_Point kletterPoint3;
  72.     SDL_Point kletterPoint4;
  73.  
  74.     SDL_Point kletterPoint5;
  75.     SDL_Point kletterPoint6;
  76.  
  77.  
  78.     kletterPoint1.x = LETTER_HEIGHT;
  79.     kletterPoint1.y = (SCREEN_HEIGHT / 2) - LETTER_HEIGHT;
  80.  
  81.     kletterPoint2.x = kletterPoint1.x;
  82.     kletterPoint2.y = kletterPoint1.y + LETTER_HEIGHT;
  83.  
  84.     kletterPoint3.y = kletterPoint1.y + (LETTER_HEIGHT / 2);
  85.     kletterPoint3.x = kletterPoint1.x;
  86.  
  87.     kletterPoint4.y = kletterPoint3.y - LETTER_HEIGHT / 2;
  88.     kletterPoint4.x = kletterPoint3.x + LETTER_HEIGHT / 2;
  89.  
  90.     kletterPoint5.y = kletterPoint3.y + LETTER_HEIGHT / 2;
  91.     kletterPoint5.x = kletterPoint3.x + LETTER_HEIGHT / 2;
  92.  
  93.  
  94.     SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  95.     SDL_RenderDrawLine(renderer, kletterPoint1.x, kletterPoint1.y, kletterPoint2.x, kletterPoint2.y);
  96.     SDL_RenderDrawLine(renderer, kletterPoint3.x, kletterPoint3.y, kletterPoint4.x, kletterPoint4.y);
  97.     SDL_RenderDrawLine(renderer, kletterPoint3.x, kletterPoint3.y, kletterPoint5.x, kletterPoint5.y);
  98.  
  99.     return kletterPoint5.x;
  100. }
  101.  
  102. int draw_o_and_return_last_x(SDL_Renderer* renderer, int last_x) {
  103.     int radius = LETTER_HEIGHT / 1.5;
  104.     int countOfPoints = 10000;
  105.     SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  106.     double step = 2 * M_PI / countOfPoints;
  107.     int max_x = INT32_MIN;
  108.     for (float angle = 0; angle < 2 * M_PI; angle += step) {
  109.  
  110.         int x = (int)(last_x * 1.7 + radius * cos(angle));
  111.         max_x = max(max_x, x);
  112.         int y = (int)(SCREEN_HEIGHT / 2 - LETTER_HEIGHT / 2 + radius * sin(angle));
  113.  
  114.         SDL_RenderDrawPoint(renderer, x, y);
  115.     }
  116.  
  117.     return max_x;
  118. }
  119.  
  120. int draw_c_and_return_last_x(SDL_Renderer* renderer, int last_x) {
  121.     int radius = LETTER_HEIGHT / 1.5;
  122.     int countOfPoints = 10000;
  123.     SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  124.     double step = 2 * M_PI / countOfPoints;
  125.     int max_x = INT32_MIN;
  126.     int center_x = last_x * 1.3;
  127.     for (float angle = 0; angle < 2 * M_PI; angle += step) {
  128.  
  129.         int x = (int)(center_x + radius * cos(angle));
  130.  
  131.         int y = (int)(SCREEN_HEIGHT / 2 - LETTER_HEIGHT / 2 + radius * sin(angle));
  132.  
  133.         if (x < center_x + 10) {
  134.             max_x = max(max_x, x);
  135.             SDL_RenderDrawPoint(renderer, x, y);
  136.         }
  137.  
  138.     }
  139.  
  140.     cout << "max x is";
  141.     cout << max_x;
  142.     return max_x;
  143. }
  144.  
  145. int draw_t_and_return_last_x(SDL_Renderer* renderer, int last_x) {
  146.     SDL_Point tletterPoint1;
  147.     SDL_Point tletterPoint2;
  148.  
  149.     SDL_Point tletterPoint3;
  150.     SDL_Point tletterPoint4;
  151.  
  152.     tletterPoint1.x = LETTER_HEIGHT / 2 + last_x;
  153.     tletterPoint1.y = (SCREEN_HEIGHT / 2) + LETTER_HEIGHT / 2.2;
  154.  
  155.     tletterPoint2.x = LETTER_HEIGHT / 2 + last_x;
  156.     tletterPoint2.y = (SCREEN_HEIGHT / 2) - LETTER_HEIGHT * 2;
  157.  
  158.     tletterPoint3.x = tletterPoint1.x - LETTER_HEIGHT;
  159.     tletterPoint3.y = tletterPoint2.y;
  160.  
  161.     tletterPoint4.x = tletterPoint1.x + LETTER_HEIGHT;
  162.     tletterPoint4.y = tletterPoint2.y;
  163.  
  164.     SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  165.     SDL_RenderDrawLine(renderer, tletterPoint1.x, tletterPoint1.y, tletterPoint2.x, tletterPoint2.y);
  166.     SDL_RenderDrawLine(renderer, tletterPoint3.x, tletterPoint3.y, tletterPoint4.x, tletterPoint4.y);
  167.  
  168.     return tletterPoint4.x;
  169. }
  170.  
  171. int draw_y_and_return_last_x(SDL_Renderer* renderer, int last_x) {
  172.     SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  173.  
  174.     int radius = LETTER_HEIGHT / 1.5;
  175.     int countOfPoints = 10000;
  176.     SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  177.     double step = 2 * M_PI / countOfPoints;
  178.     int max_x = INT32_MIN;
  179.     int max_y = INT32_MIN;
  180.     int center_x = last_x * 1.2;
  181.     int center_y = SCREEN_HEIGHT / 2 - LETTER_HEIGHT * 2;
  182.     for (float angle = 0; angle < 2 * M_PI; angle += step) {
  183.  
  184.         int x = (int)(center_x + radius * cos(angle));
  185.         int y = (int)(center_y + radius * sin(angle));
  186.         max_y = max(max_y, y);
  187.  
  188.         if (x < center_x + 10) {
  189.             max_x = max(max_x, x);
  190.             SDL_RenderDrawPoint(renderer, x, y);
  191.         }
  192.  
  193.     }
  194.  
  195.     SDL_Point yletterPoint1;
  196.     SDL_Point yletterPoint2;
  197.  
  198.     SDL_Point yletterPoint3;
  199.     SDL_Point yletterPoint4;
  200.  
  201.     yletterPoint1.x = max_x;
  202.     yletterPoint1.y = center_y - radius;
  203.  
  204.     yletterPoint2.x = yletterPoint1.x;
  205.     yletterPoint2.y = yletterPoint1.y + LETTER_HEIGHT * 3;
  206.  
  207.     yletterPoint3.x = center_x + 10;
  208.     yletterPoint3.y = center_y + radius;
  209.  
  210.     yletterPoint4.x = yletterPoint3.x - LETTER_HEIGHT * 1.7;
  211.     yletterPoint4.y = yletterPoint3.y + LETTER_HEIGHT * 1.7;
  212.  
  213.     SDL_RenderDrawLine(renderer, yletterPoint1.x, yletterPoint1.y, yletterPoint2.x, yletterPoint2.y);
  214.     SDL_RenderDrawLine(renderer, yletterPoint3.x, yletterPoint3.y, yletterPoint4.x, yletterPoint4.y);
  215.  
  216.  
  217.     cout << "max x is";
  218.     cout << max_x;
  219.     return max_x;
  220. }
  221.  
  222. void lab_20_1() {
  223.     SDL_Window* window;
  224.     SDL_Renderer* renderer;
  225.  
  226.     SDL_Init(SDL_INIT_VIDEO);
  227.  
  228.     window = SDL_CreateWindow("Zinoview, 2 - 1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  229.     renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  230.  
  231.  
  232.     SDL_SetRenderDrawColor(renderer, 165, 165, 0, 255);
  233.     SDL_RenderClear(renderer);
  234.  
  235.     int last_x = draw_k_and_return_lastx(renderer);
  236.     last_x = draw_o_and_return_last_x(renderer, last_x);
  237.     last_x = draw_c_and_return_last_x(renderer, last_x);
  238.     last_x = draw_t_and_return_last_x(renderer, last_x);
  239.     last_x = draw_y_and_return_last_x(renderer, last_x);
  240.  
  241.     SDL_RenderPresent(renderer);
  242.  
  243.  
  244.  
  245.  
  246.     SDL_Delay(DELAY_BEFORE_EXIT);
  247.  
  248.     SDL_DestroyRenderer(renderer);
  249.     SDL_DestroyWindow(window);
  250.     SDL_Quit();
  251.  
  252. }
  253.  
  254.  
  255. void lab_20_2() {
  256.     int count_of_lines = -1;
  257.     int space_between_lines = -1;
  258.  
  259.     do {
  260.         cout << endl << "Count of lines(at least 1): ";
  261.         cin >> count_of_lines;
  262.     } while (count_of_lines < 1);
  263.  
  264.     do {
  265.         cout << endl << "Space between lines in pxs(at least 1): ";
  266.         cin >> space_between_lines;
  267.     } while (space_between_lines < 1);
  268.  
  269.     int lines_left = count_of_lines;
  270.  
  271.     SDL_Window* window;
  272.     SDL_Renderer* renderer;
  273.  
  274.     SDL_Init(SDL_INIT_VIDEO);
  275.  
  276.     window = SDL_CreateWindow("Zinoview, 2-2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  277.     renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  278.  
  279.     SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  280.     SDL_RenderClear(renderer);
  281.     for (int line = 0; line < count_of_lines; line++) {
  282.         int start_x = line * space_between_lines;
  283.         if (start_x > SCREEN_WIDTH) break;
  284.         int start_y = SCREEN_HEIGHT;
  285.  
  286.         int end_x = 0;
  287.         int end_y = SCREEN_HEIGHT - (line * space_between_lines);
  288.  
  289.         SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  290.         SDL_RenderDrawLine(renderer, start_x, start_y, end_x, end_y);
  291.         lines_left--;
  292.     }
  293.  
  294.     int temp_lines_left = lines_left;
  295.     for (int line = 0; line < temp_lines_left; line++) {
  296.         int start_x = SCREEN_WIDTH - line * space_between_lines;
  297.         if (start_x < 0) break;
  298.         int start_y = 0;
  299.  
  300.         int end_x = SCREEN_WIDTH;
  301.         int end_y = line * space_between_lines;
  302.  
  303.         SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  304.         SDL_RenderDrawLine(renderer, start_x, start_y, end_x, end_y);
  305.         lines_left--;
  306.     }
  307.  
  308.     SDL_RenderPresent(renderer);
  309.  
  310.     SDL_Delay(DELAY_BEFORE_EXIT);
  311.  
  312.     SDL_DestroyRenderer(renderer);
  313.     SDL_DestroyWindow(window);
  314.     SDL_Quit();
  315. }
  316.  
  317. double calculate_y(double x, double a, double b) {
  318.     return a * x * x;
  319. }
  320. #define SPACE_BETWEEN_POINTS_IN_PX 20 // единичный отрезок в пикселях
  321. #define X_THRESHOLD_STEP_IN_PX 100
  322. #define Y_THRESHOLD_STEP_IN_PX 100
  323.  
  324. void lab_20_3() {
  325.     double a, b, x_min, x_max;
  326.     cout << "Enter x_min: ";
  327.     cin >> x_min;
  328.     cout << "Enter x_max: ";
  329.     cin >> x_max;
  330.  
  331.     cout << "Enter a: ";
  332.     cin >> a;
  333.     cout << "Enter b: ";
  334.     cin >> b;
  335.  
  336.     int max_dividers_by_y = (SCREEN_HEIGHT / 2) / SPACE_BETWEEN_POINTS_IN_PX;
  337.     int max_dividers_by_x = (SCREEN_WIDTH / 2) / SPACE_BETWEEN_POINTS_IN_PX;
  338.  
  339.     b = (int) b % max_dividers_by_y;
  340.     x_min = (int) x_min % max_dividers_by_x;
  341.     x_max = (int) x_max % max_dividers_by_x;
  342.  
  343.     cout << "dividers by y " << max_dividers_by_y << " dividers by x " << max_dividers_by_x << endl;
  344.     cout << "Calculated b is " << b;
  345.  
  346.     SDL_Window* window;
  347.     SDL_Renderer* renderer;
  348.  
  349.     SDL_Init(SDL_INIT_VIDEO);
  350.  
  351.     window = SDL_CreateWindow("Graph", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  352.     renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  353.  
  354.  
  355.     SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  356.     SDL_RenderClear(renderer);
  357.  
  358.     SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  359.     SDL_RenderDrawLine(renderer, SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT); // axis y
  360.  
  361.     int xs = SCREEN_WIDTH / 2 - 3;
  362.     int xk = SCREEN_WIDTH / 2 + 3;
  363.     for (double y = 0; y < SCREEN_HEIGHT; y += SPACE_BETWEEN_POINTS_IN_PX) {
  364.         SDL_RenderDrawLine(renderer, xs, y, xk, y); // y dividers
  365.     }
  366.  
  367.     SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  368.     SDL_RenderDrawLine(renderer, 0, SCREEN_HEIGHT / 2, SCREEN_WIDTH, SCREEN_HEIGHT / 2); // axis x
  369.  
  370.     int ys = SCREEN_HEIGHT / 2 - 3;
  371.     int yk = SCREEN_HEIGHT / 2 + 3;
  372.     for (double x = 0; x < SCREEN_WIDTH; x += SPACE_BETWEEN_POINTS_IN_PX) {
  373.         SDL_RenderDrawLine(renderer, x, ys, x, yk); // x dividers
  374.     }
  375.  
  376.     double threshold_y_min = SCREEN_HEIGHT / 2 - 10 * SPACE_BETWEEN_POINTS_IN_PX;
  377.     double threshold_y_max = SCREEN_HEIGHT / 2 + 10 * SPACE_BETWEEN_POINTS_IN_PX;
  378.  
  379.     SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  380.     for (double x = 0; x < SCREEN_WIDTH; x += X_THRESHOLD_STEP_IN_PX) {
  381.         double xs = x;
  382.         double xk = x + X_THRESHOLD_STEP_IN_PX / 2;
  383.  
  384.         SDL_RenderDrawLine(renderer, xs, threshold_y_min, xk, threshold_y_min); // y min treshold
  385.         SDL_RenderDrawLine(renderer, xs, threshold_y_max, xk, threshold_y_max); // y max treshold
  386.     }
  387.  
  388.     SDL_RenderPresent(renderer);
  389.  
  390.     double threshold_x_min = SCREEN_WIDTH / 2 + (x_min * SPACE_BETWEEN_POINTS_IN_PX);
  391.     double threshold_x_max = SCREEN_WIDTH / 2 + (x_max * SPACE_BETWEEN_POINTS_IN_PX);
  392.  
  393.     SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  394.     for (double y = 0; y < SCREEN_HEIGHT; y += Y_THRESHOLD_STEP_IN_PX) {
  395.         double ys = y;
  396.         double yk = y + Y_THRESHOLD_STEP_IN_PX / 2;
  397.  
  398.         SDL_RenderDrawLine(renderer, threshold_x_min, ys, threshold_x_min, yk); // x min treshold
  399.         SDL_RenderDrawLine(renderer, threshold_x_max, ys, threshold_x_max, yk); // x max treshold
  400.     }
  401.  
  402.     SDL_RenderPresent(renderer);
  403.  
  404.     double additional_x_for_broading = 0.0;
  405.     for (double i = 0; i < SCREEN_WIDTH / 2; i+=0.3) {
  406.         double x = i / SPACE_BETWEEN_POINTS_IN_PX;
  407.         double y = SCREEN_HEIGHT / 2 - calculate_y(x, a, b) - b * SPACE_BETWEEN_POINTS_IN_PX;
  408.         cout << y << endl;
  409.  
  410.         additional_x_for_broading += 0.1 / a;
  411.         double screen_x_left = SCREEN_WIDTH / 2 - x - additional_x_for_broading;
  412.         double screen_x_right = SCREEN_WIDTH / 2 + x + additional_x_for_broading;
  413.  
  414.         if (screen_x_right < threshold_x_min || screen_x_right > threshold_x_max || screen_x_left < threshold_x_min || screen_x_left > threshold_x_max || y < threshold_y_min || y > threshold_y_max) continue;
  415.  
  416.        SDL_RenderDrawPoint(renderer, screen_x_left, y);
  417.        SDL_RenderDrawPoint(renderer, screen_x_right, y);
  418.     }
  419.  
  420.     cout << endl << endl << "THE SECOND PART" << endl << endl;
  421.  
  422.    
  423.  
  424.     SDL_RenderPresent(renderer);
  425.  
  426.     SDL_Delay(DELAY_BEFORE_EXIT * 100);
  427.  
  428.     SDL_DestroyRenderer(renderer);
  429.     SDL_DestroyWindow(window);
  430.     SDL_Quit();
  431. }
  432.  
  433. void draw_fallen_snow(SDL_Renderer* renderer, const int count) {
  434.     SDL_Point* points = new SDL_Point[count];
  435.  
  436.     int range_x = SCREEN_WIDTH - 0 + 1;
  437.     int range_y = SCREEN_HEIGHT - 0 + 1;
  438.  
  439.     for (int i = 0; i < count; i++) {
  440.         SDL_Point point;
  441.         int x = 0 + (rand() % range_x);
  442.         int y = 0 + (rand() % range_y);
  443.  
  444.         points[i].x = x;
  445.         points[i].y = y;
  446.     }
  447.  
  448.     int range_extra_offset = 30 - 0 + 1;
  449.     while (true) {
  450.         for (int i = 0; i < count; i++) {
  451.             SDL_Point point;
  452.             int extra_offset = 0 + (rand() % range_extra_offset);
  453.             int offset_by_y = extra_offset + 10;
  454.             int new_y = points[i].y + offset_by_y;
  455.             if (new_y > SCREEN_HEIGHT) new_y = new_y - SCREEN_HEIGHT;
  456.             points[i].y = new_y;
  457.         }
  458.  
  459.         SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
  460.         SDL_RenderClear(renderer);
  461.  
  462.         SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  463.         SDL_RenderDrawPoints(renderer, points, count);
  464.         SDL_RenderPresent(renderer);
  465.         SDL_Delay(150);
  466.     }
  467.    
  468. }
  469.  
  470. void lab_21_1() {
  471.     srand(time(0));
  472.     int countOfPointers;
  473.  
  474.     do {
  475.         cout << "Enter points. Count of points should be positive(0 - to exit)" << endl;
  476.         cout << "Enter cout of points: ";
  477.         cin >> countOfPointers;
  478.  
  479.         if (countOfPointers == 0) break;
  480.  
  481.         if (countOfPointers > 0) {
  482.             SDL_Window* window;
  483.             SDL_Renderer* renderer;
  484.  
  485.             SDL_Init(SDL_INIT_VIDEO);
  486.  
  487.             window = SDL_CreateWindow("Zinoview - 3", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  488.             renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  489.  
  490.             SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
  491.             SDL_RenderClear(renderer);
  492.  
  493.             draw_fallen_snow(renderer, countOfPointers);
  494.  
  495.             SDL_DestroyRenderer(renderer);
  496.             SDL_DestroyWindow(window);
  497.             SDL_Quit();
  498.         }
  499.     } while (countOfPointers < 0);
  500.    
  501. }
  502.  
  503. #define CIRCLE_RADIUS 50
  504. #define START_X CIRCLE_RADIUS + 50
  505. #define START_Y CIRCLE_RADIUS + 50
  506.  
  507. #define X_DEVIATION 3
  508. #define Y_DEVIATION 3
  509.  
  510. void get_random_color(int* r, int* g, int* b) {
  511.     (*r) = 0 + (rand() % 255);
  512.     (*g) = 0 + (rand() % 255);
  513.     (*b) = 0 + (rand() % 255);
  514. }
  515.  
  516. int r = 0;
  517. int g = 0;
  518. int b = 0;
  519.  
  520. void draw_circle(SDL_Renderer* renderer, double x0, double y0, bool is_random_color) {
  521.    
  522.     if(is_random_color) get_random_color(&r, &g, &b);
  523.  
  524.     double radius = CIRCLE_RADIUS;
  525.     int countOfPoints = 300;
  526.     SDL_SetRenderDrawColor(renderer, r, g, b, 255);
  527.     double step = 2 * M_PI / countOfPoints;
  528.     int max_x = INT32_MIN;
  529.     for (float angle = 0; angle < 2 * M_PI; angle += step) {
  530.  
  531.         double x = x0 + radius * cos(angle);
  532.         double y = y0 + radius * sin(angle);
  533.  
  534.         SDL_RenderDrawPoint(renderer, x, y);
  535.     }
  536. }
  537.  
  538. bool restart_if_is_out_of_bound(SDL_Renderer* renderer, double *curr_x, double *curr_y) {
  539.     bool is_out_of_bound = (*curr_x) + CIRCLE_RADIUS > SCREEN_WIDTH || (*curr_x - CIRCLE_RADIUS) < 0 ||(* curr_y) < 0 || (*curr_y) + CIRCLE_RADIUS > SCREEN_HEIGHT || (*curr_y) - CIRCLE_RADIUS < 0;
  540.     if (is_out_of_bound) {
  541.         (*curr_x) = START_X;
  542.         (*curr_y) = START_Y;
  543.  
  544.         SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  545.         SDL_RenderClear(renderer);
  546.         draw_circle(renderer, (*curr_x), (*curr_y), true);
  547.         SDL_RenderPresent(renderer);
  548.     }
  549.  
  550.     return is_out_of_bound;
  551. }
  552.  
  553. void lab_22_1() {
  554.     srand(time(0));
  555.  
  556.     SDL_Window* window;
  557.     SDL_Renderer* renderer;
  558.  
  559.     SDL_Init(SDL_INIT_VIDEO);
  560.  
  561.     window = SDL_CreateWindow("Zinoview, 2 - 1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  562.     renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  563.  
  564.  
  565.     SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  566.     SDL_RenderClear(renderer);
  567.  
  568.     draw_circle(renderer, START_X, START_Y, false);
  569.  
  570.     SDL_RenderPresent(renderer);
  571.  
  572.     double curr_x = START_X;
  573.     double curr_y = START_Y;
  574.  
  575.     SDL_Event event;
  576.     bool quit = false;
  577.     while(!quit)
  578.     {
  579.         SDL_PollEvent(&event);
  580.         switch (event.type) {
  581.             case SDL_QUIT: quit = true; break;
  582.             case SDL_MOUSEBUTTONUP: {
  583.                 bool ignore_all_conditions = false;
  584.                
  585.                 int mouse_x, mouse_Y;
  586.                 SDL_GetMouseState(&mouse_x, &mouse_Y);
  587.                 if (event.button.button == SDL_BUTTON_LEFT) {
  588.                     cout << "x is " << curr_x << " y is " << curr_y;
  589.                     bool is_plus_by_x = false;
  590.                     bool is_plus_by_y = false;
  591.  
  592.                     if (curr_x < mouse_x) is_plus_by_x = true;
  593.                     if (curr_y < mouse_Y) is_plus_by_y = true;
  594.  
  595.                     if (is_plus_by_x) {
  596.                         while (curr_x < mouse_x) {
  597.                             curr_x += X_DEVIATION;
  598.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  599.                             SDL_RenderClear(renderer);
  600.                             draw_circle(renderer, curr_x, curr_y, false);
  601.                             SDL_RenderPresent(renderer);
  602.  
  603.                             if (restart_if_is_out_of_bound(renderer, &curr_x, &curr_y)) {
  604.                                 ignore_all_conditions = true;
  605.                                 break;
  606.                             }
  607.                         }
  608.                     }
  609.                     else {
  610.                         while (curr_x > mouse_x) {
  611.                             curr_x -= X_DEVIATION;
  612.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  613.                             SDL_RenderClear(renderer);
  614.                             draw_circle(renderer, curr_x, curr_y, false);
  615.                             SDL_RenderPresent(renderer);
  616.  
  617.                             if (restart_if_is_out_of_bound(renderer, &curr_x, &curr_y)) {
  618.                                 ignore_all_conditions = true;
  619.                                 break;
  620.                             }
  621.                         }
  622.                     }
  623.  
  624.                     if (ignore_all_conditions) break; // break from curr switch branch
  625.  
  626.                     if (is_plus_by_y) {
  627.                         while (curr_y < mouse_Y) {
  628.                             curr_y += Y_DEVIATION;
  629.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  630.                             SDL_RenderClear(renderer);
  631.                             draw_circle(renderer, curr_x, curr_y, false);
  632.                             SDL_RenderPresent(renderer);
  633.  
  634.                             if (restart_if_is_out_of_bound(renderer, &curr_x, &curr_y)) {
  635.                                 ignore_all_conditions = true;
  636.                                 break;
  637.                             }
  638.                         }
  639.                     }
  640.                     else {
  641.                         while (curr_y > mouse_Y) {
  642.                             curr_y -= Y_DEVIATION;
  643.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  644.                             SDL_RenderClear(renderer);
  645.                             draw_circle(renderer, curr_x, curr_y, false);
  646.                             SDL_RenderPresent(renderer);
  647.  
  648.                             if (restart_if_is_out_of_bound(renderer, &curr_x, &curr_y)) {
  649.                                 ignore_all_conditions = true;
  650.                                 break;
  651.                             }
  652.                         }
  653.                     }
  654.  
  655.                 }
  656.                 if (event.button.button == SDL_BUTTON_RIGHT) {
  657.                     bool curr_y_in_top_half = curr_y <= SCREEN_HEIGHT / 2 && curr_y > 0;
  658.                     bool curr_x_in_left_half = curr_x <= SCREEN_WIDTH / 2 && curr_x > 0;
  659.  
  660.                    
  661.                     if (curr_y_in_top_half && curr_x_in_left_half) { // top left angle
  662.                         bool is_curr_x_more = false;
  663.                         bool is_curr_y_more = false;
  664.                         if (curr_x > START_X) is_curr_x_more = true;
  665.                         if (curr_y > START_Y) is_curr_x_more = true;
  666.  
  667.                         while (curr_x > START_X) {
  668.                             curr_x -= X_DEVIATION;
  669.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  670.                             SDL_RenderClear(renderer);
  671.                             draw_circle(renderer, curr_x, curr_y, false);
  672.                             SDL_RenderPresent(renderer);
  673.                         }
  674.  
  675.                         while (curr_x < START_X) {
  676.                             curr_x += X_DEVIATION;
  677.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  678.                             SDL_RenderClear(renderer);
  679.                             draw_circle(renderer, curr_x, curr_y, false);
  680.                             SDL_RenderPresent(renderer);
  681.                         }
  682.                        
  683.                         while (curr_y > START_Y) {
  684.                             curr_y -= Y_DEVIATION;
  685.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  686.                             SDL_RenderClear(renderer);
  687.                             draw_circle(renderer, curr_x, curr_y, false);
  688.                             SDL_RenderPresent(renderer);
  689.                         }
  690.  
  691.                         while (curr_y < START_Y) {
  692.                             curr_y += Y_DEVIATION;
  693.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  694.                             SDL_RenderClear(renderer);
  695.                             draw_circle(renderer, curr_x, curr_y, false);
  696.                             SDL_RenderPresent(renderer);
  697.                         }
  698.                     }
  699.                     if (curr_y_in_top_half && !curr_x_in_left_half) { // top right angle
  700.                         while (curr_x > SCREEN_WIDTH - START_X) {
  701.                             curr_x -= X_DEVIATION;
  702.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  703.                             SDL_RenderClear(renderer);
  704.                             draw_circle(renderer, curr_x, curr_y, false);
  705.                             SDL_RenderPresent(renderer);
  706.                         }
  707.  
  708.                         while (curr_x < SCREEN_WIDTH - START_X) {
  709.                             curr_x += X_DEVIATION;
  710.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  711.                             SDL_RenderClear(renderer);
  712.                             draw_circle(renderer, curr_x, curr_y, false);
  713.                             SDL_RenderPresent(renderer);
  714.                         }
  715.  
  716.                         while (curr_y > START_Y) {
  717.                             curr_y -= Y_DEVIATION;
  718.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  719.                             SDL_RenderClear(renderer);
  720.                             draw_circle(renderer, curr_x, curr_y, false);
  721.                             SDL_RenderPresent(renderer);
  722.                         }
  723.  
  724.                         while (curr_y < START_Y) {
  725.                             curr_y += Y_DEVIATION;
  726.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  727.                             SDL_RenderClear(renderer);
  728.                             draw_circle(renderer, curr_x, curr_y, false);
  729.                             SDL_RenderPresent(renderer);
  730.                         }
  731.                     }
  732.                     if (!curr_y_in_top_half && curr_x_in_left_half) { // bottom left angle
  733.                         while (curr_x > START_X) {
  734.                             curr_x -= X_DEVIATION;
  735.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  736.                             SDL_RenderClear(renderer);
  737.                             draw_circle(renderer, curr_x, curr_y, false);
  738.                             SDL_RenderPresent(renderer);
  739.                         }
  740.  
  741.                         while (curr_x < START_X) {
  742.                             curr_x += X_DEVIATION;
  743.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  744.                             SDL_RenderClear(renderer);
  745.                             draw_circle(renderer, curr_x, curr_y, false);
  746.                             SDL_RenderPresent(renderer);
  747.                         }
  748.  
  749.                         while (curr_y > SCREEN_HEIGHT - START_Y) {
  750.                             curr_y -= Y_DEVIATION;
  751.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  752.                             SDL_RenderClear(renderer);
  753.                             draw_circle(renderer, curr_x, curr_y, false);
  754.                             SDL_RenderPresent(renderer);
  755.                         }
  756.  
  757.                         while (curr_y < SCREEN_HEIGHT - START_Y) {
  758.                             curr_y += Y_DEVIATION;
  759.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  760.                             SDL_RenderClear(renderer);
  761.                             draw_circle(renderer, curr_x, curr_y, false);
  762.                             SDL_RenderPresent(renderer);
  763.                         }
  764.                     }
  765.                     if (!curr_y_in_top_half && !curr_x_in_left_half) { // bottom right angle
  766.                         while (curr_x > SCREEN_WIDTH - START_X) {
  767.                             curr_x -= X_DEVIATION;
  768.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  769.                             SDL_RenderClear(renderer);
  770.                             draw_circle(renderer, curr_x, curr_y, false);
  771.                             SDL_RenderPresent(renderer);
  772.                         }
  773.  
  774.                         while (curr_x < SCREEN_WIDTH - START_X) {
  775.                             curr_x += X_DEVIATION;
  776.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  777.                             SDL_RenderClear(renderer);
  778.                             draw_circle(renderer, curr_x, curr_y, false);
  779.                             SDL_RenderPresent(renderer);
  780.                         }
  781.  
  782.                         while (curr_y > SCREEN_HEIGHT - START_Y) {
  783.                             curr_y -= Y_DEVIATION;
  784.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  785.                             SDL_RenderClear(renderer);
  786.                             draw_circle(renderer, curr_x, curr_y, false);
  787.                             SDL_RenderPresent(renderer);
  788.                         }
  789.  
  790.                         while (curr_y < SCREEN_HEIGHT - START_Y) {
  791.                             curr_y += Y_DEVIATION;
  792.                             SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
  793.                             SDL_RenderClear(renderer);
  794.                             draw_circle(renderer, curr_x, curr_y, false);
  795.                             SDL_RenderPresent(renderer);
  796.                         }
  797.                     }
  798.                 }
  799.                 break;
  800.             }
  801.         }
  802.        
  803.  
  804.     }
  805.  
  806.     SDL_DestroyRenderer(renderer);
  807.     SDL_DestroyWindow(window);
  808.     SDL_Quit();
  809. }
  810.  
  811. int main(int argc, char** argv)
  812. {
  813.     int num_of_lab;
  814.  
  815.     do {
  816.  
  817.         cout << "Num of the task "
  818.             << "\n 1 - Lab 19, 1 task"
  819.             << "\n 2 - Lab 20, 1 task"
  820.             << "\n 3 - Lab 20, 2 task"
  821.             << "\n 4 - Lab 20, 3 task"
  822.             << "\n 5 - Lab 21, 1 task: "
  823.             << "\n 6 - Lab 22, 1 task: ";
  824.         cin >> num_of_lab;
  825.  
  826.     switch (num_of_lab) {
  827.         case 1: lab_19_1(); break;
  828.         case 2: lab_20_1(); break;
  829.         case 3: lab_20_2(); break;
  830.         case 4: lab_20_3(); break;
  831.         case 5: lab_21_1(); break;
  832.         case 6: lab_22_1(); break;
  833.         default:
  834.                cout << "Num by " << num_of_lab << " cannot be found";
  835.                break;
  836.          }
  837.  
  838.     } while (num_of_lab != -1);
  839.  
  840.  
  841.  
  842.  return 0;
  843. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement