#ifndef PICTUREH #define PICTUREH #include #include #include #include #include #include "xyz.h" using namespace std; typedef unsigned char raster_t[383][223][3]; class picture { public: static int round(double d) { const int i = static_cast(d); const double difference = d - i; return difference >= .5 ? i + 1 : difference < -.5 ? i - 1 : i; } static const unsigned char cmin = 0; static const unsigned char cmax = 255; static const size_t width = 383; static const int xmax = width / 2; static const int xmin = -xmax; static const size_t height = 223; static const int ymax = height / 2; static const int ymin = -ymax; static const int dmax = 1000000; static const double stretch; int frameno, nframes; xyz viewpoint; //viewpoint xyz direction; //looking toward this point double zrot; //rotation around line of sight double d; //distance from viewpoint to screen //because of the inverse square law, a point is visible //out to a distance of d * visible. static const double visible; raster_t a; picture( xyz initial_viewpoint = xyz(0, 0, dmax), xyz initial_direction = xyz(), double initial_d = dmax, double initial_zrot = 0) : frameno(0), nframes(0), viewpoint(initial_viewpoint), direction(initial_direction), d(initial_d), zrot(initial_zrot) { for (size_t x = 0; x < width; ++x) { for (size_t y = 0; y < height; ++y) { for (size_t color = 0; color < 3; ++color) { a[x][y][color] = 0; } } } } picture(const raster_t initial_a, xyz initial_viewpoint = xyz(), xyz initial_direction = xyz(0, 0, -1), double initial_d = dmax, double initial_zrot = 0) : frameno(0), nframes(0), viewpoint(initial_viewpoint), direction(initial_direction), d(initial_d), zrot(initial_zrot) { for (size_t x = 0; x < width; ++x) { for (size_t y = 0; y < height; ++y) { for (size_t color = 0; color < 3; ++color) { a[x][y][color] = initial_a[x][y][color]; } } } } void drawpoint(double x, double y, unsigned char red, unsigned char green, unsigned char blue) { x *= stretch; const size_t ix = round(x + width / 2.0); const size_t iy = round(y + height/ 2.0); if (ix < width && iy < height /* && (a[ix][iy][0] < red || a[ix][iy][1] < green || a[ix][iy][2] < blue) */) { for (size_t color = 0; color < 3; ++color) { a[ix][iy][0] = red; a[ix][iy][1] = green; a[ix][iy][2] = blue; } } } void drawpoint(double x, double y, unsigned char brightness = cmax) { x *= stretch; const size_t ix = round(x + width / 2.0); const size_t iy = round(y + height/ 2.0); if (ix < width && iy < height /* && (a[ix][iy][0] < brightness || a[ix][iy][1] < brightness || a[ix][iy][2] < brightness) */ ) { for (size_t color = 0; color < 3; ++color) { a[ix][iy][color] = brightness; } } } void plotpoint(xyz point, unsigned char red, unsigned char green, unsigned char blue); void plotpoint(xyz point) {plotpoint(point, cmax, cmax, cmax);} friend ostream& operator<<(ostream& ost, const picture& p); void write(const char *outfilename, const char *comment) const; }; class putable; picture& operator<<(picture& pict, const putable& put); #endif