#include <stdlib.h>
#include <stdio.h>
#include <math.h>
//#include "ecriture.h"
#define H 500
#define L 500
#define N 1
typedef struct {int x, y;} Point;	//Structure representant les coordonnees d'un point
///////////////////////////////////////////////////////
//Fonction d'initialisation de tableau
void init_tab (int tab[L][H]) {
     int i, j, t = 0;
     for(i=0;i<L;i++)
	 for(j=0;j<H;j++)
		tab[i][j]=0;
		return;
    }
//Fonction d'initialisation des points
void init_point (Point *p) {
     p->x = p->y = 0;
     }
///////////////////////////////////////////////////////
//Fonction de tracage de droite
void tracer_droite (Point p1, Point p2, int tab[L][H]) {
     int dx, dy, X, Y;
     /*Pre-traitement pour verifier les points X et Y*/
     //if (p1.x < p2.x && p2.y < p1.y)
//        {tracer_droite (p2, p1, tab);}
//                       else {
     printf("Debut tracer_droite\n") ;
     dx = p2.x - p1.x;
     dy = p2.y - p1.y;
     for (X = p1.x; X <= p2.x; X++) {
         Y = p1.y + dy *(X - p1.x)/dx;
         tab[X][Y] = 255;
         printf("(%d,%d)\n", X, Y) ; 
     }
     printf("Fin tracer_droite\n") ;
}       
//}  
///////////////////////////////////////////////////////
void koch (Point a, Point b, int n, int tab[L][H])	//2 points et nombre d'iterations n en argument de la fonction
	{
	Point c, e, d;		//Points suivants du segments
	int i, j, dx, dy;
	float sin60, cos60;
	printf("DEBUG\n") ;
    //Initialise les parametres
    sin60 = sqrt(3)/2;
    cos60 = 0.5;

	printf("DEBUG2\n") ;
////////////////////////////////////////
if (n==0) {
   //tab[a.x][a.y] = 255;
   //tab[b.x][b.y] = 255;
   tracer_droite(a, b, tab);
   printf("DEBUG3\n") ;
}
    else {
   printf("DEBUG4\n") ;
   c.x=(2*a.x+b.x)/3;
   c.y=(2*a.y+b.y)/3;
   d.x=(2*b.x+a.x)/3;
   d .y=(2*b.y+a.y)/3;
   dx=d.x-c.x;
   dy=d.y-c.y;
   e.x=c.x+cos60*dx-cos60*dy; printf("%d\n", e.x) ;
   e.y=c.y+sin60*dx+cos60*dy; printf("%d\n", e.y) ;
   printf("DEBUG5\n") ;
   
   tab[a.x][a.y] = 255;printf("a.x=%d, a.y=%d\n",a.x,a.y) ; 
   tab[b.x][b.y] = 255;printf("b.x=%d, b.y=%d\n",b.x,b.y) ;
   tab[c.x][c.y] = 255;printf("c.x=%d, c.y=%d\n",c.x,c.y) ;
   tab[d.x][d.y] = 255;printf("d.x=%d, d.y=%d\n",d.x,d.y) ;
   tab[e.x][e.y] = 255;printf("e.x=%d, e.y=%d\n",e.x,e.y) ;
   printf("DEBUG6\n") ;
    koch(a, c, n-1, tab);
	koch(c, e, n-1, tab);
	koch(e, d, n-1, tab);
	koch(d, b, n-1, tab);
   

}

printf("ITERATION n =%d\nA (%d, %d)\nB (%d, %d)\nC(%d, %d)\nD(%d, %d)\nE(%d, %d) \n", n, a.x, a.y, b.x, b.y, c.x, c.y, d.x, d.y,e.x, e.y);

///////Sortie en fichier ppm
FILE *f = fopen( "droite.ppm", "w" ); /* ouvre en ecriture */
fprintf(f,"P3\n");
fprintf(f,"500 500\n");
fprintf(f,"255\n");
for (j=H-1;j>=0;j--)
	{for(i=0;i<L;i++)
	  {
		fprintf(f,"%4d %4d %4d", tab[i][j], tab[i][j], tab[i][j]);
		
	   }    fprintf(f,"\n");
		fprintf(f,"\n");}
		
  fclose(f);  /* ferme le fichier */

}

////////////////////////////// Main //////////////////////////
int main(void)
{   
 
//int t = 0;     
int tab[L][H];      
init_tab (tab);

Point a = {100 , 200};
Point b = {400 , 200};
//Point p1 = {100 , 200};
//Point p2 = {400 , 200};

koch (b, a, N, tab);
//t++;

printf("Fichier ppm cree !!!!! \n");    /* affiche */
system("pause");
return 0;
}