//*************************************//
//******** created by mamo139 *********//
//*** http://mamo139.altervista.org ***//
//*************************************//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
//*** strumenti gestione file bmp ***//
struct bmpfile {
long x;
long y;
char *** matrice;
};
char *** crea_bmp(char *nome, long x, long y);
struct bmpfile carica_bmp(char *nome);
void disegna_bmp(char *nome, char *** array, long x, long y);
//************************//
struct punto2d {
double x;
double y;
};
long combinazione(long n,long i);
long fattoriale(long x);
//*** MAIN ***//
int main (void){
char img_name[]="out.bmp";
long x = 1000;
long y = 1000;
long grado_curva = 10;
double numero_punti = 30000;
struct punto2d * punti;
punti = (struct punto2d *) malloc((grado_curva+1) * sizeof(struct punto2d));
punti[0].x=50;
punti[0].y=50;
punti[1].x=900;
punti[1].y=100;
punti[2].x=900;
punti[2].y=900;
punti[3].x=100;
punti[3].y=900;
punti[4].x=100;
punti[4].y=800;
punti[5].x=100;
punti[5].y=300;
punti[6].x=100;
punti[6].y=200;
punti[7].x=100;
punti[7].y=100;
punti[8].x=100;
punti[8].y=50;
punti[9].x=500;
punti[9].y=100;
punti[10].x=900;
punti[10].y=900;
char *** mappa = crea_bmp(img_name,x,y);
double t = 0;
long i;
double spread = 1/numero_punti;
double comb,pow1,pow2;
struct punto2d b;
for(t=0;t<=1;t=t+spread){
b.x=0;
b.y=0;
for(i=0;i<=grado_curva;i++){
comb = combinazione(grado_curva,i);
pow1 = pow(t,i);
pow2 = pow((1-t),(grado_curva-i));
b.x = b.x + comb*punti[i].x*pow1*pow2;
b.y = b.y + comb*punti[i].y*pow1*pow2;
}
if(b.x>0&&b.x<x&&b.y>0&&b.y<y)
mappa[(int)b.x][(int)b.y][2] = 255;
}
for(i=0;i<=grado_curva;i++){//coloro i punti base
mappa[(int)punti[i].x][(int)punti[i].y-1][1] = 255;
mappa[(int)punti[i].x][(int)punti[i].y+1][1] = 255;
mappa[(int)punti[i].x-1][(int)punti[i].y][1] = 255;
mappa[(int)punti[i].x+1][(int)punti[i].y][1] = 255;
mappa[(int)punti[i].x][(int)punti[i].y][1] = 255;
}
disegna_bmp(img_name,mappa,x,y);
return 0;
}
long combinazione(long n,long i){
long result = fattoriale(n)/(fattoriale(n-i)*fattoriale(i));
return result;
}
long fattoriale(long x){
long result=1;
while(x>0){
result = result * x;
x--;
}
return result;
}
//****** FUNZIONI *******//
char *** crea_bmp(char *nome, long x, long y){
char buffer [55] = "\x42\x4d\x36\x39\xb1\x00\x00\x00\x00\x00" \
"\x36\x00\x00\x00\x28\x00\x00\x00\xe0\x08\x00\x00\xa8\x06" \
"\x00\x00\x01\x00\x18\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
"\xc4\x0e\x00\x00\xc4\x0e\x00\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00";
buffer[18] = x%256;//asse x
buffer[19] = x/256;
buffer[22] = y%256;//asse y
buffer[23] = y/256;
FILE *stream;
stream = fopen(nome,"wb");
fwrite(buffer, 1, 54, stream);
fclose(stream);
char *** array;
int i, j, h;
array = (char***)malloc(x * sizeof(char *));
for(i=0; i<x; i++) {
array[i] = (char**)malloc(y * sizeof(char *));
for (j=0; j<y; j++)
array[i][j] = (char*)malloc(3 * sizeof(char *));
}
int i1,i2,i3;
for(i1=0; i1<x; i1++)
for (i2=0; i2<y; i2++)
for (i3=0; i3<3; i3++)
array[i1][i2][i3] = 0;
return array;
}
struct bmpfile carica_bmp(char *nome){
struct bmpfile dati;
long filesize,b,i,j,z=0;
char *** array;
char * buffer;
FILE * file;
file = fopen(nome,"rb");
fseek (file , 0 , SEEK_END);
filesize = ftell(file);
rewind(file);
buffer = (char *) malloc(filesize * sizeof(char *));
b = fread( buffer, 1, 54, file);
dati.x = ((unsigned char)buffer[18])+((unsigned char)buffer[19])*256;
dati.y = ((unsigned char)buffer[22])+((unsigned char)buffer[23])*256;
b = fread( buffer, 1, filesize, file);
array = (char***)malloc(dati.x * sizeof(char *));
for(i=0; i<dati.x; i++) {
array[i] = (char**)malloc(dati.y * sizeof(char *));
for (j=0; j<dati.y; j++)
array[i][j] = (char*)malloc(3 * sizeof(char *));
}
for(i=0; i<dati.x; i++)
for (j=0; j<dati.y; j++){
array[i][j][0]=buffer[z];
array[i][j][1]=buffer[z+1];
array[i][j][2]=buffer[z+2];
z=z+3;
}
dati.matrice = array;
return dati;
}
void disegna_bmp(char *nome, char *** array, long x, long y){
FILE *stream;
stream = fopen(nome,"ab");
int i1,i2;
for(i1=0; i1<x; i1++)
for (i2=0; i2<y; i2++)
fwrite(array[i1][i2], 1, 3, stream);
fclose(stream);
}