<-------- Header File ----------->
#include <string>
#include <vector>
#ifndef MANDELBROT_ASCIIIMAGE_H
#define MANDELBROT_ASCIIIMAGE_H
class ASCIIImage
{
private:
int height;
int length;
char** field;
std::string charset="$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. ";
public:
ASCIIImage(int height, int length);
void setPix(int x, int y, int color);
void show();
};
#endif //MANDELBROT_ASCIIIMAGE_H
<------- image.cpp --------->
#include "asciiimage.h"
#include <iostream>
ASCIIImage::ASCIIImage(int height, int weidth) : height(height), length(weidth) {
field = new char*[height];
for(int i = 0; i < height; i++){
field[i] = new char[length];
}
}
void ASCIIImage::setPix(int x, int y, int color) {
int index = color;
if(index < 0){
index = 0;
}else if(color >= charset.size()){
index = charset.size()-1;
}
index = (index-charset.size() + 1) * (-1);
field[y][x] = charset[index];
}
void ASCIIImage::show() {
for (int i = 0; i < height; ++i) {
for (int j = 0; j < length; ++j) {
std::cout << field[i][j];
}
std::cout << std::endl;
}
}
<--------- main.cpp --------->
#include <iostream>
#include "asciiimage.h"
using namespace std;
int main() {
int height, weidth, max, finish;
double x0, x1, y0, y1, y, x, zr, zi;
cout << "Enter Width: ";
cin >> weidth;
cout << "Enter Height: ";
cin >> height;
cout << "Enter x0: ";
cin >> x0;
cout << "Enter y0: ";
cin >> y0;
cout << "Enter x1: ";
cin >> x1;
cout << "Enter y1: ";
cin >> y1;
cout << "Enter max: ";
cin >> max;
ASCIIImage *asciiimage = new ASCIIImage(height, weidth);
for(int w = 0; w < weidth; w++) {
for(int h = 0; h < height; h++) {
x = x0 + w*(x1 - x0)/(weidth);
y = y0 + h*(y1 - y0)/(height);
zr = 0.;
zi = 0.;
finish = 1;
for (int i = 0; i < max; ++i) {
// calculate next iteration
// For the interested reader: z=zr+i*zi and c=x+i*y are complex numbers
// In the next row we calculate z * z + c and check whether |z| > 2
double nextzr = zr * zr - zi * zi + x;
double nextzi = 2 * zr * zi + y;
// are we done?
if (nextzr * nextzr + nextzi * nextzi > 4) {
// set pixel to i % 70
asciiimage->setPix(w, h, (i % 70));
break;
}
zr = nextzr;
zi = nextzi;
if(i+1 == max){
finish = 0;
}
}
if(finish != 1) {
asciiimage->setPix(w, h, max % 70);
}
}
}
asciiimage->show();
return 0;
}
Comments