简体   繁体   English

表达结果未使用,图像生成

[英]Expression result unused, image generation

I'm trying to use this code to generate a circle image. 我正在尝试使用此代码生成圆形图像。

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

double e, f;

double scaler (double a, double b) {
    if (a < 256) {  e = (-1) * a / 256.0;
        if (b < 256) {
            f = b / 256.0;
        } else {
            f = (-1) * (b - 256.0) / 256.0;
        }
    } else { e = (a - 256.0) / 256.0;
        if (b < 256) {
            f = b / 256.0;
        } else {
            f = (-1) * (b - 256.0) / 256.0;
        }
    }
    return e, f;
}

int main () {

    int max_color = 255;
    int dimention = 512;

    ofstream fout;
    fout.open("mandeloutput.ppm");

    fout << "P3\n" << dimention << " " << dimention << endl << max_color << endl;
    for (int i = 0; i < dimention; i++) { f = i;
        for (int j = 0; j < dimention; j++) { e = j;
            scaler (e, f);
            cout << fixed << setprecision(5) << e << " " << f << endl;
            if ( e*e + f*f <= 1) {
                fout << right << setw(4) << 0 << setw(4) << 0 << setw(4) << 0 << "   ";
            } else {fout << right << setw(4) << 255 << setw(4) << 255 << setw(4) << 255 << "   ";}
        }
        fout << endl;
    }
} 

I can't understand why is my variable e always 0, and why I get the error described in the title regarding that variable inside scaler? 我不明白为什么我的变量e总是为0,为什么我在标题中看到关于缩放器中该变量的错误?

return e, f; doesn't do what you think it does. 不按照您的想法去做。 The comma operator simply evaluates and discards e and then the value of f is returned as a function result. 逗号运算符仅计算并丢弃e ,然后将f的值作为函数结果返回。 Since these are globals anyway there is no need to return anything from this function, but it would be better not to use globals and do this properly. 由于这些都是全局变量,因此无需从此函数返回任何内容,但是最好不要使用全局变量并正确地执行此操作。 Delete the global declaration double e, f; 删除全局声明double e, f; and change the function to something like this: 并将函数更改为如下所示:

void scaler (int a, int b, double &e, double &f) {
    if (a < 256) {  e = (-1) * a / 256.0;
        if (b < 256) {
            f = b / 256.0;
        } else {
            f = (-1) * (b - 256.0) / 256.0;
        }
    } else { e = (a - 256.0) / 256.0;
        if (b < 256) {
            f = b / 256.0;
        } else {
            f = (-1) * (b - 256.0) / 256.0;
        }
    }
}

In your main function you would then change these lines: 然后在您的主要功能中更改以下行:

for (int i = 0; i < dimention; i++) { f = i;
    for (int j = 0; j < dimention; j++) { e = j;
        scaler (e, f);
        ...

to this: 对此:

for (int i = 0; i < dimention; i++) {
    for (int j = 0; j < dimention; j++) {
        double e, f;
        scaler (j, i, e, f);
        ...

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

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