Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 30
  1. #1

    [C++] std::complex

    Ciao a tutti.
    Sto provando a implementare un programmino che mi permetta di disegnare l'insieme di Mandelbrot ma mi sono bloccato sull'utilizzo degli std::complex.
    È possibile modificare unicamente o la parte reale o la parte immaginaria e non tutte e due assieme?!
    Propio non riesco a utilizzare la libreria per i miei scopi. Potrei risolvere utilizzando due variabili separate, ma visto che c'è la libreria...

    Grazie.
    K. L. Thompson
    You can't trust code that you did not totally create yourself.
    A. Bogk
    UNIX is user-friendly, it just chooses its friends.

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Modificare no.
    Puoi però creare un nuovo complex a partire dai dati che ti interessano.
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  3. #3
    Originariamente inviato da shodan
    Modificare no.
    Puoi però creare un nuovo complex a partire dai dati che ti interessano.
    E come esattamente?

    Così:
    codice:
    complex<double> Cn(10, 10)
    
    complex<double> Zn (real(Cn), 11)
    ?
    K. L. Thompson
    You can't trust code that you did not totally create yourself.
    A. Bogk
    UNIX is user-friendly, it just chooses its friends.

  4. #4
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Si. O anche:
    codice:
    complex<double> Zn (Cn.real(), 11)
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  5. #5
    Originariamente inviato da shodan
    Si. O anche:
    codice:
    complex<double> Zn (Cn.real(), 11)
    Ok, perfetto!
    Grazie!

    K. L. Thompson
    You can't trust code that you did not totally create yourself.
    A. Bogk
    UNIX is user-friendly, it just chooses its friends.

  6. #6

    [C++] Insieme di Mandelbrot (Mandelbrot Set)

    Ho appena scritto questo programma in C++ per creare l'insieme di Mandelbrot in bianco e nero ma non funzione. O meglio: fa schizzare l'attività di processore e ventole alle stelle senza però produrre un bel cavolo di niente in tempi apprezzabili.

    La prima esecuzione ho sbagliato io. Ho passato al programma i seguenti argomenti, palesemente troppo elevati: 1024 786 100.
    Ho poi rieseguito il programma con i dati seguenti, ma con lo stesso risultato: 20 12 25.

    Il programma è giusto ed è un problema di efficienza, vi sono altri problemi oppure mi tocca proprio aspettare un sacco?

    codice:
    #include <iostream>
    using std::cout;
    using std::endl;
    using std::ios;
    
    #include<cmath>
    using std::pow;
    
    #include<vector>
    using std::vector;
    
    #include <fstream>
    using std::ofstream;
    
    #include <complex>
    using std::complex;
    
    #include<cstdlib>
    using std::exit;
    
    bool** createPBM (int imageW, int imageH)
    {
    	bool** image = new bool*[imageH];
    	
    	for (int i = 0; i < imageH; i++) 
    	{
    		image[i] = new bool[imageW];
    	}
    	
    	return image;
    }
    
    void mandelbrot (int imageW, int imageH, bool** image, int iterations)
    {
    	double const Rmin = 2, Rmax = 1;
    	double const Imin = -1.2, Imax = 1.2;
    	double const scaleR = (Rmax - Rmin) / (imageW - 1), scaleI = (Imax - Imin) / (imageH - 1);
    	
    	complex<double> Z (0, 0);
    	complex<double> Zn (0, 0);
    	complex<double> C (0, 0);
    	
    	for (int y = 0; y < imageH; y++) 
    	{
    		complex<double> C (C.real(), Imax - y * scaleI);
    		
    		for (int x = 0; x < imageW; x++) 
    		{
    			complex<double> C (Rmin + x * scaleR, C.imag());
    			complex<double> Z (C.real(), C.imag());
    			
    			for (int n = 0; n < iterations; n++) 
    			{
    				complex<double> Zn (pow(Z.real(), 2), pow(Z.imag(), 2));
    				
    				if (Zn.real() + Zn.imag() > 4) 
    				{
    					image[x][y] = false;
    					break;
    				}
    				
    				complex<double> Z (Zn.real() - Zn.imag() + C.real(), 2 * Z.real() * Z.imag() + C.imag());
    			}
    		}
    	}
    }
    
    void printPBM (int imageW, int imageH,bool** image, ofstream& oFile)
    {
    	for (int i = 0; i < imageW; i++) 
    	{
    		for (int j = 0; j < imageH; j++) 
    		{
    			if (image[i][j]) 
    			{
    				oFile << 1 << ' ';
    			}
    			else 
    			{
    				oFile << 0 << ' '; 
    			}
    
    		}
    	}
    }
    
    int main (int argc, int const argv[]) 
    {
    	ofstream oFile("mandelbrot.txt", ios::out);
    	
    	if (argc =! 4) 
    	{
    		cout << "Invalid arguments." << endl;
    		exit(1);
    	}
    	
    	bool** image = createPBM(argv[1], argv[2]);
    	
    	mandelbrot(argv[1], argv[2], image, argv[3]);
    	
    	printPBM(argv[1], argv[2], image, oFile);
    	
    	system("convert mandelbrot.txt mandelbrot.png");
    }
    K. L. Thompson
    You can't trust code that you did not totally create yourself.
    A. Bogk
    UNIX is user-friendly, it just chooses its friends.

  7. #7
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Intanto correggi questo (non c'entra ma è ambiguo):
    codice:
    if (argc =! 4)
    Hai provato a commentare qualcosa? Tipo quella system ad esempio.
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  8. #8
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,296

    Moderazione

    Ho unito le discussioni aperte sullo stesso argomento.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  9. #9

    Re: Moderazione

    Originariamente inviato da alka
    Ho unito le discussioni aperte sullo stesso argomento.
    Ok. =).

    Nessuno ha consigli allora?

    Magari ho sbagliato già all'inizio, modificando gli argomenti presi dal main()!
    K. L. Thompson
    You can't trust code that you did not totally create yourself.
    A. Bogk
    UNIX is user-friendly, it just chooses its friends.

  10. #10
    argv [1], argv [2], argv [3] sono char *, ma tu le tratti come se fosse int, quindi giustamente, ad esempio in "bool** createPBM (int imageW, int imageH)" imageW e imageH avranno dei valori.. indecenti ;-)
    per converire da char * a int vedi ad esempio http://en.wikipedia.org/wiki/Atoi

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.