Threaded Mandelbrot program C++ -
to preface this: i'm first-year student allowed enroll in second-year classes. because of this, i'm wrangling language (c++) haven't had time learn (first-years learn c#), code might not pretty.
our assignment twofold. first, need write program outputs mandelbrot image in ppm. achieve this, i've followed youtube tutorial here.
the second part of assignment make program multithreaded. essentially, program supposed use 4 threads each draw quarter of image.
to end have altered code video tutorial, , converted main method. now, i'm trying make first quarter of image. figured way adjust
(int y = 0; y < imageheight; y++) //rows! { (int x = 0; x < imagewidth; x++) //columns! (pixels in every row) { to
(int y = 0; y < halfheight; y++) //rows! { (int x = 0; x < halfwidth; x++) //columns! (pixels in every row) { however, instead of drawing top left quarter suspected, program drew along full width, repeating after halfway mark of image width reached, , drew along quarter of height (see image)
as learn mistakes, i'd love know going wrong here.
thank helping programming greenhorn out :)
full program code below.
#include "stdafx.h" #include <fstream> #include <iostream> int imagewidth = 512, imageheight = 512, maxn = 255, halfwidth = 256, halfheight = 256; double minr = -1.5, maxr = 0.7, mini = -1.0, maxi = 1.0; std::ofstream f_out("output_image.ppm"); int findmandelbrot(double cr, double ci, int max_iterations) { int = 0; double zr = 0.0, zi = 0.0; while (i < max_iterations && zr * zr + zi * zi < 4.0) { double temp = zr * zr - zi * zi + cr; zi = 2.0 * zr * zi + ci; zr = temp; i++; } return i; } double maptoreal(int x, int imagewidth, double minr, double maxr) { double range = maxr - minr; return x * (range / imagewidth) + minr; } double maptoimaginary(int y, int imageheight, double mini, double maxi) { double range = maxi - mini; return y * (range / imageheight) + mini; } void threadedmandelbrot() { (int y = 0; y < halfheight; y++) //rows! { (int x = 0; x < halfwidth; x++) //columns! (pixels in every row) { //... find real , imaginary values of c, corresponding // x,y pixel in image double cr = maptoreal(x, imagewidth, minr, maxr); double ci = maptoimaginary(y, imageheight, mini, maxi); //... find number of iterations in mandelbrot formula // using said c. int n = findmandelbrot(cr, ci, maxn); //... map resulting number rgb value. int r = (n % 256); int g = (n % 256); int b = (n % 256); //... output image f_out << r << " " << g << " " << b << " "; } f_out << std::endl; } } int main() { //initializes file f_out << "p3" << std::endl; f_out << imagewidth << " " << imageheight << std::endl; f_out << "256" << std::endl; //for every pixel... threadedmandelbrot(); f_out.close(); std::cout << "helemaal klaar!" << std::endl; return 0; }
your calculating quarter of image, have set dimension of halfheight, halfwidth or fill file zeroes. when image viewer reads file, shows 2 lines of in single line of pixels untill reaches end of file, @ quarter of picture height.
to fix problem have calculate other 3 quarters of image, suggest seperate calc function file writing function: threaded calcs putting result in array (std::array or std::vector), right color , write file.
Comments
Post a Comment