Outils pour utilisateurs

Outils du site


fractale_de_lyapunov

Table des matières

Fractale de Lyapunov

C++

// Compilation : g++ Lyapunov.cpp -o Lyapunov
// Execution   : ./Lyapunov "AB" 1.8 2.2 3.6 4.0 200 200 2000 -1
 
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <string>
 
using namespace std;
 
int largeur, hauteur;
int iteration;
string sequence;
float bornes[4];
float limite;
 
float proportion(float valeur, float valeurMin, float valeurMax, float valeurOutMin, float valeurOutMax) {
    float valeurOut = (valeur - valeurMin) / (valeurMax - valeurMin);
    valeurOut = valeurOutMin + valeurOut * (valeurOutMax - valeurOutMin);
    return valeurOut;
}
 
int main(int argc, char *argv[]) {
    if (argc != 10) {
        cout << "Format : ./Lyapunov \"AB\" 1.8 2.2 3.6 4.0 200 200 2000 -1" << endl;
        return 0;
    }
    // Initialisation des constantes
    sequence = argv[1];
    for (int i = 0; i < 4; i++) {
        bornes[i] = stof(argv[i + 2]);
    }
    largeur = stoi(argv[6]);
    hauteur = stoi(argv[7]);
    iteration = stoi(argv[8]);
    limite = stof(argv[9]);
 
    time_t debut, fin;
    time(&debut);
 
    float  tableau[iteration];
    tableau[0]= 0.5;
    float a, b;
    float* sequenceTab[sequence.size()];
    for (int i = 0; i < sequence.size(); i++) {
        sequenceTab[i] = (sequence[i] == 'A') ? &a : &b;
    }
    float aValeur[largeur];
    float bValeur[hauteur];
    for (int i = 0; i < largeur; i++) {
        aValeur[i] = proportion(i, 0, largeur, bornes[0], bornes[1]);
    }
    for (int i = 0; i < hauteur; i++) {
        bValeur[i] = proportion(i, 0, hauteur, bornes[2], bornes[3]);
    }
 
    // Formation du nom du fichier
    string nom  =  "Lyapunov_" + sequence;
    for (int i = 0; i < 4; i++) {
        string s = "_" + to_string(bornes[i]);
        int position = s.find('.');
        if (position != -1) {
            s[position] = ',';
        }
        nom +=  s;
    }
    nom += ".pgm";
 
    // Création de l'image
    ofstream image(nom);
    image << "P2" << endl;
    image << largeur << " " << hauteur << endl;
    image << "255" << endl;
    for (int y = 0; y < hauteur; y++) {
        b = bValeur[y];
        for (int x = 0; x < largeur; x++) {
            a = aValeur[x];
            for (int k = 1; k < iteration; k++) {
                tableau[k] = *sequenceTab[k % sequence.size()] * tableau[k - 1] * (1 - tableau[k - 1]);
            }
            float exposant = 0;
            for (int k = 1; k < iteration; k++) {
              exposant += log(abs(*sequenceTab[k % sequence.size()] * (1 - 2 * tableau[k])));
            }
            exposant = exposant / iteration;
 
            int c;
            if ((exposant > 0) || (exposant < limite)) {
              c = 0;
            } else {
              c = floor(proportion(exposant, 0, limite, 255, 0));
            }
            image << c << endl;;
        }
    }
 
 
    time(&fin);
    cout << "Durée = " << difftime(fin, debut) << endl;
    const string fonction = "open " + nom;
    system(fonction.c_str());
    return 0;
}

Processing

int iteration = 20000;
int valMin = 0;
 
void setup() {
size(200, 200);
}
 
void draw() {
println(hour() + ":" + minute() + ":" + second());
loadPixels();
for (int i = 0; i < width; i++) {
 for (int j = 0; j < height; j++) {
   float a = map(i, 0, width, 1.8, 2.2);
   float b = map(j, 0, height, 3.6,4);
   float seq[] = {a, a, a, a, b, b, b, b, b,b ,b , b,b , b, b, b, b, b, b, b}; // AAABBBBBAB
   float[]  A = new float[iteration];
   A[0]= 0.5;
   for (int k = 1; k < iteration; k++) {
     A[k] = seq[k % seq.length] * A[k - 1] * (1 - A[k - 1]);
   }
   float exposant = 0;
   for (int k = 1; k < iteration; k++) {
     exposant += log(abs(seq[k % seq.length] * (1 - 2 * A[k])));
   }
   exposant = exposant / iteration;
 
   int c;
   if ((exposant > 0) || (exposant < -1)) {
     c = 0;
   } else {
     c = floor(map(exposant, 0, -1, 255, 0));
   }
   pixels[i + j * width] = color(c);
 }
}
updatePixels();
saveFrame("Lyapunov" + "-" + year() + "-" + month() + "-" + day() + "-" + hour() + "-" + minute() + "-" + second() + ".png");
println(hour() + ":" + minute() + ":" + second());
exit(); 
}

Liens

PPM

fractale_de_lyapunov.txt · Dernière modification: 2020/04/07 18:53 par Mushussu