acidportal

- 😈 Worlds smallest Evil Portal on a LilyGo T-QT
git clone git://git.acid.vegas/acidportal.git
Log | Files | Refs | Archive | README | LICENSE

Julia_Set.ino (2474B)

      1 // Based on sketch here:
      2 // https://github.com/OpenHDZ/Arduino-experimentation
      3 // Adapted for TFT_eSPI library
      4 
      5 // Note: a high number of floating point calculations are needed
      6 // for each pixel so rendering will be quite slow.
      7 // For best performance use a Teensy 4.x (600MHz CPU clock).
      8 
      9 #include <TFT_eSPI.h>       // Hardware-specific library
     10 
     11 TFT_eSPI tft = TFT_eSPI();  // Invoke custom library
     12 
     13 const uint16_t MAX_ITERATION = 300; // Nombre de couleurs
     14 
     15 #define SCREEN_WIDTH  tft.width()  // 
     16 #define SCREEN_HEIGHT tft.height() // Taille de l'écran
     17 
     18 static float zoom = 0.5;
     19 
     20 /* Fonction setup */
     21 void setup() {
     22 /* Initialise l'écran LCD */
     23   tft.begin();
     24   tft.setRotation(1);
     25   tft.fillScreen(TFT_BLACK);
     26   tft.setFreeFont(&FreeMono9pt7b);
     27 }
     28 
     29 /* Fonction loop() */
     30 void loop() {
     31   /* Dessine la fractale */
     32   draw_Julia(-0.8,+0.156,zoom);
     33   tft.fillRect(0, 0, 150, 20, TFT_BLACK);
     34   tft.setCursor(0,15);
     35   tft.setTextColor(TFT_WHITE);
     36   tft.print(" Zoom = ");
     37   tft.println(zoom);
     38   delay(2000);
     39   zoom *= 1.5;
     40   if (zoom > 100) zoom = 0.5;
     41 }
     42 
     43 /* 
     44   Dessine une fractale de Julia
     45  */
     46 
     47 void draw_Julia(float c_r, float c_i, float zoom) {
     48 
     49   tft.setCursor(0,0);
     50   float new_r = 0.0, new_i = 0.0, old_r = 0.0, old_i = 0.0;
     51 
     52   /* Pour chaque pixel en X */
     53 
     54   for(int16_t x = SCREEN_WIDTH/2 - 1; x >= 0; x--) { // Rely on inverted symettry
     55     /* Pour chaque pixel en Y */
     56     for(uint16_t y = 0; y < SCREEN_HEIGHT; y++) {      
     57       old_r = 1.5 * (x - SCREEN_WIDTH / 2) / (0.5 * zoom * SCREEN_WIDTH);
     58       old_i = (y - SCREEN_HEIGHT / 2) / (0.5 * zoom * SCREEN_HEIGHT);
     59       uint16_t i = 0;
     60 
     61       while ((old_r * old_r + old_i * old_i) < 4.0 && i < MAX_ITERATION) {
     62         new_r = old_r * old_r - old_i * old_i ;
     63         new_i = 2.0 * old_r * old_i;
     64 
     65         old_r = new_r+c_r;
     66         old_i = new_i+c_i;
     67         
     68         i++;
     69       }
     70       /* Affiche le pixel */
     71       if (i < 100){
     72         tft.drawPixel(x,y,tft.color565(255,255,map(i,0,100,255,0)));
     73         tft.drawPixel(SCREEN_WIDTH - x - 1,SCREEN_HEIGHT - y - 1,tft.color565(255,255,map(i,0,100,255,0)));
     74       }if(i<200){
     75         tft.drawPixel(x,y,tft.color565(255,map(i,100,200,255,0),0));
     76         tft.drawPixel(SCREEN_WIDTH - x - 1,SCREEN_HEIGHT - y - 1,tft.color565(255,map(i,100,200,255,0),0));
     77       }else{
     78         tft.drawPixel(x,y,tft.color565(map(i,200,300,255,0),0,0));
     79         tft.drawPixel(SCREEN_WIDTH - x - 1,SCREEN_HEIGHT - y - 1,tft.color565(map(i,200,300,255,0),0,0));
     80       }
     81     }
     82   }
     83 }