//We should put comments here #include #include #include "graphics.h" using namespace std; const int WINDOW_SIZE = 600; //The size of the window that we use int main() { //Variable Declarations long total_points; //The total number of points dropped in square long circle_points; //How many points fell inside the circle int x, y; //Pixel coordinates of a single point //Init graphics window initwindow(WINDOW_SIZE, WINDOW_SIZE, "Apple Pi"); //Set up loop total_points = circle_points = 0; setfillstyle(SOLID_FILL, RED); fillellipse(WINDOW_SIZE/2, WINDOW_SIZE/2, WINDOW_SIZE/2, WINDOW_SIZE/2); //Loop while(! (kbhit()) ) { //Drop a random point (2 random numbers from 0 to WINDOW_SIZE - 1) x = rand() % WINDOW_SIZE; y = rand() % WINDOW_SIZE; ++total_points; //determine if it's in the circle if (getpixel(x, y) == RED) { ++circle_points; putpixel(x, y, WHITE); delay(1000); putpixel(x, y, RED); } else { putpixel(x, y, WHITE); delay(1000); putpixel(x, y, BLACK); } //Print the value of pi (estimated) cout << "Pi estimate = " << 4.0 * circle_points / total_points << " after " << total_points << " iterations." << endl; } }