-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
137 lines (120 loc) · 3.23 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <SFML/Graphics.hpp>
#include "mpreal.h"
#include <iostream>
#ifdef _WIN32
# include <windows.h>
#elif defined(__unix__)
# include <unistd.h>
#endif
#define DEFAULT_WINDOWS_SIZE 800
#define DEFAULT_PRECISION 8
#define DEFAULT_NUM_OF_POINTS 1024
unsigned int get_numeric_input(unsigned int default_value);
bool is_interactive();
int main()
{
unsigned int window_size = DEFAULT_WINDOWS_SIZE;
bool interactive = is_interactive();
if (interactive) std::cout << "Precision: ";
unsigned int digits = get_numeric_input(DEFAULT_PRECISION);
if (interactive) std::cout << "Number of points: ";
unsigned int number_of_points
= get_numeric_input(DEFAULT_NUM_OF_POINTS);
mpfr::mpreal::set_default_prec(mpfr::digits2bits(digits));
sf::ContextSettings settings;
settings.antialiasingLevel = 16;
sf::RenderWindow app(sf::VideoMode(window_size, window_size), "MC",
sf::Style::Titlebar | sf::Style::Close, settings);
float radius = window_size/2.0f;
sf::CircleShape ring(radius - 1.f);
ring.setPosition(1.f, 1.f);
ring.setPointCount(1000);
ring.setFillColor(sf::Color::Black);
ring.setOutlineColor(sf::Color::White);
ring.setOutlineThickness(1.f);
sf::Image buffer;
buffer.create(window_size, window_size, sf::Color::Black);
buffer.createMaskFromColor(sf::Color::Black);
sf::Texture bufferTexture;
bufferTexture.loadFromImage(buffer);
sf::Sprite bufferSprite;
bufferSprite.setTexture(bufferTexture);
std::cout.precision(digits);
mpfr::mpreal x, y;
unsigned int screen_x, screen_y;
mpfr::mpreal distance;
unsigned int in_points = 0;
unsigned int out_points = 0;
bool result_printed = false;
sf::Color color;
while (app.isOpen())
{
sf::Event event;
while (app.pollEvent(event))
{
if (event.type == sf::Event::Closed) app.close();
}
if (number_of_points > 0)
{
x = mpfr::random();
y = mpfr::random();
distance = mpfr::pow(x - 0.5, 2) + mpfr::pow(y - 0.5, 2);
screen_x = static_cast<unsigned>(
(x * window_size).toULong());
screen_y = static_cast<unsigned>(
(y * window_size).toULong());
if (distance > 0.25f)
{
++out_points;
color = sf::Color::Red;
}
else
{
++in_points;
color = sf::Color::Green;
}
--number_of_points;
buffer.setPixel(screen_x, screen_y, color);
bufferTexture.update(buffer);
app.clear();
app.draw(ring);
app.draw(bufferSprite);
app.display();
}
else if (number_of_points == 0 && result_printed == false)
{
if (interactive) std::cout
<< "----------------------" << std::endl;
std::cout << "In: " << in_points << std::endl;
std::cout << "Out: " << out_points << std::endl;
mpfr::mpreal sum = in_points + out_points;
std::cout << "Pi: " << 4.f*in_points / sum << std::endl;
result_printed = true;
}
}
return 0;
}
unsigned int get_numeric_input(unsigned int default_value)
{
unsigned int temp;
if (!(std::cin >> temp))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
return default_value;
}
return temp;
}
bool is_interactive()
{
#ifdef _WIN32
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
DWORD type = GetFileType(hIn);
return (type == FILE_TYPE_CHAR) ? true : false;
#elif defined(__unix__)
return (isatty(fileno(stdin))) ? true : false;
#else
return true;
#endif
}