简体   繁体   中英

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?

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. 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; 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);
        ...

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