Italiano
HOME | PROGETTI | CLASSI C++ | Sorgenti c\c++ | Galleria | Guestbook | Server BW | Contattami | Login

<< Curva di Bezier >>

visiste: 2864


Implementazione della curva di Bezier.
E' possibile scegliere arbitrariamente il grado della curva, basta solo specificare la posizione dei punti e la variabile "grado_curva".
Nella foto di esempio č rappresentata una curva di Bezier con 10 gradi.





  1. //*************************************//
  2. //******** created by mamo139 *********//
  3. //*** http://mamo139.altervista.org ***//
  4. //*************************************//
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <math.h>
  10.  
  11.  
  12. //*** strumenti gestione file bmp ***//
  13. struct bmpfile {
  14. long x;
  15. long y;
  16. char *** matrice;
  17. };
  18. char *** crea_bmp(char *nome, long x, long y);
  19. struct bmpfile carica_bmp(char *nome);
  20. void disegna_bmp(char *nome, char *** array, long x, long y);
  21.  
  22. //************************//
  23. struct punto2d {
  24. double x;
  25. double y;
  26. };
  27. long combinazione(long n,long i);
  28. long fattoriale(long x);
  29.  
  30. //*** MAIN ***//
  31. int main (void){
  32.  
  33. char img_name[]="out.bmp";
  34. long x = 1000;
  35. long y = 1000;
  36. long grado_curva = 10;
  37. double numero_punti = 30000;
  38. struct punto2d * punti;
  39. punti = (struct punto2d *) malloc((grado_curva+1) * sizeof(struct punto2d));
  40. punti[0].x=50;
  41. punti[0].y=50;
  42. punti[1].x=900;
  43. punti[1].y=100;
  44. punti[2].x=900;
  45. punti[2].y=900;
  46. punti[3].x=100;
  47. punti[3].y=900;
  48. punti[4].x=100;
  49. punti[4].y=800;
  50. punti[5].x=100;
  51. punti[5].y=300;
  52. punti[6].x=100;
  53. punti[6].y=200;
  54. punti[7].x=100;
  55. punti[7].y=100;
  56. punti[8].x=100;
  57. punti[8].y=50;
  58. punti[9].x=500;
  59. punti[9].y=100;
  60. punti[10].x=900;
  61. punti[10].y=900;
  62.  
  63. char *** mappa = crea_bmp(img_name,x,y);
  64. double t = 0;
  65. long i;
  66. double spread = 1/numero_punti;
  67. double comb,pow1,pow2;
  68. struct punto2d b;
  69.  
  70.  
  71. for(t=0;t<=1;t=t+spread){
  72. b.x=0;
  73. b.y=0;
  74. for(i=0;i<=grado_curva;i++){
  75. comb = combinazione(grado_curva,i);
  76. pow1 = pow(t,i);
  77. pow2 = pow((1-t),(grado_curva-i));
  78. b.x = b.x + comb*punti[i].x*pow1*pow2;
  79. b.y = b.y + comb*punti[i].y*pow1*pow2;
  80. }
  81. if(b.x>0&&b.x<x&&b.y>0&&b.y<y)
  82. mappa[(int)b.x][(int)b.y][2] = 255;
  83. }
  84.  
  85. for(i=0;i<=grado_curva;i++){//coloro i punti base
  86. mappa[(int)punti[i].x][(int)punti[i].y-1][1] = 255;
  87. mappa[(int)punti[i].x][(int)punti[i].y+1][1] = 255;
  88. mappa[(int)punti[i].x-1][(int)punti[i].y][1] = 255;
  89. mappa[(int)punti[i].x+1][(int)punti[i].y][1] = 255;
  90. mappa[(int)punti[i].x][(int)punti[i].y][1] = 255;
  91. }
  92.  
  93. disegna_bmp(img_name,mappa,x,y);
  94.  
  95. return 0;
  96. }
  97.  
  98. long combinazione(long n,long i){
  99. long result = fattoriale(n)/(fattoriale(n-i)*fattoriale(i));
  100. return result;
  101. }
  102. long fattoriale(long x){
  103. long result=1;
  104. while(x>0){
  105. result = result * x;
  106. x--;
  107. }
  108. return result;
  109. }
  110.  
  111.  
  112.  
  113. //****** FUNZIONI *******//
  114. char *** crea_bmp(char *nome, long x, long y){
  115.  
  116. char buffer [55] = "\x42\x4d\x36\x39\xb1\x00\x00\x00\x00\x00" \
  117. "\x36\x00\x00\x00\x28\x00\x00\x00\xe0\x08\x00\x00\xa8\x06" \
  118. "\x00\x00\x01\x00\x18\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
  119. "\xc4\x0e\x00\x00\xc4\x0e\x00\x00\x00\x00\x00\x00\x00\x00" \
  120. "\x00\x00";
  121.  
  122. buffer[18] = x%256;//asse x
  123. buffer[19] = x/256;
  124. buffer[22] = y%256;//asse y
  125. buffer[23] = y/256;
  126.  
  127. FILE *stream;
  128. stream = fopen(nome,"wb");
  129. fwrite(buffer, 1, 54, stream);
  130. fclose(stream);
  131.  
  132. char *** array;
  133. int i, j, h;
  134. array = (char***)malloc(x * sizeof(char *));
  135. for(i=0; i<x; i++) {
  136. array[i] = (char**)malloc(y * sizeof(char *));
  137. for (j=0; j<y; j++)
  138. array[i][j] = (char*)malloc(3 * sizeof(char *));
  139. }
  140.  
  141. int i1,i2,i3;
  142. for(i1=0; i1<x; i1++)
  143. for (i2=0; i2<y; i2++)
  144. for (i3=0; i3<3; i3++)
  145. array[i1][i2][i3] = 0;
  146.  
  147. return array;
  148. }
  149.  
  150. struct bmpfile carica_bmp(char *nome){
  151. struct bmpfile dati;
  152. long filesize,b,i,j,z=0;
  153. char *** array;
  154. char * buffer;
  155. FILE * file;
  156.  
  157. file = fopen(nome,"rb");
  158. fseek (file , 0 , SEEK_END);
  159. filesize = ftell(file);
  160. rewind(file);
  161.  
  162. buffer = (char *) malloc(filesize * sizeof(char *));
  163.  
  164. b = fread( buffer, 1, 54, file);
  165.  
  166. dati.x = ((unsigned char)buffer[18])+((unsigned char)buffer[19])*256;
  167. dati.y = ((unsigned char)buffer[22])+((unsigned char)buffer[23])*256;
  168.  
  169.  
  170. b = fread( buffer, 1, filesize, file);
  171.  
  172.  
  173.  
  174. array = (char***)malloc(dati.x * sizeof(char *));
  175. for(i=0; i<dati.x; i++) {
  176. array[i] = (char**)malloc(dati.y * sizeof(char *));
  177. for (j=0; j<dati.y; j++)
  178. array[i][j] = (char*)malloc(3 * sizeof(char *));
  179. }
  180. for(i=0; i<dati.x; i++)
  181. for (j=0; j<dati.y; j++){
  182. array[i][j][0]=buffer[z];
  183. array[i][j][1]=buffer[z+1];
  184. array[i][j][2]=buffer[z+2];
  185. z=z+3;
  186. }
  187.  
  188.  
  189. dati.matrice = array;
  190.  
  191. return dati;
  192. }
  193.  
  194. void disegna_bmp(char *nome, char *** array, long x, long y){
  195.  
  196. FILE *stream;
  197. stream = fopen(nome,"ab");
  198.  
  199. int i1,i2;
  200. for(i1=0; i1<x; i1++)
  201. for (i2=0; i2<y; i2++)
  202. fwrite(array[i1][i2], 1, 3, stream);
  203.  
  204. fclose(stream);
  205. }
  206.  
Versione sito: 1.03.01 by mamo139. - Tutti i sorgenti presenti su questo sito sono rilasciati sotto licenza "GNU GPL version 3".