// FILE: canvas.cpp // written for the course 2172, 1999 // #include "canvas.h" // for the definition of setw #include // constructor // create a (xsize x ysize) canvas canvas::canvas(int xsize, int ysize ) :x(xsize),y(ysize){ if (x<=0 || y<=0) return; _bottom = new pixel*[y]; _top = new pixel*[y]; for(int i=0;i with color c and id // if p is out of the grid, do not crash void canvas::draw_point(int _x,int _y,const color& c,long id) { if (_x>=0 && _x=0 && _y= x || oy < 0 || oy >= y) return; if(border !=_top[oy][ox].get_id()) { if (c == clear) { _top[oy][ox] = _bottom[oy][ox]; } else { _top[oy][ox].set_color(c); } _top[oy][ox].set_id(border); fill_area(ox+1,oy,border,c); fill_area(ox-1,oy,border,c); fill_area(ox,oy+1,border,c); fill_area(ox,oy-1,border,c); } } // write a text ppm file ostream& canvas::dump_text_PPM(ostream& os) const { os << "P3" << endl << "# created by the \"draw\" scripted drawing program" << endl << "# developed for the course CSI2172 " << endl << "# University of Ottawa, 1999" << endl << x << ' ' << y << " 255 " << endl; int fc = 0; for(int i=y-1;i>=0;i--) { for(int j=0;j 4) { fc = 0; os << endl; } } } return os; } // write a binary ppm file ostream& canvas::dump_binary_PPM(ostream& os) const { os << "P6" << endl << "# created by the \"draw\" scripted drawing program" << endl << "# developed for the course CSI2172 " << endl << "# University of Ottawa, 1999" << endl << x << ' ' << y << " 255" << endl; for(int i=y-1;i>=0;i--) { for(int j=0;j and using the // well known Bresenheim drwaing algortihm // which does as many calculations as many pixels it // draws // (see any elementary computer graphics book) void canvas::draw_line(int x1, int y1, int x2, int y2, const color& c, long id) { int width = x2 - x1, height = y2 - y1, awidth = width>=0?width:-width, aheight = height>=0?height:-height; int e; // error term if (awidth>=aheight) { e = awidth; int j = y1; for(int i=0; i <= awidth;i++) { draw_point(x1+(width>0?1:-1)*i,j,c,id); e += 2*aheight; if (e >= 2*awidth) { e -= 2*awidth; j = height>0?j+1:j-1; } } // for } else { // dx/dy e=aheight; int i = x1; for(int j=0; j <= aheight;j++) { draw_point(i,y1+(height>0?1:-1)*j,c,id); e += 2*awidth; if (e>= 2*aheight) { e -= 2*aheight; i = width>0?i+1:i-1; } } // for } }