Advertisement
apexsquirt

[C++ / GrAPic] Julia sets

Jan 31st, 2020
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1. #include <Grapic.h>
  2. #include <math.h>
  3. #include <iostream>
  4. #include <windows.h>
  5.  
  6. // si vous vous demandez pourquoi y a tout ça là :
  7. const float pi = 3.141592653;
  8. struct complex { float x, y; };
  9. complex C (float x, float y) { struct complex ret; ret.x = x; ret.y = y; return ret; }
  10. complex C_exp (float rho, float theta) { complex ret; ret.x = rho*cos(theta); ret.y = rho*sin(theta); return ret; }
  11. complex operator+ (complex a, complex b) { complex ret; ret.x = a.x + b.x; ret.y = a.y + b.y; return ret; }
  12. complex operator- (complex a, complex b) { complex ret; ret.x = a.x - b.x; ret.y = a.y - b.y; return ret; }
  13. complex operator* (float a, complex b) { complex ret; ret.x = a * b.x; ret.y = a * b.y; return ret; }
  14. 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; }
  15. complex scale (complex p, float x, float y, float lambda) { return lambda*(p-C(x,y)); }
  16. complex translate (complex P, float dx, float dy) { complex ret; ret = P+C(dx,dy); return ret; }
  17. complex rotate (complex p, float x, float y, float theta) { return (p-C(x,y))*C_exp(1,theta)+C(x,y); }
  18. void show (complex z) { std::cout << z.x << " + " << z.y << " i"; }
  19. float abs (complex z) { return sqrt(z.x*z.x + z.y*z.y); }
  20. float arg (complex z) { if (z.x > 0 or z.y != 0) return 2*atan(z.y/(z.x+abs(z))); else -pi; }
  21. // 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 ^^'
  22.  
  23. using namespace grapic;
  24. const int DIMW = 500;
  25. const int MAX = 500;
  26.  
  27. int f (float x) {
  28.     return DIMW*(x+1)/2;
  29. }
  30. float f_inv (float x) {
  31.     return 2*(x-DIMW/2)/DIMW;
  32. }
  33.  
  34. int julia (complex z, complex c) {
  35.     complex julia = z*z+c;
  36.     complex Max_J = julia;
  37.     int Max = 1;
  38.     for (int i = 2; i < 256; i++) {
  39.         julia = julia*julia + c;
  40.         if (abs(julia) > abs(Max_J)) {
  41.             Max_J = julia;
  42.             Max = i;
  43.         }
  44.     }
  45.     return Max;
  46. }
  47.  
  48. void bruh (int a, char b) {
  49.     if (a > 0) for (int i = 0; i < a; i++) printf("%c",b);
  50. }
  51.  
  52. void percentage (float x) {
  53.     bruh((x/DIMW*10+1),'\xDB');
  54.     bruh(((1-x/DIMW)*10-1),'\xB0');
  55.     printf("\r");
  56. }
  57.  
  58. float r (int n) { return std::max(255/3-(255-n),0)*3; }
  59. float g (int n) { return std::max(255/3-abs((255-n)-255/3),0)*3; }
  60. float b (int n) { return std::max(255/3-abs((255-n)-2*255/3),0)*3; }
  61.  
  62. int main (int, char**) {
  63.     bool stop = false;
  64.     winInit("julia",DIMW,DIMW);
  65.     backgroundColor(0,0,0);
  66.     int i,j;
  67.     int x,y;
  68.     complex z = C(0.32,0.043);
  69.     float z1,z2=0;
  70.     while (!stop) {
  71.         system("cls");
  72.         winClear();
  73.         z2 = 0;
  74.         for (i = 0; i < DIMW; i++) {
  75.             for (j = 0; j < DIMW; j++) {
  76.                 if (julia(C(f_inv(i),f_inv(j)),z) > z2) { z2 = julia(C(f_inv(i),f_inv(j)),z); }
  77.             }
  78.             percentage(i);
  79.         }
  80.         printf("\n");
  81.         for (i = 0; i < DIMW; i++) {
  82.             for (j = 0; j < DIMW; j++) {
  83.                 z1 = julia(C(f_inv(i),f_inv(j)),z);
  84.                 put_pixel(i,j,r(z1*255/z2),g(z1*255/z2),b(z1*255/z2));
  85.             }
  86.             percentage(i);
  87.         }
  88.         system("cls");
  89.         pressSpace();
  90.         printf("Re(z) = "); std::cin >> z.x;
  91.         printf("Im(z) = "); std::cin >> z.y;
  92.         stop = winDisplay();
  93.     }
  94.     winQuit();
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement