//*************************** File main.c **************************** #include #include "../opsys.h" #include "../Xglib/xglib.h" #include "../Shape2d/application.h" #include "../Shape2d/win_view.h" #include "../Shape2d/mouse.h" #include "../Shape2d/circle.h" int wrap(int x, int len) { x = x % len; if (x < 0) x += len; return x; } void setpix_wrap(int x, int y) { // setpix but with wraparound x = wrap(x, DWID); y = wrap(y, DHEI); setpix(x, y); } void setpix_octs(int x, int y, point_t & cen) { int cenx = int(cen.givx()); int ceny = int(cen.givy()); setpix_wrap( x+cenx, y+ceny); // four quadrants setpix_wrap( x+cenx,-y+ceny); setpix_wrap(-x+cenx, y+ceny); setpix_wrap(-x+cenx,-y+ceny); setpix_wrap( y+cenx, x+ceny); // four quadrants, x and y interchanged setpix_wrap( y+cenx,-x+ceny); setpix_wrap(-y+cenx, x+ceny); setpix_wrap(-y+cenx,-x+ceny); } void bresenham(circle_t& c, application_t & ap) { // get center and radius in world coordinates point_t center = c.givcen(); double radius = c.givr(); point_t top = center + point_t(0.0,radius); // convert to screen coordinates ap.wv.win_dev(center); ap.wv.win_dev(top); int r = int(center.givy() - top.givy()); // draw the circle int x = 0; int y = r; int s = 3 - 2*r; while ( x <= y) { setpix_octs(x, y, center); x++; if (s <= 0) { s += 4*x + 6; } else { y--; s += 4*(x - y) + 10; } } } void main() { // create an "application". This will create the X window and initialize two colors // with color map locations 0 (background color) and 1 (foreground color). application_t ap; scene_t s(&(ap.wv)); // create a "mouse" mouse_t mouse(ap.wv) ; // set color entry 2 to red setlook(255,0,0,2); for (int i=0; i<20; i++) { circle_t c(mouse); // create a line (uses mouse clicks) bresenham(c,ap); // draw it mouse.wait_button_click(); // wait for mouse click col(2); // change color c.draw(&s); // draw circle using library routine col(1); // set color back to cyan mouse.wait_button_click(); // wait for mouse click setfr(0); // clear screen } } /********************************************************************/