Advertisement
apexsquirt

[C++ / GrAPic] Cool fractal

Feb 3rd, 2020
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. // you shall download grapic, open grapic-[whatever version it is]/build/[OS]/grapic.workspace in code::blocks and copy-paste this code in some .cpp from the workspace. ain't gonna do this for y'all every time... xD
  2.  
  3. #include <Grapic.h>
  4. #include <math.h>
  5. #include <iostream>
  6. #include <windows.h>
  7.  
  8. // si vous vous demandez pourquoi y a tout ça là :
  9. const float pi = 3.141592653;
  10. struct complex { float x, y; };
  11. complex C (float x, float y) { struct complex ret; ret.x = x; ret.y = y; return ret; }
  12. complex C_exp (float rho, float theta) { complex ret; ret.x = rho*cos(theta); ret.y = rho*sin(theta); return ret; }
  13. complex operator+ (complex a, complex b) { complex ret; ret.x = a.x + b.x; ret.y = a.y + b.y; return ret; }
  14. complex operator- (complex a, complex b) { complex ret; ret.x = a.x - b.x; ret.y = a.y - b.y; return ret; }
  15. complex operator* (float a, complex b) { complex ret; ret.x = a * b.x; ret.y = a * b.y; return ret; }
  16. complex operator* (complex a, complex b) { complex ret; ret.x = a.x * b.x - a.y * b.y; ret.y = a.x * b.y + a.y * b.x; return ret; }
  17. complex scale (complex p, float x, float y, float lambda) { return lambda*(p-C(x,y)); }
  18. complex translate (complex P, float dx, float dy) { complex ret; ret = P+C(dx,dy); return ret; }
  19. complex rotate (complex p, float x, float y, float theta) { return (p-C(x,y))*C_exp(1,theta)+C(x,y); }
  20. void show (complex z) { std::cout << z.x << " + " << z.y << " i"; }
  21. float abs (complex z) { return sqrt(z.x*z.x + z.y*z.y); }
  22. float arg (complex z) { if (z.x > 0 or z.y != 0) return 2*atan(z.y/(z.x+abs(z))); else -pi; }
  23. complex operator/ (complex a, complex b) { complex ret; ret.x = b.x/abs(b); ret.y = b.y/abs(b); return a*ret; }
  24. // c'est du bout d'code que j'ai mis dans un .h et qu'j'ai include après, du coup j'ai juste copié-collé l'contenu du header ^^'
  25.  
  26. using namespace grapic;
  27. const int DIMW = 1000;
  28. const int MAX = 1000;
  29.  
  30. int f (float x) {
  31.     return DIMW*(x+1)/2;
  32. }
  33. float f_inv (float x) {
  34.     return 3*(x-DIMW/2)/DIMW;
  35. }
  36.  
  37. complex exp (complex z) { return C(z.x,0)*C(cos(z.y),sin(z.y)); }
  38. complex cos (complex z) { return (exp(C(0,1)*z)+exp(C(0,-1)*z))*C(0.5,0); }
  39.  
  40. complex heavisign (complex z) {
  41.     return (cos(C(pi,0)*z)+C(1,0))*C(0.5,0);
  42. }
  43.  
  44. int julia (complex z, complex c) {
  45.     complex julia = z-C(2,0);
  46.     complex Max_J = julia;
  47.     int Max = 0;
  48.     for (int i = 1; i < 50; i++) {
  49.         julia = C(0.5,0)*(C(3,0)*julia+C(1,0))*heavisign(julia+C(1,0))+C(0.5,0)*julia*heavisign(julia);
  50.         if (abs(julia) > abs(Max_J)) {
  51.             Max_J = julia;
  52.             Max = i;
  53.         }
  54.     }
  55.     return Max;
  56. }
  57.  
  58. void bruh (int a, char b) {
  59.     if (a > 0) for (int i = 0; i < a; i++) printf("%c",b);
  60. }
  61.  
  62. void percentage (float x) {
  63.     bruh((x/DIMW*10+1),'\xDB');
  64.     bruh(((1-x/DIMW)*10-1),'\xB0');
  65.     printf("\r");
  66. }
  67.  
  68. float r (int n) { return std::max(255/3-(255-n),0)*3; }
  69. float g (int n) { return std::max(255/3-abs((255-n)-255/3),0)*3; }
  70. float b (int n) { return std::max(255/3-abs((255-n)-2*255/3),0)*3; }
  71.  
  72. int main (int, char**) {
  73.     bool stop = false, s = false;
  74.     winInit("julia",DIMW,DIMW);
  75.     backgroundColor(0,0,0);
  76.     int i,j;
  77.     int x,y;
  78.     complex z = C(0.2,0.6);
  79.     float z1,z2=0;
  80.     while (!stop) {
  81.         system("cls");
  82.         winClear();
  83.         z2 = 0;
  84.         for (i = 0; i < DIMW; i += 2) {
  85.             for (j = 0; j < DIMW; j += 2) {
  86.                 if (julia(C(f_inv(i),f_inv(j)),z) > z2) { z2 = julia(C(f_inv(i),f_inv(j)),z); }
  87.             }
  88.             percentage(i);
  89.         }
  90.         printf("\n");
  91.         for (i = 0; i < DIMW; i++) {
  92.             for (j = 0; j < DIMW; j++) {
  93.                 z1 = julia(C(f_inv(i),f_inv(j)),z);
  94.                 put_pixel(i,j,r(z1*255/z2),g(z1*255/z2),b(z1*255/z2));
  95.             }
  96.             percentage(i);
  97.         }
  98.         system("cls");
  99.         s = false;
  100.         while (!s) {
  101.             s = winDisplay();
  102.             if (isKeyPressed(SDLK_SPACE)) {
  103.                 s = true;
  104.             }
  105.         }
  106.         pressSpace();
  107.         printf("Re(z) = "); std::cin >> z.x;
  108.         printf("Im(z) = "); std::cin >> z.y;
  109.         stop = winDisplay();
  110.     }
  111.     winQuit();
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement