Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication1.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <cmath>
- #include <random>
- #include <chrono>
- #include <iostream>
- using namespace std;
- class Point2D
- {
- private:
- float x_co;
- float y_co;
- public:
- Point2D(float x = 0, float y = 0) : x_co{ x }, y_co{ y }
- {}
- float get_x() const { return x_co; }
- float get_y() const { return y_co; }
- };
- class Circle{
- Point2D m_middle;
- float r;
- public:
- Circle(const Point2D& middle = Point2D(), float rr = 1) : m_middle{ middle }, r{ rr }
- {}
- bool is_inside(const Point2D& p){
- return sqrt((m_middle.get_x() - p.get_x())*(m_middle.get_x() - p.get_x()) + (m_middle.get_y() - p.get_y())*(m_middle.get_y() - p.get_y())) < r;
- }
- };
- int main(){
- mt19937 gen{ (unsigned int)chrono::system_clock::now().time_since_epoch().count() };
- uniform_real_distribution<double> distribution{ -1.0, 1.0 };
- const int n = 1e6;
- float ni = 0.0;
- float pole_kwadratu = 4;
- Circle carlo;
- for (int i = 0; i < n; ++i) {
- Point2D k(distribution(gen), distribution(gen));
- if (carlo.is_inside(k) == true){
- ni++;
- }
- }
- cout << (ni / n)*pole_kwadratu << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement