Advertisement
CherMi

Lab 14 final

Dec 20th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.69 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <errno.h>
  5. #include <stdlib.h>
  6. #include "graphics.h"
  7.  
  8. double f(double x); //Подпрограмма, вычисляющая значение функции x^2*exp(-x)
  9. void draw_chart(double a, double b, int x_steps, int y_steps); //Подпрограмма, рисующая график функции x^2*exp(-x)
  10. void draw_histogram(double a, double b, int x_steps, int y_steps); //Подпрограмма, рисующая гистограмму функции x^2*exp(-x)
  11. double find_max(double a, double b, int steps);
  12.  
  13. int main()
  14. {
  15.     int res;
  16.     double a = -1, b = 2;
  17.  
  18.     int gdriver = DETECT, gmode;
  19.     initgraph(&gdriver, &gmode, ""); //Инициализация графического режима
  20.     res = graphresult();
  21.     if (res != grOk)
  22.     {
  23.         printf("An error has occured while initializing graphicsl output.");
  24.         return -1;
  25.     }
  26.  
  27.     draw_chart(a, b, 78, 25);
  28.     draw_histogram(a, b, 60, 80);
  29.     writeimagefile(NULL, 0, 0, getmaxx() - 1, getmaxy() - 1, 1, NULL);
  30.     closegraph(); //Выход из графического режима
  31.     return 0;
  32. }
  33.  
  34. double f(double x) //Подпрограмма, вычисляющая значение функции x^2*exp(-x)
  35. {
  36.     double result;
  37.     result = x * x;
  38.     return (result / exp(x));
  39. }
  40.  
  41. void draw_chart(double a, double b, int x_steps, int y_steps) //Подпрограмма, рисующая график функции x^2*exp(-x)
  42. {
  43.     int i, j, max_x, max_y, zero_x, zero_y;
  44.     double x, y;
  45.     int poly[6];
  46.     char str[10];
  47.     double top_y, x_inc, del_x, del_y;
  48.     double len = b - a; //Длина отрезка
  49.     double offset_percent; //Процент от максимальных координат, который отступается от осей
  50.     double cur_x, cur_y;
  51.     double *arr = (double *) malloc(y_steps * sizeof(double));
  52.  
  53.     max_x = getmaxx();
  54.     max_y = getmaxy();
  55.  
  56.     //Сделать фон белым, а цвет рисования чёрным.
  57.     setfillstyle(1, WHITE);
  58.     bar(0, 0, max_x, max_y);
  59.     setcolor(BLACK);
  60.     setlinestyle(0, 0, 1);
  61.     setbkcolor(WHITE);
  62.  
  63.     offset_percent = 0.1;
  64.  
  65.     //Вычисляем положение нуля на оси x
  66.     zero_x = 0;
  67.     if (a < 0 && b >0)
  68.     {
  69.         zero_x = (fabs(a)/len) * x_steps;
  70.     }
  71.     if (a < 0 && b < 0)
  72.     {
  73.         zero_x = x_steps - 1;
  74.     }
  75.  
  76.     zero_y = y_steps - 1;
  77.  
  78.     line(max_x * offset_percent, max_y * (1 - offset_percent), max_x * (1 - offset_percent), max_y * (1 - offset_percent)); //Ось x
  79.     del_x = (max_x * (1 - 2 * offset_percent)) / x_steps; //Цена деления по оси x в пикселях
  80.     for (i = 0; i < x_steps; i++)
  81.     {
  82.         line(max_x * offset_percent + i * del_x, max_y * (1 - offset_percent) + 1, max_x * offset_percent + i * del_x, max_y * (1 - offset_percent) - 1);
  83.     }
  84.     poly[0] = max_x * (1 - offset_percent) - 5;
  85.     poly[1] = max_y * (1 - offset_percent) + 3;
  86.     poly[2] = max_x * (1 - offset_percent);
  87.     poly[3] = max_y * (1 - offset_percent);
  88.     poly[4] = max_x * (1 - offset_percent) - 5;
  89.     poly[5] = max_y * (1 - offset_percent) - 3;
  90.     drawpoly(3, poly);
  91.     outtextxy(max_x * (1 - offset_percent), max_y * (1 - offset_percent) + 5, "x");
  92.  
  93.     line(max_x * offset_percent + zero_x * del_x, max_y * (1 - offset_percent), max_x * offset_percent + zero_x * del_x, max_y * offset_percent); //Ось y
  94.     del_y = (max_y * (1 - 2 * offset_percent)) / y_steps; //Цена деления по оси y в пикселях
  95.     cur_x = max_x * offset_percent + zero_x * del_x;
  96.     for (i = 0; i < y_steps; i++)
  97.     {
  98.         cur_y = max_y * (1 - offset_percent) - i * del_y;
  99.         arr[i] = cur_y; //Массив с координатами всех делений по оси y
  100.         line(cur_x - 1, cur_y, cur_x + 1, cur_y);
  101.     }
  102.     poly[0] = max_x * offset_percent + zero_x * del_x + 3;
  103.     poly[1] = max_y * offset_percent + 5;
  104.     poly[2] = max_x * offset_percent + zero_x * del_x;
  105.     poly[3] = max_y * offset_percent;
  106.     poly[4] = max_x * offset_percent + zero_x * del_x - 3;
  107.     poly[5] = max_y * offset_percent + 5;
  108.     drawpoly(3, poly);
  109.     outtextxy(max_x * offset_percent + zero_x * del_x - 15, max_y * offset_percent - 10, "y");
  110.  
  111.     top_y = find_max(a, b, x_steps); //Наибольшее значение по y
  112.     del_y = (max_y * (1 - 2 *offset_percent)) / top_y; //Цена деления по оси y;
  113.  
  114.     x_inc = len / x_steps;
  115.     x = a;
  116.     for (i = 0; i < x_steps; i++, x += x_inc)
  117.     {
  118.         cur_y = max_y * (1 - offset_percent) - f(x) * del_y;
  119.        
  120.         for (j = 1; j < y_steps; j++) //Найти ближайшее деление по оси y
  121.         {
  122.             if (cur_y < arr[j - 1] && cur_y >= arr[j + 1])
  123.             {
  124.                 cur_y = (fabs(arr[j - 1] - cur_y) > fabs(arr[j] - cur_y)) ? arr [j] : arr[j-1];
  125.             }
  126.         }
  127.  
  128.         cur_x = max_x * offset_percent + i * del_x;
  129.         setfillstyle(1, BLACK);
  130.         circle(cur_x, cur_y, 2);
  131.         floodfill(cur_x, cur_y, BLACK);
  132.     }
  133.     free(arr);
  134.     system("pause");
  135. }
  136.  
  137. void draw_histogram(double a, double b, int x_steps, int y_steps)
  138. {
  139.     int i, max_x, max_y, zero_x, zero_y, y;
  140.     double x, tmp;
  141.     int poly[6];
  142.     char str[10];
  143.     double top_y, x_inc, del_x, del_y, c_y;
  144.     double len = b - a; //Длина отрезка
  145.     double offset_percent; //Процент от максимальных координат, который отступается от осей.
  146.  
  147.     max_x = getmaxx();
  148.     max_y = getmaxy();
  149.  
  150.     //Сделать фон белым, а цвет рисования чёрным.
  151.     setfillstyle(1, WHITE);
  152.     bar(0, 0, max_x, max_y);
  153.     setcolor(BLACK);
  154.     setlinestyle(0, 0, 1);
  155.  
  156.     offset_percent = 0.1;
  157.  
  158.     //Вычисляем положение нуля по вертикали (на оси y)
  159.     zero_x = 0;
  160.     if (a < 0 && b >0)
  161.     {
  162.         zero_x += (fabs(a) / len) * x_steps;
  163.     }
  164.     if (a < 0 && b < 0)
  165.     {
  166.         zero_x = x_steps - 1;
  167.     }
  168.  
  169.     zero_y = 0;
  170.  
  171.     del_y = (max_x * (1 - 2 * offset_percent)) / y_steps;
  172.     del_x = max_y * (1 - 2 * offset_percent) / x_steps;
  173.     line(max_x * offset_percent, max_y * offset_percent + zero_x * del_y, max_x * (1 - offset_percent), max_y * offset_percent + zero_x * del_y); //Ось y (горизонтальная)
  174.  
  175.     for (i = 0; i < y_steps; i++) //Деления по оси y
  176.     {
  177.         line(max_x * offset_percent + i * del_y, max_y * offset_percent + zero_x * del_y + 1, max_x * offset_percent + i * del_y, max_y * offset_percent + zero_x * del_y - 1);
  178.     }
  179.     poly[0] = max_x * (1 - offset_percent) - 5;
  180.     poly[1] = max_y * offset_percent + zero_x * del_y - 3;
  181.     poly[2] = max_x * (1 - offset_percent);
  182.     poly[3] = max_y * offset_percent + zero_x * del_y;
  183.     poly[4] = max_x * (1 - offset_percent)- 5;
  184.     poly[5] = max_y * offset_percent + zero_x * del_y + 3;
  185.     drawpoly(3, poly);
  186.     outtextxy(max_x * (1 - offset_percent), max_y * offset_percent + zero_x * del_y + 5, "y");
  187.  
  188.     line(max_x * offset_percent , max_y * offset_percent, max_x * offset_percent , max_y *  (1 - offset_percent)); //Ось x (вертикальная)
  189.     for (i = 0; i < x_steps; i++) //Деления по оси x
  190.     {
  191.         line(max_x*offset_percent - 1, max_y * offset_percent + del_x * i, max_x *offset_percent + 1, max_y * offset_percent + del_x * i);
  192.     }
  193.     poly[0] = max_x * offset_percent + 3;
  194.     poly[1] = max_y * (1 - offset_percent) - 5;
  195.     poly[2] = max_x * offset_percent;
  196.     poly[3] = max_y * (1 - offset_percent);
  197.     poly[4] = max_x * offset_percent - 3;
  198.     poly[5] = max_y * (1 - offset_percent) - 5;
  199.     drawpoly(3, poly);
  200.     outtextxy(max_x *offset_percent - 15, max_y * (1 - offset_percent) , "x");
  201.  
  202.     tmp = del_y;
  203.     top_y = find_max(a, b, x_steps);
  204.     del_y = (max_x * (1 - 2 * offset_percent)) / top_y; //Цена делания по оси y (горизонтальная)
  205.  
  206.  
  207.     double prev_x, prev_y, cur_x, cur_y;
  208.  
  209.     settextstyle(SMALL_FONT, HORIZ_DIR, 3);
  210.     c_y = top_y / y_steps; //Цена деления по оси y в единицах измерения y
  211.     for (i = 1; i < y_steps - 1; i++) //Подписи по оси y
  212.     {
  213.         if (i % 10 == 0)
  214.         {
  215.             sprintf(str, "%3.1f", c_y * i);
  216.             outtextxy(max_x * offset_percent + i * tmp - 4, max_y * offset_percent + zero_x * tmp + 2, str);
  217.         }
  218.     }
  219.  
  220.  
  221.     x_inc = len / x_steps;
  222.     for (x = a, i = 0; i < x_steps; i++, x += x_inc)
  223.     {
  224.         tmp = f(x);
  225.         cur_x = max_y * offset_percent + i * del_x;
  226.         cur_y = max_x * offset_percent + tmp * del_y;
  227.  
  228.         y = max_x * offset_percent+2;
  229.         while (y <= cur_y)
  230.         {
  231.             circle(y, cur_x, 2);
  232.             y += 4;
  233.         }
  234.         if(i != zero_x)
  235.         {
  236.             bar(cur_y, cur_x - 2, cur_y + 10, cur_x + 2);
  237.         }
  238.  
  239.         sprintf(str, "%f", tmp); //Подписи по вертикальной оси
  240.         outtextxy(cur_y + 10, cur_x - 3, str);
  241.     }
  242.     //system("pause");
  243. }
  244.  
  245. double find_max(double a, double b, int steps)
  246. {
  247.     int i;
  248.     double max = f(a);
  249.     double len = b - a;
  250.     double del = len / steps;
  251.     double tmp;
  252.  
  253.     for (i = 1; i < steps; i++)
  254.     {
  255.         tmp = f(a + i * del);
  256.         if (max < tmp)
  257.         {
  258.             max = tmp;
  259.         }
  260.     }
  261.     return max;
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement