|
- #include <string>
- #include "pgm.hh"
-
- // Komplexe Zahl
- struct Complex
- {
- double real;
- double imag;
- };
-
- class Canvas
- {
- public :
- // Konstruktor, erzeuge int* _pixels
- Canvas(double center_x, double center_y,
- double width, double height,
- int horPixels, int vertPixels);
-
- // Desktruktor, raeume int* _pixels auf
- ~Canvas();
-
- // gibt die Breite des Bildes zurueck
- double width();
-
- // gibt die Hoehe des Bildes zurueck
- double height();
-
- // gibt die Anzahl an horizontalen Pixeln
- int horPixels();
-
- // gibt die Anzahl an vertikalen Pixeln
- int vertPixels();
-
- // gebe die Koordinaten des Pixels (i,j) als Complex zurueck
- Complex coord(int i, int j);
-
- // schreibe value an den Pixel (i,j)
- // Ueberlegen Sie wie aus (i,j) den flachen Index bei row-major bekommen
- void writePixel(int i, int j, int value);
-
- // Zugang zum Pixel (i,j) im 1D Array
- // Ueberlegen Sie wie aus (i,j) den flachen Index bei row-major bekommen
- int operator()(int i, int j);
-
- // schreibe Bild mit Dateinamen filename
- void write(std::string filename);
- private :
- double _center_x;
- double _center_y;
- double _width;
- double _height;
- int _horPixels;
- int _vertPixels;
- int* _pixels;
- };
-
- // diese Methode ist bereits implementiert
- void Canvas::write(std::string filename)
- {
- write_pgm(_pixels,_horPixels,_vertPixels,filename);
- }
-
- Canvas::Canvas(double center_x, double center_y, double width, double height, int horPixels,
- int vertPixels) {
- // initialize all values
- _center_x = center_x;
- _center_y = center_y;
- _width = width;
- _height = height;
- _horPixels = horPixels;
- _vertPixels = vertPixels;
- _pixels = new int[horPixels * vertPixels];
- }
-
- Canvas::~Canvas() {
- _pixels = 0;
- }
-
- double Canvas::width() {
- return _width;
- }
-
- double Canvas::height() {
- return _height;
- }
-
- int Canvas::horPixels() {
- return _horPixels;
- }
-
- int Canvas::vertPixels() {
- return _vertPixels;
- }
-
- Complex Canvas::coord(int i, int j) {
- Complex c;
- // Bringe die Pixel mit den echten Laengen ins Verhaeltnis
- double y = i * (_height / _vertPixels) - _height / 2;
- double x = j * (_width / _horPixels) - _width / 2;
- // Gebe die Koordinaten in Relation zum gewuenschten Zentrum aus
- c.imag = y + _center_y;
- c.real = x + _center_x;
- return c;
- }
-
- void Canvas::writePixel(int i, int j, int value) {
- // i = Zeile und wegen row-major Konvention muss i mit der Zahl der horizontalen
- // Pixel multipliziert werden
- _pixels[i*_horPixels + j] = value;
- }
-
- int Canvas::operator()(int i, int j) {
- // i = Zeile und wegen row-major Konvention muss i mit der Zahl der horizontalen
- // Pixel multipliziert werden
- return _pixels[i*_horPixels + j];
- }
|