// By: Xitij Shukla xitij1983@rediffmail.com
// Program to find the value of PI by populating random points in the circle
// inscribed in the square.

#include <stdio.h>
#include <stdlib.h>

struct coordinate       //intial coordinates
{
	float x;
	float y;
	};
struct coordinate centre; //centre of the system
struct coordinate point;  //any arbitrary point
unsigned long int num_circle=0; //initialization of points in the circle


     void detect_centre(float c)  //function to detect the centre
		{
		centre.x = c/2;
		centre.y = c/2;

		printf("\nThe Centre is (%f,%f)",centre.x,centre.y);
		}

      void in_circle(struct coordinate point_circ,int len_circ) //determine whether each random point is contained in the number or no
	{
		if((point_circ.x*point_circ.x)+(point_circ.y*point_circ.y)<=(len_circ*len_circ)) //Circle's formula x^2+y^2=r^2
		num_circle+=1;
	}

	void populate(unsigned long int n_pop, int len_pop) //popualte points randomly within circle
	{
		unsigned long int i;
		for(i=1;i<=n_pop;++i)
		    {
		       point.x=	rand() % len_pop;
		       point.y= rand() % len_pop;
			in_circle(point,len_pop);
		       }
		       printf("\nTotal Number of points populated is %ld",n_pop);
		       printf("\nNumber of points inside the circle is %ld ",num_circle);
		       printf("\nNumber of points ouside the circle is %ld ",n_pop-num_circle);
	}

	 void cal_PI(unsigned long int num_circ,unsigned long int total) //calculate pi=4*(number of points fallen in the circle/total number of points)
		{
			float PI;
			PI=4*(float)num_circ/(float)total;
			printf("\nThe Value of \343 = %f\n ",PI);
		}

	void main(void)
	{
		int length=100; //length and accordingly radius determined in advance
		unsigned long int n;

		printf("Enter the number of points you want to put :");
		scanf("%ld", &n);

		printf("Length of the square l=%d",length);
		printf("\nRadius of the circle inside the square r=%d",length);

		detect_centre(length);
		populate(n,length);
		cal_PI(num_circle,n);

		exit(0);
		}
