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

<< Grafico funzione 2d >>

visiste: 1793


Programma per disegnare un grafico di una qualsiasi funzione bidimensionale.



  1. //*************************************//
  2. //******** created by mamo139 *********//
  3. //*** http://mamo139.altervista.org ***//
  4. //*************************************//
  5. #include <stdio.h>
  6. #include <windows.h>
  7. #include <stdlib.h>
  8. #include <conio.h>
  9. #include <math.h>
  10.  
  11. int main (void){
  12.  
  13. FILE *stream;
  14. stream = fopen("output.bmp","wb");
  15.  
  16. unsigned char buffer1[19]="\x42\x4d\x26\xec\x00\x00\x00\x00\x00\x00\x36\x00\x00\x00\x28\x00\x00\x00";
  17. unsigned char buffer2[5]="\xE8\x03\x00\x00";
  18. unsigned char buffer3[5]="\xE8\x03\x00\x00";
  19. unsigned char buffer4[28]="\x01\x00\x18\x00\x00\x00\x00\x00\xf0\xeb\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  20.  
  21.  
  22.  
  23. fwrite(buffer1,sizeof(buffer1)-1,1,stream);
  24. fwrite(buffer2,sizeof(buffer2)-1,1,stream);
  25. fwrite(buffer3,sizeof(buffer3)-1,1,stream);
  26. fwrite(buffer4,sizeof(buffer4),1,stream);
  27.  
  28. printf("header: %d bytes, scritto!\n",(sizeof(buffer1)+sizeof(buffer2)+sizeof(buffer3)+sizeof(buffer4)-3));
  29.  
  30. long x=0,end=(buffer2[0]+buffer2[1]*256)*(buffer3[0]+buffer3[1]*256);
  31. long bytes = end*3;
  32.  
  33. printf("immagine %dx%d\nbyte da scrivere: %d\npartenza ciclo %d\n",(buffer2[0]+buffer2[1]*256),(buffer3[0]+buffer3[1]*256),end*3,x);
  34.  
  35.  
  36.  
  37. long latox = (buffer2[0]+buffer2[1]*256),latoy=(buffer3[0]+buffer3[1]*256);
  38. long middlex=latox/2, middley=latoy/2;
  39.  
  40. unsigned char *mappa;
  41. mappa=(unsigned char *)malloc((end*3) * sizeof(unsigned char));
  42.  
  43. //disegna mappa bianca e poi il grafico, tarato per 1000x1000 grafico 10x10
  44. for(x=0;x<bytes;x++){//mappa bianca
  45. if( (x/3)/latox == middley || (x/3)%latox == middlex ){
  46. mappa[x]=01; mappa[x+1]=01; mappa[x+2]=255; x=x+2;
  47. }
  48. else
  49. mappa[x]=255;
  50. }
  51. printf("mappa bianca creata, grandezza: %d!!\n",x);
  52. //system("pause");
  53.  
  54. //***************************************************** CICLO DISEGNO FUNZIONE 1 ***//
  55. printf("inizio disegno funzione 1:\n");
  56. for(x=0;x<latox;x++){
  57. double graficox = (double) (x-(latox/2))/100;
  58.  
  59. //*** FUNZIONE ***//
  60. double graficoy = graficox*graficox;
  61.  
  62. long realy = (long)( (graficoy)*100 + latoy/2 );
  63. if (realy < 1000 && realy > 0 ){
  64. mappa[(latox*realy+x)*3]=00;
  65. mappa[((latox*realy+x)*3) +1]=00;
  66. mappa[((latox*realy+x)*3) +2]=00;
  67. printf("graficox:%f graficoy:%f realx:%d realy:%d scrittura:%d \n",graficox,graficoy,x,realy,((latox*realy+x)*3));
  68. }
  69. }
  70.  
  71. //****************************************************** CICLO DISEGNO FUNZIONE 2 ***//
  72. printf("inizio disegno funzione 2:\n");
  73. for(x=0;x<latox;x++){
  74. double graficox = (double) (x-(latox/2))/100;
  75.  
  76. //*** FUNZIONE ***//
  77. double graficoy = graficox*graficox*graficox;
  78.  
  79. long realy = (long)( (graficoy)*100 + latoy/2 );
  80. if (realy < 1000 && realy > 0 ){
  81. mappa[(latox*realy+x)*3]=255;
  82. mappa[((latox*realy+x)*3) +1]=00;
  83. mappa[((latox*realy+x)*3) +2]=00;
  84. printf("graficox:%f graficoy:%f realx:%d realy:%d scrittura:%d \n",graficox,graficoy,x,realy,((latox*realy+x)*3));
  85. }
  86. }
  87.  
  88. //****************************************************** CICLO DISEGNO FUNZIONE 3 ***//
  89. printf("inizio disegno funzione 3:\n");
  90. for(x=0;x<latox;x++){
  91. double graficox = (double) (x-(latox/2))/100;
  92.  
  93. //*** FUNZIONE ***//
  94. double graficoy = sin(graficox);
  95.  
  96. long realy = (long)( (graficoy)*100 + latoy/2 );
  97. if (realy < 1000 && realy > 0 ){
  98. mappa[(latox*realy+x)*3]=00;
  99. mappa[((latox*realy+x)*3) +1]=255;
  100. mappa[((latox*realy+x)*3) +2]=00;
  101. printf("graficox:%f graficoy:%f realx:%d realy:%d scrittura:%d \n",graficox,graficoy,x,realy,((latox*realy+x)*3));
  102. }
  103. }
  104. //****************************************************** CICLO DISEGNO FUNZIONE 4 ***//
  105. printf("inizio disegno funzione 4:\n");
  106. for(x=0;x<latox;x++){
  107. double graficox = (double) (x-(latox/2))/100;
  108.  
  109. //*** FUNZIONE ***//
  110. double graficoy = cos(graficox);
  111.  
  112. long realy = (long)( (graficoy)*100 + latoy/2 );
  113. if (realy < 1000 && realy > 0 ){
  114. mappa[(latox*realy+x)*3]=255;
  115. mappa[((latox*realy+x)*3) +1]=255;
  116. mappa[((latox*realy+x)*3) +2]=00;
  117. printf("graficox:%f graficoy:%f realx:%d realy:%d scrittura:%d \n",graficox,graficoy,x,realy,((latox*realy+x)*3));
  118. }
  119. }
  120.  
  121. printf("x=%d, funzione disegnata in memoria!!\n",x);
  122.  
  123. unsigned char bufff[3];
  124. for(x=0;x<bytes;x++){
  125. bufff[0]=mappa[x];
  126. bufff[1]='\0';
  127. fwrite(bufff,1,1,stream);
  128. }
  129. printf("disegno scritto!!\n");
  130.  
  131. /*
  132.   for(x=0;x<end;x++){
  133.   unsigned char buff[3];
  134.   long xc = x%(buffer2[0]+buffer2[1]*256);
  135.   long yc = x/(buffer2[0]+buffer2[1]*256);
  136.   if( (xc >= (yc-2) && xc <= (yc+2)) || (xc >= (latoy-yc-2) && xc <= (latoy-yc+2))) buff[0] = 255; else buff[0] = 0;
  137.   if(xc<middlex+2 && xc>middlex-2) buff[1] = 255; else buff[1] = 0;
  138.   if(yc<middley+2 && yc>middley-2) buff[2] = 255; else buff[2] = 0;
  139.  
  140.  
  141.   fwrite(buff,3,1,stream);
  142.   }
  143.   */
  144.  
  145. printf("byte scritti: %d",x);
  146.  
  147. fclose(stream);
  148. system("output.bmp");
  149. return 1;
  150. }
  151.  
Versione sito: 1.03.01 by mamo139. - Tutti i sorgenti presenti su questo sito sono rilasciati sotto licenza "GNU GPL version 3".