简体   繁体   English

分形生成代码不起作用

[英]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? 我已经玩了很长时间了,发现当我打印r1,r2和r3的内容时,它写道:(0,0)(0.866025,0)(-0.866025,0)所以这似乎是一回事,它以错误的顺序显示复数的各个部分,但是为什么在r1明显不是的情况下却显示r1为零?

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. r1(1,0)是构造函数调用, r1{1,0}是统一初始化,并且r1 = (1,0)r1 = 0相同,因为(1,0 )是使用逗号运算符,其值为最后一个表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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