Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Window seems to stop responding upon fractal generation attempt  (Read 1204 times)

0 Members and 1 Guest are viewing this topic.

LayV

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Hey all. I'm pretty new to SFML, and I'm trying to write some code to generate the Mandelbrot Set. For some reason, when I run the code, it doesn't respond after I press enter (the key I set to generate the fractals).

Here's my code:

#include <iostream>
#include <cmath>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

const int width = 900, height = 600;

int maxIter = 128; //Program uses maximum escape time of 'maxIter' terms
double minRe = -2, maxRe = 1, minIm = -1, maxIm = 1; //Range of values for the window
double reInc = (maxRe - minRe)/width; //Multiply by x-coord of pixel to get the increment to add to minRe
double imInc = (maxIm - minIm)/height; //Multiply by y-coord of pixel to get the increment to add to minRe

struct complexNumber
{
    long double re;
    long double im;
};

sf::Color HSVtoRGB(int H, double S, double V) {
        int output[3];
    double C = S * V;
        double X = C * (1 - abs(fmod(H / 60.0, 2) - 1));
        double m = V - C;
        double Rs, Gs, Bs;

        if(H >= 0 && H < 60) {
                Rs = C;
                Gs = X;
                Bs = 0;
        }
        else if(H >= 60 && H < 120) {  
                Rs = X;
                Gs = C;
                Bs = 0;
        }
        else if(H >= 120 && H < 180) {
                Rs = 0;
                Gs = C;
                Bs = X;
        }
        else if(H >= 180 && H < 240) {
                Rs = 0;
                Gs = X;
                Bs = C;
        }
        else if(H >= 240 && H < 300) {
                Rs = X;
                Gs = 0;
                Bs = C;
        }
        else {
                Rs = C;
                Gs = 0;
                Bs = X;
        }
       
        output[0] = (Rs + m) * 255;
        output[1] = (Gs + m) * 255;
        output[2] = (Bs + m) * 255;

    return sf::Color(output[0], output[1], output[2]);
}

void generateMandelbrot(sf::VertexArray& plane)
{
#pragma omp parallel for
    for (double y = 0; y <= height; y += imInc)
    {
        for (double x = 0; x <= width; y += reInc)
        {
            //Convention: Real numbers are the 0th index, imaginary numbers are 1st index
            int yCoord = floor(y);
            int xCoord = floor(x);
            complexNumber c;
            c.re = x*reInc + minRe;
            c.re = y*imInc + minIm;
            complexNumber zOld;
            zOld.re = x*reInc + minRe;
            zOld.im = y*imInc + minIm;
            double modZ;
            complexNumber zNew;
            sf::Color pixelColor = sf::Color(0,0,0);

            for (int iter = 0; iter <= maxIter; iter++)
            {
                zNew.re = pow(zOld.re, 2) - pow(zOld.im, 2) + c.re;
                zNew.im = (2 * zOld.re * zOld.im) + c.im;
                zOld.re = zNew.re;
                zOld.im = zNew.im;

                modZ = pow(zNew.re, 2) + pow(zNew.im, 2);

                if (modZ >= 4) //Divergence case ==> We need to colour the pixel appropriately
                {
                    double hue = (((iter/maxIter) * 360) + 240) % 360; //Adding 240 to make it start on colour
                    sf::Color pixelColor = HSVtoRGB(hue, 255, 255);
                    break;
                }
                plane[xCoord * width + yCoord].position = sf::Vector2f(xCoord, yCoord);
                plane[xCoord * width + yCoord].color = pixelColor;  
            }        
        }
    }
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(width, height), "Mandelbrot set explorer");

    sf::VertexArray complexPlane(sf::Points, width * height);

    //Initial drawing to be done here
    window.clear();
    window.display();
    //Event loop
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            //Checks all the window's events that were triggered since the last iteration of the loop
            switch (event.type)
            {
                case (sf::Event::Closed):
                    window.close();
                    break;

                case (sf::Event::KeyPressed):
                    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
                    {
                        std::cout << "drawing image" << std::endl;
                        generateMandelbrot(complexPlane);
                        std::cout << "image drawn" << std::endl;
                    }  
            }
        }
        window.clear();
        window.draw(complexPlane);
        window.display();
    }

    return 0;
}

Any suggestions as to why this might be happening?
« Last Edit: June 01, 2020, 01:44:10 am by LayV »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Window seems to stop responding upon fractal generation attempt
« Reply #1 on: June 01, 2020, 09:19:44 am »
It probably takes long to compute, you should first check that.
Laurent Gomila - SFML developer

LayV

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Window seems to stop responding upon fractal generation attempt
« Reply #2 on: June 01, 2020, 09:46:31 am »
It probably takes long to compute, you should first check that.

Forgive my incompetence, but how would I go about checking this?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Window seems to stop responding upon fractal generation attempt
« Reply #3 on: June 01, 2020, 10:52:35 am »
You can print x and y every time your algorithm loops, for example. That should give you a good idea of the total time it would take to complete.
Laurent Gomila - SFML developer

 

anything