display.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "display.h"
  2. void mandelbrot_graph(char *filename, int *pixels, unsigned width, unsigned height)
  3. {
  4. FILE *myfile;
  5. myfile = fopen(filename, "w");
  6. fprintf(myfile, "P3\n%u %u\n255\n", width, height);
  7. unsigned i,j;
  8. for (i = 0; i < height; i++){
  9. for (j = 0; j < width; j++){
  10. fprintf(myfile, "%d 0 0 ", pixels[j + i*width]);
  11. }
  12. fprintf(myfile, "\n");
  13. }
  14. fclose(myfile);
  15. }
  16. void mandelbrot_graph_transpose(char *filename, int64_t *pixels, unsigned width, unsigned height)
  17. {
  18. FILE *myfile;
  19. myfile = fopen(filename, "w");
  20. fprintf(myfile, "P3\n%u %u\n255\n", width, height);
  21. unsigned i,j;
  22. for (i = 0; i < width; i++){
  23. for (j = 0; j < height; j++){
  24. fprintf(myfile, "%d 0 0 ", pixels[i + j*width]);
  25. }
  26. fprintf(myfile, "\n");
  27. }
  28. fclose(myfile);
  29. }
  30. void pixels_print(int *pixels, unsigned width, unsigned height)
  31. {
  32. unsigned i,j;
  33. for (i = 0; i < height; i++){
  34. for (j = 0; j < width; j++){
  35. printf("%d ", pixels[j + i * width]);
  36. }
  37. printf("\n");
  38. }
  39. }
  40. ////////////////////////// NBODY ////////////////////////
  41. static int is_inside(struct Position p, unsigned width, unsigned height);
  42. static void get_planet_pixels(struct Position *pix, struct Position p);
  43. static void graph_pixels(struct Pixel *pixel, unsigned width, unsigned height, struct Position *pix, unsigned index, unsigned nbr_planets);
  44. static void nbody_ppm(char *filename, struct Pixel *pixels, unsigned width, unsigned height);
  45. static int is_inside(struct Position p, unsigned width, unsigned height)
  46. {
  47. if (p.x >= 0 && p.x < width && p.y >= 0 && p.y < height)
  48. return 1;
  49. else
  50. return 0;
  51. }
  52. void nbody_print(double *array, unsigned nbr_planets)
  53. {
  54. unsigned i,j;
  55. for (j = 0; j < nbr_planets; j++){
  56. printf("Planet %u:\n", j);
  57. printf("%f ; %f\n", array[j], array[j + nbr_planets]);
  58. }
  59. }
  60. //Returns a circle centered on the position p.
  61. static void get_planet_pixels(struct Position *pix, struct Position p)
  62. {
  63. int i,j,k;
  64. k = 0;
  65. for (i = p.x - 1; i <= p.x + 1; i++){
  66. for (j = p.y - 3; j <= p.y + 3; j++){
  67. pix[k].x = i;
  68. pix[k].y = j;
  69. k++;
  70. }
  71. }
  72. for (j = p.y - 2; j <= p.y + 2; j++){
  73. pix[k].x = p.x - 2;
  74. pix[k].y = j;
  75. k++;
  76. pix[k].x = p.x + 2;
  77. pix[k].y = j;
  78. k++;
  79. }
  80. for (j = p.y - 1; j <= p.y + 1; j++){
  81. pix[k].x = p.x - 3;
  82. pix[k].y = j;
  83. k++;
  84. pix[k].x = p.x + 3;
  85. pix[k].y = j;
  86. k++;
  87. }
  88. }
  89. static void graph_pixels(struct Pixel *pixels, unsigned width, unsigned height, struct Position *pix, unsigned index, unsigned nbr_planets)
  90. {
  91. /* printf("Planet: %u\n", index); */
  92. unsigned j;
  93. struct Pixel pixel = {0,0,0};
  94. for (j = 0; j < 37; j++){
  95. /* printf("X: %d, Y: %d\n", pix[j].x, pix[j].y); */
  96. if ( is_inside(pix[j], width, height) ){
  97. pixel.r = 125;
  98. pixel.b = round((255. * index) / nbr_planets);
  99. pixels[pix[j].x + pix[j].y * width] = pixel;
  100. }
  101. }
  102. }
  103. static void nbody_ppm(char *filename, struct Pixel *pixels, unsigned width, unsigned height)
  104. {
  105. unsigned i,j;
  106. FILE *myfile;
  107. myfile = fopen(filename, "w");
  108. fprintf(myfile, "P3\n%u %u\n255\n", width, height);
  109. for (i = 0; i < height; i++){
  110. for (j = 0; j < width; j++){
  111. struct Pixel pixel = pixels[j + i * width];
  112. fprintf(myfile, "%u %u %u ", pixel.r, pixel.g, pixel.b);
  113. }
  114. fprintf(myfile, "\n");
  115. }
  116. fclose(myfile);
  117. }
  118. void nbody_graph(char *filename, double *positions, unsigned nbr_planets, unsigned width, unsigned height, double min_val, double max_val)
  119. {
  120. struct Position *pix = malloc(37 * sizeof(struct Position));
  121. struct Pixel *pixels = calloc(width * height, sizeof(struct Pixel));
  122. unsigned i,j;
  123. for (i = 0; i < nbr_planets; i++){
  124. struct Position posi;
  125. posi.x = round( (positions[i] - min_val) / (max_val - min_val) * (width - 1));
  126. posi.y = round( (positions[i + nbr_planets] - min_val) / (max_val - min_val) * (width - 1));
  127. get_planet_pixels(pix, posi);
  128. graph_pixels(pixels, width, height, pix, i, nbr_planets);
  129. }
  130. nbody_ppm(filename, pixels, width, height);
  131. }
  132. void nbody_graph_transpose(char *filename, double *positions, unsigned nbr_planets, unsigned width, unsigned height, double min_val, double max_val)
  133. {
  134. struct Position *pix = malloc(37 * sizeof(struct Position));
  135. struct Pixel *pixels = calloc(width * height, sizeof(struct Pixel));
  136. unsigned i,j;
  137. for (i = 0; i < nbr_planets; i++){
  138. struct Position posi;
  139. posi.x = round( (positions[2 * i] - min_val) / (max_val - min_val) * (width - 1));
  140. posi.y = round( (positions[2 * i + 1] - min_val) / (max_val - min_val) * (width - 1));
  141. get_planet_pixels(pix, posi);
  142. graph_pixels(pixels, width, height, pix, i, nbr_planets);
  143. }
  144. nbody_ppm(filename, pixels, width, height);
  145. }