/* intersect.cpp Laura Toma What it does: Draws a set of horizontal and vertical line segments in the default 2D projection. Then it pretends to compute their intersections using the line sweep algorithm, and simulates the sweep line moving from left to right. */ #include "geom.h" #include "rtimer.h" #include #include #include #include #ifdef __APPLE__ #include #else #include #endif #include using namespace std; GLfloat red[3] = {1.0, 0.0, 0.0}; GLfloat green[3] = {0.0, 1.0, 0.0}; GLfloat blue[3] = {0.0, 0.0, 1.0}; GLfloat black[3] = {0.0, 0.0, 0.0}; GLfloat white[3] = {1.0, 1.0, 1.0}; GLfloat gray[3] = {0.5, 0.5, 0.5}; GLfloat yellow[3] = {1.0, 1.0, 0.0}; GLfloat magenta[3] = {1.0, 0.0, 1.0}; GLfloat cyan[3] = {0.0, 1.0, 1.0}; /* forward declarations of functions */ void display(void); void keypress(unsigned char key, int x, int y); void timerfunc(); void initialize_segments_random(); void initialize_segments_horizontal(); void print_segments(); //renders the sweep line void draw_sweep_line(); //renders the active structure void draw_active_structure(); //renders the intersection points void draw_intersection_points(); /* global variables */ const int WINDOWSIZE = 500; //we got two test cases so far: random and horizontal; add more! int init_case = 0; const int NB_TEST_CASES = 2; //NOTE: all the structures below need to be global so that they can be rendered //number of segments requested by user int n; //the array of segments vector segments; //the active structure that stores the segments intersecting the sweep line vector as; //the intersections points of the segments vector intpoints; //the events. do we need this?? //vector events; //current position of sweep line; this is used to animate the sweep line moving int sweep_line_x = 0; /* ************************************************** */ //fills global variable "segments" with n segments void initialize_segments() { switch (init_case) { case 0: initialize_segments_random(); break; case 1: initialize_segments_horizontal(); break; default: initialize_segments_random(); } init_case = (init_case+1) % NB_TEST_CASES; return; } /* ************************************************** */ //fills global variable "segments" with n horizontal segments void initialize_segments_horizontal() { int i; point2D a,b; segment2D s; //clear the vector of segments segments.clear(); //a long horizontal segment a.x = 1; a.y = WINDOWSIZE/2; b.x = WINDOWSIZE - 10; b.y = a.y; s.start = a; s.end = b; segments.push_back(s); //n-1 vertical segments for (i=0; i\n"); exit(1); } n = atoi(argv[1]); printf("you entered n=%d\n", n); assert(n >0); //the default is to initialize the segments randomly initialize_segments_random(); print_segments(); //Rtimer rt1; //rt_start(rt1); //compute the intersections //rt_stop(rt1); //print the timing // char buf [1024]; //rt_sprint(buf,rt1); //printf("run time: %s\n\n", buf); //fflush(stdout); /* initialize GLUT */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(WINDOWSIZE, WINDOWSIZE); glutInitWindowPosition(100,100); glutCreateWindow(argv[0]); /* register callback functions */ glutDisplayFunc(display); glutKeyboardFunc(keypress); glutIdleFunc(timerfunc); //<--------note we got an idle function, we'll use it to animate /* init GL */ /* set background color black*/ glClearColor(0, 0, 0, 0); /* give control to event handler */ glutMainLoop(); return 0; } /* draw the segments stored in global variable segments NOTE: We draw in the local coordinate system (0,0) to (WINSIZE,WINSIZE) */ void draw_segments(){ //set color glColor3fv(yellow); int i; for (i=0; i