-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
94 lines (73 loc) · 2.45 KB
/
mainwindow.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
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <random>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->lineEdit->setValidator(new QIntValidator(0, 10000000, this));
QLineSeries* series1 = new QLineSeries();
series1->setName("y1");
QLineSeries* series2 = new QLineSeries();
series2->setName("y2");
double x = -3.0;
while(x < 0.5) {
series1->append(x, 5.0*sin(0.7*x+2.0));
series2->append(x, (9.0*(x+2.0)*(x+2.0)-7.0)/10.0);
x += 0.01;
}
ui->graphicsView->chart()->addSeries(series1);
ui->graphicsView->chart()->addSeries(series2);
ui->graphicsView->chart()->createDefaultAxes();
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(calc()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::calc() {
int n = ui->lineEdit->text().toInt();
if (y1 != nullptr) {
ui->graphicsView->chart()->removeSeries(y1);
}
if (y2 != nullptr) {
ui->graphicsView->chart()->removeSeries(y2);
}
y1 = new QScatterSeries();
y1->setName("Непопавшие точки");
y1->setMarkerShape(QScatterSeries::MarkerShapeCircle);
y1->setMarkerSize(5);
y1->setBorderColor(QColorConstants::Yellow);
y2 = new QScatterSeries();
y2->setName("Попавшие точки");
y2->setMarkerShape(QScatterSeries::MarkerShapeCircle);
y2->setMarkerSize(5);
y2->setBorderColor(QColorConstants::Blue);
int inside = 0;
int every10000 = n / 100000;
for (int i = 0; i < n; i++) {
double x = -3.0 + ((double)rand() / RAND_MAX) * (0.5 - -3.0);
double y = -1.0 + ((double)rand() / RAND_MAX) * (5.0 - -1.0);
double xy1 = 5.0*sin(0.7*x+2.0);
double xy2 = (9.0*(x+2.0)*(x+2.0)-7.0)/10.0;
bool underY1 = y <= xy1;
bool aboveY2 = y >= xy2;
bool ins = underY1 && aboveY2;
if (ins) {
if (n < 500000 || (i % every10000 == 0)) {
y2->append(x, y);
}
inside++;
} else {
if (n < 500000 || (i % every10000 == 0)) {
y1->append(x, y);
}
}
}
double footprint = 6.0 * 3.5 * (double(inside) / double(n));
ui->label_5->setText(QString::number(footprint));
ui->graphicsView->chart()->addSeries(y1);
ui->graphicsView->chart()->addSeries(y2);
ui->graphicsView->chart()->createDefaultAxes();
}