venerdì 10 aprile 2020

QML Mandelbrot in UBPorts / Ubuntu Touch (2)

Avevo gia' provato qui a creare l'insieme di Mandelbrot su UBPorts in QML ma il programma era scandalosamente lento ed ho provato a migliorare


In questo codice il risultato di calcolo per ogni pixel viene salvato in un array imgData che poi 
trasferito in un colpo sul Canvas. In questo modo il tempo di calcolo e' divenuto inferiore ad un terzo di quello della precedente prova

==============================================================
/*
 * Copyright (C) 2020  Luca Innocenit
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * ubuntu-calculator-app is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import QtQuick 2.7
import Ubuntu.Components 1.3
//import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import Qt.labs.settings 1.0
import QtQuick.Window 2.2

MainView {
    id: root
    objectName: 'mainView'
    applicationName: 'qtcpp.com.luca.innocenti'
    automaticOrientation: true

    visible: true
      width: Screen.width
      height: Screen.width
      Canvas {
          id: mycanvas
          anchors.centerIn: parent
          width: Screen.width
          height: Screen.width
          onPaint: {
              console.log("width "+ width.toString())
              console.log("height "+ height.toString())

              var ctx = getContext("2d");
              ctx.fillStyle = Qt.rgba(0, 0, 0, 1);
              ctx.fillRect(0, 0, width, height);
              var imgData = ctx.createImageData(height, width);

              ////////
              var re_min = -2.0;
             var im_min = -1.2;
             var re_max = 0.7;
             var im_max = 1.2;
             var iterazioni = 50;

             var r;
             var a,b;
             var x,y,x_new,y_new;
             var test;
             var re_factor = (re_max-re_min);
             var im_factor = (im_max-im_min);
             var indice = 0;

              for (var i=0;i<width;i++)
                          {
                          for (var j=0;j<height;j++)
                           {
                               a = re_min+(j*re_factor/height);
                               b = im_min+(i*im_factor/width);
                               x = 0;
                               y = 0;
                               test = 0;
                              imgData.data[indice+3]= 255;
                              //console.log(indice)

                               for (var k=0;k<iterazioni;k++)
                                    {
                                    x_new = (x*x)-(y*y)+a;
                                    y_new = (2*x*y)+b;
                                    if (((x_new*x_new)+(y_new*y_new))>4)
                                          {
                                            if (k%2)
                                                {
                                                  // colora il punto
                                                  imgData.data[indice]=255;
                                                  imgData.data[indice+1]=255;
                                                  imgData.data[indice+2]=255;  
                                                }

                                            break;
                                          }
                                    x = x_new;
                                    y = y_new;
                                    }
                               indice = indice + 4;
                           }
              }
              ///////
              console.log("terminato")
              ctx.drawImage(imgData, 0, 0);

          }
      }
}

Nessun commento:

Posta un commento

Regressione per illite su suoli naturali

Il dataset visto nel precedente post e' fornito anche da determinazione mineralogiche come la speciazione delle argille. Ho provato a ...