/***************************************************************************** Main Program for CS480 Programming Assignment 1 ****************************************************************************** Author: Stan Sclaroff Boston University Computer Science Dept. January 22, 1995 ****************************************************************************** Description: This is the main program for the line sketch routine. It supports drawing disconnected line segments and polygons. The user interface is as follows: QKEY: To quit, hit the Q key EKEY: To erase the screen (and the pixmap), hit the E key LEFTMOUSE: To enter a line end point, click the left mouse button RIGHTMOUSE: -- to do a flood fill, click the right mouse button ****************************************************************************** Comments: We are using the standard color lookup table in this assignment. This means that to change a color, you use one of the constants defined in (RED,BLACK,GREEN,BLUE,WHITE,YELLOW,CYAN,MAGENTA) ******************************************************************************/ #include #include #include #include #include #include "draw.h" #define SCREEN_WIDTH 500 #define SCREEN_HEIGHT 350 main() { long winid,start_curve=FALSE,test_number=1; Pixmap *pixmap; Device event; Screencoord mouse_pos[2],p[2]; /* open a window */ winid = open_window(SCREEN_WIDTH,SCREEN_HEIGHT,"Sketch Tool"); /* create a pixmap */ pixmap = create_pixmap(winid,SCREEN_WIDTH,SCREEN_HEIGHT); if(pixmap == 0) exit(EXIT_FAILURE); /* set pixmap to black */ set_pixmap(pixmap,BLACK); post_pixmap(pixmap); /* the inner loop: respond to events */ do switch(event = get_event(winid,mouse_pos)) { /* draw lines */ case LEFTMOUSE: /* draw an end point */ draw_point(winid,mouse_pos); /* if it's the first point, save it for later */ if(start_curve==FALSE) { p[0] = mouse_pos[0]; p[1] = mouse_pos[1]; start_curve = TRUE; } /* else draw a line connecting to previous point */ else { draw_line(pixmap,p,mouse_pos,YELLOW); start_curve = FALSE; } break; /* do a flood fill, using the current mouse position as the seed point */ case RIGHTMOUSE: flood_fill(pixmap,mouse_pos[0],mouse_pos[1], RED,read_pixel(pixmap,mouse_pos[0],mouse_pos[1])); post_pixmap(pixmap); start_curve = FALSE; break; /* erase the sketch pad, then drop through to do a redraw */ case EKEY: set_pixmap(pixmap,BLACK); start_curve = FALSE; /* redraw the pixmap because there was an expose event */ case REDRAW: post_pixmap(pixmap); break; /* quit, close up shop, exit */ case QKEY: close_window(winid); exit(EXIT_SUCCESS); break; /* toggle between the two test cases */ case TKEY: set_pixmap(pixmap,BLACK); start_curve = FALSE; if(test_number%2 == 1) test1(pixmap); else test2(pixmap); ++test_number; break; /* default: ignore all other events */ default: break; } while(TRUE); } /* draw test pattern 1: line spoke pattern */ test1(pixmap) Pixmap *pixmap; { float theta,d_theta,r; Screencoord p0[2],p1[2],center[2]; int i; d_theta = PI / 16.0; center[0] = pixmap->w / 2; center[1] = pixmap->h / 2; if(center[0] < center[1]) r = 0.75 * (float)center[0]; else r = 0.75 * (float)center[1]; for(theta=0.0,i=0;i<16;++i,theta+=d_theta) { p0[0] = (int)floor(0.5 + r*sin(theta)); p0[1] = (int)floor(0.5 + r*cos(theta)); p1[0] = -p0[0] + center[0]; p1[1] = -p0[1] + center[1]; p0[0] += center[0]; p0[1] += center[1]; draw_line(pixmap,p0,p1,GREEN); } } /* draw test pattern 2: a flower with five petals */ test2(pixmap) Pixmap *pixmap; { float theta,d_theta,d_petal,petal,r,p; Screencoord p0[2],p1[2],center[2]; int i; d_theta = 2.0*PI / 64.0; d_petal = 12.0*PI / 64.0; center[0] = pixmap->w / 2; center[1] = pixmap->h / 2; if(center[0] < center[1]) r = 0.75 * (float)center[0]; else r = 0.75 * (float)center[1]; p = r*0.25; /* draw the outer petals first */ p0[0] = center[0]; p0[1] = (int)floor(0.5 + r + p) + center[1]; for(petal=d_petal,theta=d_theta,i=0;i<=64;++i,theta+=d_theta,petal+=d_petal) { p1[0] = (int)floor(0.5 + r*sin(theta) + p*sin(petal)) + center[0]; p1[1] = (int)floor(0.5 + r*cos(theta) + p*cos(petal)) + center[1]; draw_line(pixmap,p0,p1,MAGENTA); p0[0] = p1[0]; p0[1] = p1[1]; } /* draw center */ r *= 0.5; p0[0] = center[0]; p0[1] = (int)floor(0.5 + r) + center[1]; for(petal=d_petal,theta=d_theta,i=0;i<=64;++i,theta+=d_theta,petal+=d_petal) { p1[0] = (int)floor(0.5 + r*sin(theta)) + center[0]; p1[1] = (int)floor(0.5 + r*cos(theta)) + center[1]; draw_line(pixmap,p0,p1,YELLOW); p0[0] = p1[0]; p0[1] = p1[1]; } }