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;
- void RenderScene(void);
- void ChangeSize(int, int);
- void TimerFunction(int);
- 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 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 DrawCoordinateSystem()
- {
- glColor3f(0.0, 0.0, 0.0); // Чёрный цвет для осей
- // Рисуем ось X
- glBegin(GL_LINES);
- glVertex2f(-3.14f, 0.0f); // Начало оси X
- glVertex2f(3.14f, 0.0f); // Конец оси X
- glEnd();
- // Рисуем ось Y
- glBegin(GL_LINES);
- glVertex2f(0.0f, -3.14f); // Начало оси Y
- glVertex2f(0.0f, 3.14f); // Конец оси Y
- glEnd();
- // Рисуем деления на осях
- for (float i = -3.14f; i <= 3.14f; i += 0.5f)
- {
- // Деления по оси X
- glBegin(GL_LINES);
- glVertex2f(i, 0.05f);
- glVertex2f(i, -0.05f);
- glEnd();
- // Деления по оси Y
- glBegin(GL_LINES);
- glVertex2f(0.05f, i);
- glVertex2f(-0.05f, i);
- glEnd();
- }
- }
- bool is_in_figure(Point a) {
- float result = (exp(a.x) * sin(a.x));
- // Если 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)
- {
- glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
- glClear(GL_COLOR_BUFFER_BIT);
- DrawCoordinateSystem(); // Рисуем систему координат
- 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 = (5.56896);
- void TimerFunction(int value) {
- float minX = -2 * 3.14 / 3; // минимальное значение x
- float maxX = 2 * 3.14 / 3; // максимальное значение x
- float minY = -2 * 3.14 / 3; // минимальное значение y
- float maxY = 2 * 3.14 / 3; // максимальное значение 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 * 17.6;
- 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 ChangeSize(int width, int height)
- {
- GLfloat aspectRatio;
- 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("Template"); // Создание окна, его название
- 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