Chris_M_Thomasson

Rendering Correction...

Jan 24th, 2019
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. /*
  2. Chris M. Thomasson's Hybrid Reverse 2-ary Julia
  3.  
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *_____________________________________________________________*/
  17.  
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <assert.h>
  22. #include <complex.h>
  23. #include <tgmath.h>
  24. #include <stdbool.h>
  25.  
  26.  
  27. #define CT_RAND() (rand() / (RAND_MAX - 0.0))
  28.  
  29.  
  30. struct ct_axes
  31. {
  32. double xmin;
  33. double xmax;
  34. double ymin;
  35. double ymax;
  36. };
  37.  
  38.  
  39. struct ct_canvas
  40. {
  41. unsigned long width;
  42. unsigned long height;
  43. unsigned char* buf;
  44. };
  45.  
  46. bool
  47. ct_canvas_create(
  48. struct ct_canvas* const self,
  49. unsigned long width,
  50. unsigned long height
  51. ){
  52. size_t size = width * height;
  53.  
  54. self->buf = calloc(1, size);
  55.  
  56. if (self->buf)
  57. {
  58. self->width = width;
  59. self->height = height;
  60.  
  61. return true;
  62. }
  63.  
  64. return false;
  65. }
  66.  
  67. void
  68. ct_canvas_destroy(
  69. struct ct_canvas const* const self
  70. ){
  71. free(self->buf);
  72. }
  73.  
  74. bool
  75. ct_canvas_save_ppm(
  76. struct ct_canvas const* const self,
  77. char const* fname
  78. ){
  79. FILE* fout = fopen(fname, "w");
  80.  
  81. if (fout)
  82. {
  83. char const ppm_head[] =
  84. "P3\n"
  85. "# Chris M. Thomasson RIFC Cipher Renderer ver:0.0.0.0 (pre-alpha)";
  86.  
  87. fprintf(fout, "%s\n%lu %lu\n%u\n",
  88. ppm_head,
  89. self->width, self->height,
  90. 255U);
  91.  
  92. size_t size = self->width * self->height;
  93.  
  94. for (size_t i = 0; i < size; ++i)
  95. {
  96. unsigned int c = self->buf[i];
  97. fprintf(fout, "%u %u %u ", c, 0U, 0U);
  98. }
  99.  
  100. if (! fclose(fout))
  101. {
  102. return true;
  103. }
  104. }
  105.  
  106. return false;
  107. }
  108.  
  109.  
  110. struct ct_plane
  111. {
  112. struct ct_axes axes;
  113. struct ct_canvas* canvas;
  114. };
  115.  
  116.  
  117. bool
  118. ct_plane_plot(
  119. struct ct_plane const* const self,
  120. double complex c,
  121. unsigned char color
  122. ){
  123. double awidth = self->axes.xmax - self->axes.xmin;
  124. double aheight = self->axes.ymax - self->axes.ymin;
  125.  
  126. double xstep = awidth / (self->canvas->width - 1.0);
  127. double ystep = aheight / (self->canvas->height - 1.0);
  128.  
  129. // preserve signs!
  130. long x = (creal(c) - self->axes.xmin) / xstep;
  131. long y = (self->axes.ymax - cimag(c)) / ystep;
  132.  
  133. if (x > -1 && x < (long)self->canvas->width &&
  134. y > -1 && y < (long)self->canvas->height)
  135. {
  136. // Now, we can convert to index.
  137. size_t i = x + y * self->canvas->height;
  138.  
  139. assert(i < self->canvas->height * self->canvas->width);
  140.  
  141. self->canvas->buf[i] = color;
  142. return true;
  143. }
  144.  
  145. return false;
  146. }
  147.  
  148.  
  149. // Compute the fractal
  150. void
  151. ct_ifs(
  152. struct ct_plane* const self,
  153. double complex z,
  154. double complex c,
  155. double ratio,
  156. unsigned long n
  157. ){
  158. printf("ct_ifs: %lf%+lfi, ratio:%lf\n", creal(c), cimag(c), ratio);
  159.  
  160. // 2 sets
  161. double complex jp[] = {
  162. .0 + I*.0,
  163. -5.5 + I*.0
  164. };
  165.  
  166. for (unsigned long i = 0; i < n; ++i)
  167. {
  168. double rn0 = rand() / (RAND_MAX - .0);
  169. double rn1 = rand() / (RAND_MAX - .0);
  170.  
  171. if (rn0 > .5)
  172. {
  173. c = jp[0];
  174. }
  175.  
  176. else
  177. {
  178. c = jp[1];
  179. }
  180.  
  181.  
  182. double complex d = z - c;
  183. double complex root = csqrt(d);
  184.  
  185. z = root;
  186.  
  187. if (rn1 > ratio)
  188. {
  189. z = -root;
  190. }
  191.  
  192. ct_plane_plot(self, z, 255);
  193. ct_plane_plot(self, root, 255);
  194.  
  195. if (! (i % 256))
  196. {
  197. printf("rendering: %lu of %lu\r", i + 1, n);
  198. }
  199. }
  200.  
  201. printf("rendering: %lu of %lu\n", n, n);
  202. }
  203.  
  204.  
  205.  
  206.  
  207. // slow, so what for now... ;^)
  208. void ct_circle(
  209. struct ct_plane* const self,
  210. double complex c,
  211. double radius,
  212. unsigned int n
  213. ){
  214. double abase = 6.2831853071 / n;
  215.  
  216. for (unsigned int i = 0; i < n; ++i)
  217. {
  218. double angle = abase * i;
  219.  
  220. double complex z =
  221. (creal(c) + cos(angle) * radius) +
  222. (cimag(c) + sin(angle) * radius) * I;
  223.  
  224. ct_plane_plot(self, z, 255);
  225. }
  226. }
  227.  
  228.  
  229. #define CT_WIDTH 1024
  230. #define CT_HEIGHT 1024
  231. #define CT_N 10000000
  232.  
  233. int main(void)
  234. {
  235. struct ct_canvas canvas;
  236.  
  237. bool status = ct_canvas_create(&canvas, CT_WIDTH, CT_HEIGHT);
  238. assert(status);
  239.  
  240. double radius = 3.14;
  241. struct ct_axes axes = { -radius, radius, -radius, radius };
  242. struct ct_plane plane = { axes, &canvas };
  243.  
  244. // Julia circle at origin z and Julia point c = 0+0i
  245. double complex z = 0+0*I;
  246. double complex c = 0+0*I; // Julia
  247. double ratio = .5;
  248.  
  249. ct_ifs(&plane, z, c, ratio, CT_N);
  250.  
  251. // draw partial off screen circle
  252. ct_circle(&plane, 1+0*I, 3, 2048);
  253.  
  254. status = ct_canvas_save_ppm(&canvas, "ct_cipher_rifc.ppm");
  255. assert(status);
  256. printf("\ncreated: ct_cipher_rifc.ppm\n");
  257.  
  258. ct_canvas_destroy(&canvas);
  259.  
  260. return 0;
  261. }
Advertisement
Add Comment
Please, Sign In to add comment