Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Config
- $times = 1000000; // Lanzamientos de aguja
- $size = 30; // Tamaño de aguja
- $lines = 40; // Cantidad de líneas
- $separation = $size; // Separación entre líneas (tiene que ser igual al tamaño)
- $gridsize = $lines * $separation;
- $count = 0;
- for($i = 0; $i < $times; $i++)
- {
- // Generamos aguja
- $position = rand(0, $gridsize);
- $angle = rand(0, 90);
- // Detectamos las líneas de arriba y abajo
- $upper = floor($position / $separation) * $separation;
- $bottom = ceil($position / $separation) * $separation;
- // Borde arriba y abajo de la aguja
- $up = $position - sin(deg2rad($angle)) * $size/2;
- $down = $position + sin(deg2rad($angle)) * $size/2;
- if($up <= $upper || $down >= $bottom) // Si tocan o pasan por encima...
- $count++; // Sumamos 1 al contador
- //echo 'Pos: ', $position, ' | a: ', $angle, ' | Upper: ', $upper, ' | Bottom: ', $bottom, ' | Up: ', $up, ' | Down: ', $down, "\n"; // DEBUG
- }
- echo 'Colisiones: ', $count, '/', $times, "\n";
- // Calculemos PI
- echo 'PI = ', 2*$times/$count; // Aplicamos la fórmula
- while(true){} // Mantengamos la consola abierta
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement