Für Vorlesungen, bitte die Webseite verwenden. https://flavigny.de/lecture
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

117 行
2.7KB

  1. #include <string>
  2. #include "pgm.hh"
  3. // Komplexe Zahl
  4. struct Complex
  5. {
  6. double real;
  7. double imag;
  8. };
  9. class Canvas
  10. {
  11. public :
  12. // Konstruktor, erzeuge int* _pixels
  13. Canvas(double center_x, double center_y,
  14. double width, double height,
  15. int horPixels, int vertPixels);
  16. // Desktruktor, raeume int* _pixels auf
  17. ~Canvas();
  18. // gibt die Breite des Bildes zurueck
  19. double width();
  20. // gibt die Hoehe des Bildes zurueck
  21. double height();
  22. // gibt die Anzahl an horizontalen Pixeln
  23. int horPixels();
  24. // gibt die Anzahl an vertikalen Pixeln
  25. int vertPixels();
  26. // gebe die Koordinaten des Pixels (i,j) als Complex zurueck
  27. Complex coord(int i, int j);
  28. // schreibe value an den Pixel (i,j)
  29. // Ueberlegen Sie wie aus (i,j) den flachen Index bei row-major bekommen
  30. void writePixel(int i, int j, int value);
  31. // Zugang zum Pixel (i,j) im 1D Array
  32. // Ueberlegen Sie wie aus (i,j) den flachen Index bei row-major bekommen
  33. int operator()(int i, int j);
  34. // schreibe Bild mit Dateinamen filename
  35. void write(std::string filename);
  36. private :
  37. double _center_x;
  38. double _center_y;
  39. double _width;
  40. double _height;
  41. int _horPixels;
  42. int _vertPixels;
  43. int* _pixels;
  44. };
  45. // diese Methode ist bereits implementiert
  46. void Canvas::write(std::string filename)
  47. {
  48. write_pgm(_pixels,_horPixels,_vertPixels,filename);
  49. }
  50. Canvas::Canvas(double center_x, double center_y, double width, double height, int horPixels,
  51. int vertPixels) {
  52. // initialize all values
  53. _center_x = center_x;
  54. _center_y = center_y;
  55. _width = width;
  56. _height = height;
  57. _horPixels = horPixels;
  58. _vertPixels = vertPixels;
  59. _pixels = new int[horPixels * vertPixels];
  60. }
  61. Canvas::~Canvas() {
  62. _pixels = 0;
  63. }
  64. double Canvas::width() {
  65. return _width;
  66. }
  67. double Canvas::height() {
  68. return _height;
  69. }
  70. int Canvas::horPixels() {
  71. return _horPixels;
  72. }
  73. int Canvas::vertPixels() {
  74. return _vertPixels;
  75. }
  76. Complex Canvas::coord(int i, int j) {
  77. Complex c;
  78. // Bringe die Pixel mit den echten Laengen ins Verhaeltnis
  79. double y = i * (_height / _vertPixels) - _height / 2;
  80. double x = j * (_width / _horPixels) - _width / 2;
  81. // Gebe die Koordinaten in Relation zum gewuenschten Zentrum aus
  82. c.imag = y + _center_y;
  83. c.real = x + _center_x;
  84. return c;
  85. }
  86. void Canvas::writePixel(int i, int j, int value) {
  87. // i = Zeile und wegen row-major Konvention muss i mit der Zahl der horizontalen
  88. // Pixel multipliziert werden
  89. _pixels[i*_horPixels + j] = value;
  90. }
  91. int Canvas::operator()(int i, int j) {
  92. // i = Zeile und wegen row-major Konvention muss i mit der Zahl der horizontalen
  93. // Pixel multipliziert werden
  94. return _pixels[i*_horPixels + j];
  95. }