Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include "glut.h"
- #include "math.h"
- #include <windows.h>
- #include <iomanip>
- using namespace std;
- int size = 10;
- int iter = 0;
- GLfloat windowWidth = 500;
- GLfloat windowHeight = 500;
- int generatedPointsCount = 0;
- int pointsInFigureCount = 0;
- struct Point
- {
- float x;
- float y;
- Point() : x(0), y(0) {};
- Point(float x, float y) : x(x), y(y) {};
- };
- vector <Point> Points(10);
- void DrawLine(float x1, float y1, float x2, float y2)
- {
- glBegin(GL_LINES);
- glColor3f(0.0, 0.0, 0.0);
- glVertex2f(x1, y1);
- glVertex2f(x2, y2);
- glEnd();
- }
- void DrawLine(Point a, Point b)
- {
- glBegin(GL_LINES);
- glColor3f(0.0, 0.0, 0.0);
- glVertex2f(a.x, a.y);
- glVertex2f(b.x, b.y);
- glEnd();
- }
- void DrawPoint(Point& a, int size, float fillFlag)
- {
- glColor3f(fillFlag, 0.0, 0.0);
- glPointSize(size);
- glBegin(GL_POINTS);
- glVertex2f(a.x, a.y);
- glEnd();
- }
- void DrawOs()
- {
- DrawLine(1000, 0, -1000, 0);
- DrawLine(0, 1000, 0, -1000);
- DrawLine(1000, 0, 950, 30);
- DrawLine(1000, 0, 950, -30);
- DrawLine(0, 1000, -30, 950);
- DrawLine(0, 1000, 30, 950);
- for (int i = -950; i <= 950; i += 50)
- {
- DrawLine(i, 15, i, -15);
- }
- for (int i = -950; i <= 950; i += 50)
- {
- DrawLine(15, i, -15, i);
- }
- }
- void DrawSmalOs()
- {
- DrawLine(10, 0, -10, 0);
- DrawLine(0, 10, 0, -10);
- DrawLine(10, 0, 9.5, 0.3);
- DrawLine(10, 0, 9.5, -0.3);
- DrawLine(0, 10, -0.3, 9.5);
- DrawLine(0, 10, 0.3, 9.5);
- for (float i = -9.5; i <= 9.6; i += 0.5)
- {
- DrawLine(i, 0.15, i, -0.15);
- }
- for (float i = -9.5; i <= 9.5; i += 0.5)
- {
- DrawLine(0.15, i, -0.15, i);
- }
- }
- bool is_in_figure(Point a)
- {
- float result = sin(2 * a.x) / (4 - pow(cos(2 * a.x), 2));
- // Если x < 0 и точка находится выше кривой до x=0
- if (a.y < 0 && a.y > result)
- {
- return true;
- }
- // Если x > 0 и точка находится ниже кривой до x=0
- else if (a.y > 0 && a.y < result)
- {
- return true;
- }
- // Если ни одно из условий не выполнено, точка не попадает под кривую
- return false;
- }
- void RenderScene(void)
- {
- setlocale(LC_ALL, "rus");
- glClearColor(1.0f, 1.0f, 1.0f, 0.0f); // Цвет фона окна
- glClear(GL_COLOR_BUFFER_BIT);
- DrawSmalOs();
- DrawLine(-1, 0.0, 1, 0.0);
- for (long long i = 0; i < Points.size(); i++)
- {
- if (is_in_figure(Points[i]))
- {
- DrawPoint(Points[i], 1, 1);
- }
- else
- {
- DrawPoint(Points[i], 1, 0);
- }
- }
- glutSwapBuffers();
- }
- float countResult = (0.13732);
- void TimerFunction(int value)
- {
- float minX = 3.14 / 4; // минимальное значение x
- float maxX = 3.14 / 2; // максимальное значение x
- float minY = 0; // минимальное значение y
- float maxY = 0.785; // максимальное значение y
- // Генерация случайных координат в указанном диапазоне
- float randX = minX + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (maxX - minX)));
- float randY = minY + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (maxY - minY)));
- Point a(randX, randY);
- generatedPointsCount++;
- if (is_in_figure(a))
- {
- pointsInFigureCount++;
- }
- Points.push_back(a);
- if (generatedPointsCount == 10 || generatedPointsCount == 100 || generatedPointsCount == 1000 || generatedPointsCount == 10000 || generatedPointsCount == 100000)
- {
- float result = (float)pointsInFigureCount / (float)generatedPointsCount;
- float square = result * 0.616;
- cout << "Ploshad Monte-Carlo " << fixed << setprecision(6) << square << endl;
- float pogreshnost = abs(square - countResult) / countResult * 100;
- cout << "Pogreshnost v % " << fixed << setprecision(2) << pogreshnost << endl;
- }
- if (generatedPointsCount == 10000)
- {
- cout << "Analiticheski = " << fixed << setprecision(6) << countResult << endl;
- }
- glutPostRedisplay();
- glutTimerFunc(10, TimerFunction, 1);
- }
- void SetupRC(void)
- {
- // Устанавливаем синий цвет очистки
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- }
- void ChangeSize(int width, int height)
- {
- GLfloat aspectRatio;
- // Предотвращаем деление на нуль
- if (height == 0)
- height = 1;
- // Устанавливаем поле просмотра с размерами окна
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-0.07, 0.07, -0.07, 0.07, 1.0, -1.0);
- aspectRatio = (GLfloat)width / (GLfloat)height;
- if (width <= height)
- {
- windowWidth = 100;
- windowHeight = 100 / aspectRatio;
- glOrtho(-100.0, 100.0,
- -windowHeight, windowHeight, 1.0, -1.0);
- }
- else
- {
- windowWidth = 100 * aspectRatio;
- windowHeight = 100;
- glOrtho(-windowWidth, windowWidth,
- -100.0, 100.0, 1.0, -1.0);
- }
- // Восстанавливает матрицу преобразования в исходный режим вида
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(1000, 1000); // Размер окна
- glutCreateWindow("Graphic D"); // Создание окна, его название
- glutDisplayFunc(RenderScene);
- glutReshapeFunc(ChangeSize);
- setlocale(LC_ALL, "rus");
- for (int i = 0; i < 1; i++) {
- glutTimerFunc(1, TimerFunction, 0);
- }
- srand(time(NULL));
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement