Advertisement
Guest User

Untitled

a guest
Apr 15th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3.  
  4. bool left(double x1, double y1, double x2, double y2, double x, double y){
  5.     double vx,vy,wx,wy;
  6.     vx = x2-x1; vy = y2-y1;
  7.     wx = x-x1; wy = y-y1;
  8.     return (vx*wy-wx*vy)>0;
  9. }
  10.  
  11. bool insidetriangle(double x1, double y1, double x2, double y2, double x3, double y3, double x, double y){
  12.     return (left(x1,y1,x2,y2,x,y) && left(x2,y2,x3,y3,x,y) && left(x3,y3,x1,y1,x,y)) ||
  13.            (!left(x1,y1,x2,y2,x,y) && !left(x2,y2,x3,y3,x,y) && !left(x3,y3,x1,y1,x,y));
  14. }
  15.  
  16.  
  17. int main() {
  18.     std::random_device rd;
  19.     std::mt19937 gen(rd());
  20.     std::uniform_real_distribution<> dis(0, 1);
  21.     double random_number = dis(gen);
  22.     int K = 0;
  23.     int N = 10000000;
  24.     for (int i = 0; i < N ; ++i) {
  25.         double x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6;
  26.         x1 = dis(gen);
  27.         x2 = dis(gen);
  28.         x3 = dis(gen);
  29.         x4 = dis(gen);
  30.         x5 = dis(gen);
  31.         x6 = dis(gen);
  32.         y1 = dis(gen);
  33.         y2 = dis(gen);
  34.         y3 = dis(gen);
  35.         y4 = dis(gen);
  36.         y5 = dis(gen);
  37.         y6 = dis(gen);
  38.         if (insidetriangle(x1,y1,x2,y2,x3,y3,x4,y4) &&
  39.             insidetriangle(x1,y1,x2,y2,x3,y3,x5,y5) &&
  40.             insidetriangle(x1,y1,x2,y2,x3,y3,y6,y6))
  41.             K++;
  42.     }
  43.     std::cout << (K*1.0)/(N*1.0) << std::endl;
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement