简体   繁体   中英

Fractal generating code just not working

I have copied this code from the internet, but it doesn't seem to work.

All it does is draw a red line on one edge of a black picture it makes.

I have been playing with it quite a long time and found that when I print the contents of r1, r2 and r3, it writes: (0,0) (0.866025,0) (-0.866025,0) So that seems like one thing, that it displays the parts of the complex number in the wrong order, but why does it display r1 to be zero, when it is clearly not?

Also, does it seem to be the cause of this code not working?

GLuint CMyApp::NewtonFractalTexture()
{
const int pic_size = 256;
unsigned char tex[pic_size][pic_size][3];

int MaxCount = 255;
int color_multiplier = 15;
float precision = 0.0001;
std::complex<double> r1 = (1, 0);
std::complex<double> r2 = (-0.5, sin(2 * PI / 3));
std::complex<double> r3 = (-0.5, -sin(2 * PI / 3));

std::cout << r1 << " " << r2 << " " << r3 << std::endl;
std::cout << abs(r1) << " " << abs(r2) << " " << abs(r3) << std::endl;

/*
std::complex<double> roots[birds_num];
for (int i = 0; i < birds_num; ++i){
    roots[i] = (bird_positions[i][0], bird_positions[i][2]);
}
*/

for (int i = 0; i < pic_size; ++i){
    for (int j = 0; j < pic_size; ++j)
    {
        //
        std::complex<double> z = (i, j);
        //
        int count = 0;
        while (count < MaxCount && abs(z - r1) >= precision && abs(z - r2) >= precision && abs(z - r3) >= precision){
            /*
            std::complex<double> my_numerator = (z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[8]);
            std::complex<double> my_denominator = (z - roots[0])*(z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[8]) +
                                       (z - roots[0])*(z - roots[1])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[8]) +
                                       (z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[8]) +
                                       (z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[8]) +
                                       (z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[8]) +
                                       (z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[8]) +
                                       (z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[6])*(z - roots[7])*(z - roots[8]) +
                                       (z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[7])*(z - roots[8]) +
                                       (z - roots[0])*(z - roots[1])*(z - roots[2])*(z - roots[3])*(z - roots[4])*(z - roots[5])*(z - roots[6])*(z - roots[7])*(z - roots[8])*(z - roots[8]);
            */
            std::complex<double> my_numerator = z*z*z - 1.0;
            std::complex<double> my_denominator = z*z * 3.0;
            if (abs(z) > 0){
                z = z - my_numerator / my_denominator;
            }
            ++count;
        }
        //
        tex[i][j][0] = 0;
        tex[i][j][1] = 0;
        tex[i][j][2] = 0;
        //
        if (abs(z - r1) < precision){
            tex[i][j][0] = 255 - count * color_multiplier;
        }
        if (abs(z - r2) <= precision){
            tex[i][j][1] = 255 - count * color_multiplier;
        }
        if (abs(z - r3) <= precision){
            tex[i][j][2] = 255 - count * color_multiplier;
        }
        //
    }
}

GLuint tmpID;

// generáljunk egy textúra erőforrás nevet
glGenTextures(1, &tmpID);
// aktiváljuk a most generált nevű textúrát
glBindTexture(GL_TEXTURE_2D, tmpID);
// töltsük fel adatokkal az...
gluBuild2DMipmaps(  GL_TEXTURE_2D,          // aktív 2D textúrát
                    GL_RGB8,                // a vörös, zöld és kék csatornákat 8-8 biten tárolja a textúra
                    pic_size, pic_size,     // kép méretének megadása
                    GL_RGB,                 // a textúra forrása RGB értékeket tárol, ilyen sorrendben
                    GL_UNSIGNED_BYTE,       // egy-egy színkopmonenst egy unsigned byte-ról kell olvasni
                    tex);                   // és a textúra adatait a rendszermemória ezen szegletéből töltsük fel
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);   // bilineáris szűrés kicsinyítéskor
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);   // és nagyításkor is
glBindTexture(GL_TEXTURE_2D, 0);

return tmpID;
}

You're not initializing your complex numbers right. You either need to use

std::complex<double> r1(1, 0);

or

std::complex<double> r1 {1, 0};

(note the curly brackets here).

r1(1,0) is a constructor call, r1{1,0} is uniform initialization, and r1 = (1,0) is the same as r1 = 0 because the (1,0 ) is a use of the comma operator, whose value is the last expression.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM