Advertisement
CherMi

Lab 14

Dec 12th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.40 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. void save_chart(); //Подпрограмма, рисующая и сохраняющая гистограмму функции x^2*exp(-x)
  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.     save_chart();
  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, 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.  
  51.     max_x = getmaxx();
  52.     max_y = getmaxy();
  53.  
  54.     //Сделать фон белым, а цвет рисования чёрным.
  55.     setfillstyle(1, WHITE);
  56.     bar(0, 0, max_x, max_y);
  57.     setcolor(BLACK);
  58.     setlinestyle(0, 0, 1);
  59.     setbkcolor(WHITE);
  60.  
  61.     offset_percent = 0.1;
  62.  
  63.     //Вычисляем положение нуля на оси x
  64.     zero_x = 0;
  65.     if (a < 0 && b >0)
  66.     {
  67.         zero_x += (fabs(a)/len) * x_steps;
  68.     }
  69.     if (a < 0 && b < 0)
  70.     {
  71.         zero_x = x_steps - 1;
  72.     }
  73.  
  74.     zero_y = y_steps - 1;
  75.  
  76.     line(max_x * offset_percent, max_y * (1 - offset_percent), max_x * (1 - offset_percent), max_y * (1 - offset_percent)); //Ось x
  77.     del_x = (max_x * (1 - 2 * offset_percent)) / x_steps; //Цена деления по оси x в пикселя
  78.     for (i = 0; i < x_steps; i++)
  79.     {
  80.         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);
  81.         /*if (i % 10 == 0)
  82.         {
  83.             sprintf(str, "%3.2f", del_x * i); //TODO: переделать
  84.             outtextxy(max_x * offset_percent + i * del_x - 10, zero_y + 10, str);
  85.         }*/
  86.     }
  87.     poly[0] = max_x * (1 - offset_percent) - 5;
  88.     poly[1] = max_y * (1 - offset_percent) + 3;
  89.     poly[2] = max_x * (1 - offset_percent);
  90.     poly[3] = max_y * (1 - offset_percent);
  91.     poly[4] = max_x * (1 - offset_percent) - 5;
  92.     poly[5] = max_y * (1 - offset_percent) - 3;
  93.     drawpoly(3, poly);
  94.     outtextxy(max_x * (1 - offset_percent), max_y * (1 - offset_percent) + 5, "x");
  95.  
  96.     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
  97.     del_y = (max_y * (1 - 2 * offset_percent)) / y_steps; //Цена деления по оси y в пикселях
  98.     for (i = 0; i < y_steps; i++)
  99.     {
  100.         line(max_x * offset_percent + zero_x * del_x - 1, max_y * (1 - offset_percent) - i * del_y, max_x * offset_percent + zero_x * del_x + 1, max_y * (1 - offset_percent) - i * del_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.     //TODO: добавить подписи
  111.  
  112.     top_y = (f(a) > f(b)) ?  f(a) : f (b); //Наибольшее значение по y. TODO: исправить
  113.     del_y = (max_y * (1 - 2 *offset_percent)) / top_y; //Цена деления по оси y;
  114.  
  115.     x_inc = len / x_steps;
  116.     double prev_x, prev_y, cur_x, cur_y;
  117.     x = a;
  118.     for (i = 0; i < x_steps; i++, x += x_inc)
  119.     {
  120.         cur_x = max_x * offset_percent + i * del_x;
  121.         cur_y = max_y * (1 - offset_percent) - f(x) * del_y;
  122.         setfillstyle(1, BLACK);
  123.         circle(cur_x, cur_y, 2);
  124.         floodfill(cur_x, cur_y, BLACK);
  125.         prev_x = cur_x;
  126.         prev_y = cur_y;
  127.     }
  128.     system("pause");
  129. }
  130.  
  131. void draw_histogram(double a, double b, int x_steps, int y_steps)
  132. {
  133.     int i, max_x, max_y, zero_x, zero_y, y;
  134.     double x, tmp;
  135.     int poly[6];
  136.     char str[10];
  137.     double top_y, x_inc, del_x, del_y;
  138.     double len = b - a; //Длина отрезка
  139.     double offset_percent; //Процент от максимальных координат, который отступается от осей.
  140.  
  141.     max_x = getmaxx();
  142.     max_y = getmaxy();
  143.  
  144.     //Сделать фон белым, а цвет рисования чёрным.
  145.     setfillstyle(1, 15);
  146.     bar(0, 0, max_x, max_y);
  147.     setcolor(0);
  148.     setlinestyle(0, 0, 1);
  149.  
  150.     offset_percent = 0.1;
  151.  
  152.     //Вычисляем положение нуля по вертикали (на оси y)
  153.     zero_x = 0;
  154.     if (a < 0 && b >0)
  155.     {
  156.         zero_x += (fabs(a) / len) * x_steps;
  157.     }
  158.     if (a < 0 && b < 0)
  159.     {
  160.         zero_x = x_steps - 1;
  161.     }
  162.  
  163.     zero_y = 0;
  164.  
  165.     del_y = (max_x * (1 - 2 * offset_percent)) / y_steps;
  166.     del_x = max_y * (1 - 2 * offset_percent) / x_steps;
  167.     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 (горизонтальная)
  168.     for (i = 0; i < y_steps; i++)
  169.     {
  170.         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);
  171.     }
  172.     poly[0] = max_x * (1 - offset_percent) - 5;
  173.     poly[1] = max_y * offset_percent + zero_x * del_y - 3;
  174.     poly[2] = max_x * (1 - offset_percent);
  175.     poly[3] = max_y * offset_percent + zero_x * del_y;
  176.     poly[4] = max_x * (1 - offset_percent)- 5;
  177.     poly[5] = max_y * offset_percent + zero_x * del_y + 3;
  178.     drawpoly(3, poly);
  179.     outtextxy(max_x * (1 - offset_percent), max_y * offset_percent + zero_x * del_y + 5, "y");
  180.  
  181.     line(max_x * offset_percent , max_y * offset_percent, max_x * offset_percent , max_y *  (1 - offset_percent)); //Ось x (вертикальная)
  182.     del_x = (max_y * (1 - 2 * offset_percent)) / x_steps;
  183.     for (i = 0; i < x_steps; i++)
  184.     {
  185.         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);
  186.     }
  187.     poly[0] = max_x * offset_percent + 3;
  188.     poly[1] = max_y * (1 - offset_percent) - 5;
  189.     poly[2] = max_x * offset_percent;
  190.     poly[3] = max_y * (1 - offset_percent);
  191.     poly[4] = max_x * offset_percent - 3;
  192.     poly[5] = max_y * (1 - offset_percent) - 5;
  193.     drawpoly(3, poly);
  194.     outtextxy(max_x *offset_percent - 15, max_y * (1 - offset_percent) , "x");
  195.  
  196.     tmp = del_y;
  197.     top_y = (f(a) > f(b)) ? f(a) : f(b); //Наибольшее значение по y. TODO: исправить
  198.     del_y = (max_x * (1 - 2 * offset_percent)) / top_y; //Цена делания по оси y (горизонтальная)
  199.  
  200.     x_inc = len / x_steps;
  201.  
  202.     double prev_x, prev_y, cur_x, cur_y;
  203.     settextstyle(SMALL_FONT, HORIZ_DIR, 3);
  204.     setfillstyle(1, CYAN);
  205.  
  206.     /*for (i = 1; i < y_steps - 1; i++)
  207.     {
  208.         if (i % 10 == 0)
  209.         {
  210.             sprintf(str, "%3.2f", f(a + x_inc * i));
  211.             outtextxy(max_x * offset_percent + i * tmp, max_y * offset_percent + zero_x * tmp + 10, str);
  212.         }
  213.     }*/
  214.  
  215.     for (x = a, i = 0; i < x_steps; i++, x += x_inc)
  216.     {
  217.         tmp = f(x);
  218.         cur_y = max_x * offset_percent + tmp * del_y;
  219.         cur_x = max_y * offset_percent + i * del_x;
  220.  
  221.         if (cur_y - max_x * offset_percent < 2)
  222.         {
  223.             circle(cur_y, cur_x, 2);
  224.         }
  225.         else
  226.         {
  227.             y = max_x * offset_percent+2;
  228.             while (y <= cur_y)
  229.             {
  230.                 circle(y, cur_x, 2);
  231.                 y += 4;
  232.             }
  233.         }
  234.         sprintf(str, "%f", tmp);
  235.         outtextxy(cur_y + 1, cur_x - 3, str);
  236.         prev_x = cur_x;
  237.         prev_y = cur_y;
  238.     }
  239.  
  240. }
  241.  
  242.  
  243. void save_chart()
  244. {
  245.     writeimagefile(NULL, 0, 0, getmaxx() - 1, getmaxy() - 1, 1, NULL);
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement