/*
<title>Gasket Program</title>
<pre>
*/
#include <math.h>
#include <stdio.h>

#define LEFTMARGIN 180.
#define WIDTH 5760.
#define BASEMARGIN 1400.

main()			/* Sierpinski Gasket */
{
	float a1[3], a2[3], x1, x2;
	int i, k, max;
	long r;
	
	
	max=50000 ; 	/* max is the number of points; make this larger to see a clearer picture. 
				   (50,000  points gives a very nice picture, but it may take a long time.) */

	a1[0]=0.0; 	/* The points (a1[i],a2[i]) (i=0,1,or 2) are the three vertices of triangle. */ 
	a2[0]=0.0;
	a1[1]=1.0;
	a2[1]=0.0;
	a1[2]=0.5;
	a2[2]=sqrt(3.0)*0.5;

				/*  (x1,x2) is the starting point: change this to see 
				    that the picture is independent of x. */
	x1=0.5;
	x2=0.5;
				/* Some Postscript stuff. */
	printf("%%!PS-Adobe-2.0\n");
	printf("%%%%BoundingBox: 0 0 612 792\n");
	printf("%%%%Pages: 1\n");
	printf("%%%%EndComments\n");
	printf("%%%%Page: 1 1\n");
	printf("/dot {2 0 360 arc fill} def\n .1 .1 scale\n");

	for(k=0;k<max;k++)
	{
 				/* The following finds a pseudorandom vertex (ie, chooses 1, 2, or 3).
				   Since there is no "seed," this will give the same picture
				   every time. You must use something other than the rand
				   function to get a different picture each time. */

		r=rand();
		if (r<	(pow(2,31)-1)/3)
	    	    i=0;
		else if (r> 2*(pow(2,31)-1)/3)
		    i=2;
		else
		    i=1;

		x1=fabs(0.5*(a1[i]+x1));	/* This goes halfway to the chosen vertex. */
		x2=fabs(0.5*(a2[i]+x2));

		/* printf("%f %f \n", x1,x2);*/	/* This prints the new point. */
		printf("%d %d dot\n", (int)(x1*WIDTH + LEFTMARGIN), (int)(x2*WIDTH + BASEMARGIN));

	}
	printf("showpage\n");
	printf("%%%%Trailer\n");
}
/*
<pre>
*/


