Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "glut.h"
- #include <cmath>
- #include <algorithm>
- #include <ctime>
- #include <iomanip>
- using namespace std;
- void display(void)
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0, 1.0, 0.0);
- int num_points = 10000;
- int points_inside = 0;
- for (int i = 0; i < num_points; i++)
- {
- double x = -2 + 4 * (rand() / (double)RAND_MAX);
- double y = -2 + 4 * (rand() / (double)RAND_MAX);
- if (-pow(x, 3) - pow(y, 4) < 2 && 3 * x + pow(y, 2) < 2 && (x > -2 && x < 2) && (y > -2 && y < 2))
- {
- points_inside++;
- glColor3f(1.0, 0.0, 0.0); // Красный цвет для точек внутри фигуры
- }
- else
- {
- glColor3f(0.0, 1.0, 0.0); // Зеленый цвет для точек снаружи фигуры
- }
- glBegin(GL_POINTS);
- glVertex2f(x, y);
- glEnd();
- // Выводим оси координат белым цветом
- glColor3f(1.0, 1.0, 1.0);
- glBegin(GL_LINES);
- // Ось x
- glVertex2f(-2.0, 0.0);
- glVertex2f(2.0, 0.0);
- // Ось y
- glVertex2f(0.0, -2.0);
- glVertex2f(0.0, 2.0);
- // Добавляем деления через каждые 0.5 по осям x и y
- glBegin(GL_LINES);
- // Деления по оси x
- for (double i = -2.0; i <= 2.0; i += 0.5)
- {
- glVertex2f(i, -0.025);
- glVertex2f(i, 0.025);
- }
- // Деления по оси y
- for (double i = -2.0; i <= 2.0; i += 0.5)
- {
- glVertex2f(-0.025, i);
- glVertex2f(0.025, i);
- }
- glEnd();
- glutSwapBuffers();
- }
- double area = 16 * (double)points_inside / num_points;
- cout << "Estimated area:" << fixed << setprecision(8) << area << endl;
- }
- void init(void)
- {
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glMatrixMode(GL_PROJECTION);
- gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
- }
- int main(int argc, char** argv)
- {
- // Инициализация
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(500, 500);
- glutCreateWindow("Monte-Carlo Method for Area Calculation");
- srand(time(NULL));
- glutDisplayFunc(display);
- init();
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement