/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-  */
/*
 * main.c
 * Copyright (C) 2017 drdev <gualtieri@ieee.org>
 * 
 * Sierpinski is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Sierpinski is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

/* Prototypes */
char *strcpy(char *dest, const char *src);
void srand(unsigned seed);
double random_number(void);
void svg_circle (int x, int y, int r);
/* end of prototypes */

#define size 2000
#define ratio 0.50
#define points 50000
#define a_x (size/2)
#define a_y 0
#define b_x 0
#define b_y size
#define c_x size
#define c_y size

char fn1[64];
int i;
int choice;
double x,y;
FILE *outdata;

//generates a psuedo-random float between 0.0 and 0.999...
double random_number(void)
{
    return ((double)rand())/RAND_MAX;
}

void svg_circle (int x, int y, int r)
{
//<circle cx="50" cy="50" r="40" stroke="black" stroke-width="1" fill="black" />

printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"1\" fill=\"black\"/>\n",x,y,r);

fprintf(outdata,"<circle cx=\"%d\"  cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"1\" fill=\"black\"/>\n",x,y,r);
}

int main(int argc, char *argv[])
{
if (argc<2)
{
strcpy(fn1,"output.svg");
}
else
{
strcpy(fn1,argv[1]);
}
printf("\nOutput file selected = %s\n",fn1);

if ((outdata = fopen(fn1,"w"))==NULL)
	{printf ("\nOutput file cannot be opened.\n");
	exit (1);}
	
/* seed random number function */
srand((unsigned)time ((time_t *) NULL));

// Print svg header
printf("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" height=\"%d\" width=\"%d\">\n",size,size);
fprintf(outdata," <svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" height=\"%d\" width=\"%d\">\n",size,size);

x=size*random_number();
y=size*random_number();
	
for (i=1;i<points;i++)
{

choice = rand()%3; // vertex choice, a=0, b=1, c=2

switch(choice)
{
case 0:
{
//place point between last point and (a_x,a_y)
x=(ratio*a_x) + ((1-ratio)*x);
y=(ratio*a_y) + ((1-ratio)*y);
break;
}
case 1:
{
//place point between last point and (b_x,b_y)
x=(ratio*b_x) + ((1-ratio)*x);
y=(ratio*b_y) + ((1-ratio)*y);
break;
}
case 2:
{
//place point between last point and (c_x,c_y)
x=(ratio*c_x) + ((1-ratio)*x);
y=(ratio*c_y) + ((1-ratio)*y);
}

}

svg_circle(x,y,1);

}

// Print svg footer
printf("</svg>");
fprintf(outdata,"</svg>");

fclose(outdata);
printf("\nDone.\n");

return 0;

}
