/* view.c Laura Toma What it does: Draws a set of horizontal and vertical line segments in the default 2D projection. Then computes their intersections using the line sweep algorithm, and simulates the algorithm as it runs. */ #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}; GLint fillmode = 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; 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 //current position of sweep line int sweep_line_x = 0; //number of segments requested by user int n; //the array of segments vector segments; //the intersections points of the segments vector intpoints; //the active structure that stores the segments intersecting the sweep line vector as; //the events //vector events; /* ************************************************** */ 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; } /* ************************************************** */ void initialize_segments_horizontal() { int i; point2D a,b; segment2D s; //clear the vector 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); initialize_segments_random(); print_segments(); //Rtimer rt1; //rt_start(rt1); //compute something here //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); /* init GL */ /* set background color black*/ glClearColor(0, 0, 0, 0); /* here we can enable depth testing and double buffering and so on */ /* give control to event handler */ glutMainLoop(); return 0; } /* ****************************** */ /* draw the segments stored in global variable segments */ void draw_segments(){ //set color glColor3fv(yellow); int i; for (i=0; i