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)
- void save_chart(); //Подпрограмма, рисующая и сохраняющая гистограмму функции x^2*exp(-x)
- 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);
- save_chart();
- 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, 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; //Процент от максимальных координат, который отступается от осей
- 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);
- /*if (i % 10 == 0)
- {
- sprintf(str, "%3.2f", del_x * i); //TODO: переделать
- outtextxy(max_x * offset_percent + i * del_x - 10, zero_y + 10, str);
- }*/
- }
- 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 в пикселях
- for (i = 0; i < y_steps; i++)
- {
- 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);
- }
- 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");
- //TODO: добавить подписи
- top_y = (f(a) > f(b)) ? f(a) : f (b); //Наибольшее значение по y. TODO: исправить
- del_y = (max_y * (1 - 2 *offset_percent)) / top_y; //Цена деления по оси y;
- x_inc = len / x_steps;
- double prev_x, prev_y, cur_x, cur_y;
- x = a;
- for (i = 0; i < x_steps; i++, x += x_inc)
- {
- cur_x = max_x * offset_percent + i * del_x;
- cur_y = max_y * (1 - offset_percent) - f(x) * del_y;
- setfillstyle(1, BLACK);
- circle(cur_x, cur_y, 2);
- floodfill(cur_x, cur_y, BLACK);
- prev_x = cur_x;
- prev_y = cur_y;
- }
- 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;
- double len = b - a; //Длина отрезка
- double offset_percent; //Процент от максимальных координат, который отступается от осей.
- max_x = getmaxx();
- max_y = getmaxy();
- //Сделать фон белым, а цвет рисования чёрным.
- setfillstyle(1, 15);
- bar(0, 0, max_x, max_y);
- setcolor(0);
- 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++)
- {
- 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 (вертикальная)
- del_x = (max_y * (1 - 2 * offset_percent)) / x_steps;
- for (i = 0; i < x_steps; i++)
- {
- 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 = (f(a) > f(b)) ? f(a) : f(b); //Наибольшее значение по y. TODO: исправить
- del_y = (max_x * (1 - 2 * offset_percent)) / top_y; //Цена делания по оси y (горизонтальная)
- x_inc = len / x_steps;
- double prev_x, prev_y, cur_x, cur_y;
- settextstyle(SMALL_FONT, HORIZ_DIR, 3);
- setfillstyle(1, CYAN);
- /*for (i = 1; i < y_steps - 1; i++)
- {
- if (i % 10 == 0)
- {
- sprintf(str, "%3.2f", f(a + x_inc * i));
- outtextxy(max_x * offset_percent + i * tmp, max_y * offset_percent + zero_x * tmp + 10, str);
- }
- }*/
- for (x = a, i = 0; i < x_steps; i++, x += x_inc)
- {
- tmp = f(x);
- cur_y = max_x * offset_percent + tmp * del_y;
- cur_x = max_y * offset_percent + i * del_x;
- if (cur_y - max_x * offset_percent < 2)
- {
- circle(cur_y, cur_x, 2);
- }
- else
- {
- y = max_x * offset_percent+2;
- while (y <= cur_y)
- {
- circle(y, cur_x, 2);
- y += 4;
- }
- }
- sprintf(str, "%f", tmp);
- outtextxy(cur_y + 1, cur_x - 3, str);
- prev_x = cur_x;
- prev_y = cur_y;
- }
- }
- void save_chart()
- {
- writeimagefile(NULL, 0, 0, getmaxx() - 1, getmaxy() - 1, 1, NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement