Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <math.h>
- #include <errno.h>
- #include <stdlib.h>
- #include "graphics.h"
- double f(double x); //Подпрограмма, вычисляющая значение функции x^2*exp(-x)
- void draw_chart(double a, double b, int x_steps, int y_steps); //Подпрограмма, рисующая график функции x^2*exp(-x)
- void draw_histogram(double a, double b, int x_steps, int y_steps); //Подпрограмма, рисующая гистограмму функции x^2*exp(-x)
- double find_max(double a, double b, int steps);
- int main()
- {
- int res;
- double a = -1, b = 2;
- int gdriver = DETECT, gmode;
- initgraph(&gdriver, &gmode, ""); //Инициализация графического режима
- res = graphresult();
- if (res != grOk)
- {
- printf("An error has occured while initializing graphicsl output.");
- return -1;
- }
- draw_chart(a, b, 78, 25);
- draw_histogram(a, b, 60, 80);
- writeimagefile(NULL, 0, 0, getmaxx() - 1, getmaxy() - 1, 1, NULL);
- closegraph(); //Выход из графического режима
- return 0;
- }
- double f(double x) //Подпрограмма, вычисляющая значение функции x^2*exp(-x)
- {
- double result;
- result = x * x;
- return (result / exp(x));
- }
- void draw_chart(double a, double b, int x_steps, int y_steps) //Подпрограмма, рисующая график функции x^2*exp(-x)
- {
- int i, j, max_x, max_y, zero_x, zero_y;
- double x, y;
- int poly[6];
- char str[10];
- double top_y, x_inc, del_x, del_y;
- double len = b - a; //Длина отрезка
- double offset_percent; //Процент от максимальных координат, который отступается от осей
- double cur_x, cur_y;
- double *arr = (double *) malloc(y_steps * sizeof(double));
- max_x = getmaxx();
- max_y = getmaxy();
- //Сделать фон белым, а цвет рисования чёрным.
- setfillstyle(1, WHITE);
- bar(0, 0, max_x, max_y);
- setcolor(BLACK);
- setlinestyle(0, 0, 1);
- setbkcolor(WHITE);
- offset_percent = 0.1;
- //Вычисляем положение нуля на оси x
- zero_x = 0;
- if (a < 0 && b >0)
- {
- zero_x = (fabs(a)/len) * x_steps;
- }
- if (a < 0 && b < 0)
- {
- zero_x = x_steps - 1;
- }
- zero_y = y_steps - 1;
- line(max_x * offset_percent, max_y * (1 - offset_percent), max_x * (1 - offset_percent), max_y * (1 - offset_percent)); //Ось x
- del_x = (max_x * (1 - 2 * offset_percent)) / x_steps; //Цена деления по оси x в пикселях
- for (i = 0; i < x_steps; i++)
- {
- 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);
- }
- poly[0] = max_x * (1 - offset_percent) - 5;
- poly[1] = max_y * (1 - offset_percent) + 3;
- poly[2] = max_x * (1 - offset_percent);
- poly[3] = max_y * (1 - offset_percent);
- poly[4] = max_x * (1 - offset_percent) - 5;
- poly[5] = max_y * (1 - offset_percent) - 3;
- drawpoly(3, poly);
- outtextxy(max_x * (1 - offset_percent), max_y * (1 - offset_percent) + 5, "x");
- 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
- del_y = (max_y * (1 - 2 * offset_percent)) / y_steps; //Цена деления по оси y в пикселях
- cur_x = max_x * offset_percent + zero_x * del_x;
- for (i = 0; i < y_steps; i++)
- {
- cur_y = max_y * (1 - offset_percent) - i * del_y;
- arr[i] = cur_y; //Массив с координатами всех делений по оси y
- line(cur_x - 1, cur_y, cur_x + 1, cur_y);
- }
- poly[0] = max_x * offset_percent + zero_x * del_x + 3;
- poly[1] = max_y * offset_percent + 5;
- poly[2] = max_x * offset_percent + zero_x * del_x;
- poly[3] = max_y * offset_percent;
- poly[4] = max_x * offset_percent + zero_x * del_x - 3;
- poly[5] = max_y * offset_percent + 5;
- drawpoly(3, poly);
- outtextxy(max_x * offset_percent + zero_x * del_x - 15, max_y * offset_percent - 10, "y");
- top_y = find_max(a, b, x_steps); //Наибольшее значение по y
- del_y = (max_y * (1 - 2 *offset_percent)) / top_y; //Цена деления по оси y;
- x_inc = len / x_steps;
- x = a;
- for (i = 0; i < x_steps; i++, x += x_inc)
- {
- cur_y = max_y * (1 - offset_percent) - f(x) * del_y;
- for (j = 1; j < y_steps; j++) //Найти ближайшее деление по оси y
- {
- if (cur_y < arr[j - 1] && cur_y >= arr[j + 1])
- {
- cur_y = (fabs(arr[j - 1] - cur_y) > fabs(arr[j] - cur_y)) ? arr [j] : arr[j-1];
- }
- }
- cur_x = max_x * offset_percent + i * del_x;
- setfillstyle(1, BLACK);
- circle(cur_x, cur_y, 2);
- floodfill(cur_x, cur_y, BLACK);
- }
- free(arr);
- system("pause");
- }
- void draw_histogram(double a, double b, int x_steps, int y_steps)
- {
- int i, max_x, max_y, zero_x, zero_y, y;
- double x, tmp;
- int poly[6];
- char str[10];
- double top_y, x_inc, del_x, del_y, c_y;
- double len = b - a; //Длина отрезка
- double offset_percent; //Процент от максимальных координат, который отступается от осей.
- max_x = getmaxx();
- max_y = getmaxy();
- //Сделать фон белым, а цвет рисования чёрным.
- setfillstyle(1, WHITE);
- bar(0, 0, max_x, max_y);
- setcolor(BLACK);
- setlinestyle(0, 0, 1);
- offset_percent = 0.1;
- //Вычисляем положение нуля по вертикали (на оси y)
- zero_x = 0;
- if (a < 0 && b >0)
- {
- zero_x += (fabs(a) / len) * x_steps;
- }
- if (a < 0 && b < 0)
- {
- zero_x = x_steps - 1;
- }
- zero_y = 0;
- del_y = (max_x * (1 - 2 * offset_percent)) / y_steps;
- del_x = max_y * (1 - 2 * offset_percent) / x_steps;
- 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 (горизонтальная)
- for (i = 0; i < y_steps; i++) //Деления по оси y
- {
- 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);
- }
- poly[0] = max_x * (1 - offset_percent) - 5;
- poly[1] = max_y * offset_percent + zero_x * del_y - 3;
- poly[2] = max_x * (1 - offset_percent);
- poly[3] = max_y * offset_percent + zero_x * del_y;
- poly[4] = max_x * (1 - offset_percent)- 5;
- poly[5] = max_y * offset_percent + zero_x * del_y + 3;
- drawpoly(3, poly);
- outtextxy(max_x * (1 - offset_percent), max_y * offset_percent + zero_x * del_y + 5, "y");
- line(max_x * offset_percent , max_y * offset_percent, max_x * offset_percent , max_y * (1 - offset_percent)); //Ось x (вертикальная)
- for (i = 0; i < x_steps; i++) //Деления по оси x
- {
- 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);
- }
- poly[0] = max_x * offset_percent + 3;
- poly[1] = max_y * (1 - offset_percent) - 5;
- poly[2] = max_x * offset_percent;
- poly[3] = max_y * (1 - offset_percent);
- poly[4] = max_x * offset_percent - 3;
- poly[5] = max_y * (1 - offset_percent) - 5;
- drawpoly(3, poly);
- outtextxy(max_x *offset_percent - 15, max_y * (1 - offset_percent) , "x");
- tmp = del_y;
- top_y = find_max(a, b, x_steps);
- del_y = (max_x * (1 - 2 * offset_percent)) / top_y; //Цена делания по оси y (горизонтальная)
- double prev_x, prev_y, cur_x, cur_y;
- settextstyle(SMALL_FONT, HORIZ_DIR, 3);
- c_y = top_y / y_steps; //Цена деления по оси y в единицах измерения y
- for (i = 1; i < y_steps - 1; i++) //Подписи по оси y
- {
- if (i % 10 == 0)
- {
- sprintf(str, "%3.1f", c_y * i);
- outtextxy(max_x * offset_percent + i * tmp - 4, max_y * offset_percent + zero_x * tmp + 2, str);
- }
- }
- x_inc = len / x_steps;
- for (x = a, i = 0; i < x_steps; i++, x += x_inc)
- {
- tmp = f(x);
- cur_x = max_y * offset_percent + i * del_x;
- cur_y = max_x * offset_percent + tmp * del_y;
- y = max_x * offset_percent+2;
- while (y <= cur_y)
- {
- circle(y, cur_x, 2);
- y += 4;
- }
- if(i != zero_x)
- {
- bar(cur_y, cur_x - 2, cur_y + 10, cur_x + 2);
- }
- sprintf(str, "%f", tmp); //Подписи по вертикальной оси
- outtextxy(cur_y + 10, cur_x - 3, str);
- }
- //system("pause");
- }
- double find_max(double a, double b, int steps)
- {
- int i;
- double max = f(a);
- double len = b - a;
- double del = len / steps;
- double tmp;
- for (i = 1; i < steps; i++)
- {
- tmp = f(a + i * del);
- if (max < tmp)
- {
- max = tmp;
- }
- }
- return max;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement