//MAKEFILE # To use this Makefile, just type make. That's it. CC = gcc LDLIBS = -L /usr/X11R6/lib -lGL -lGLU -lglut -lXmu -lX11 -lXi -lm INCLUDE = -I /usr/X11R6/include -I /usr/X11R6/include/GL executable: $(CC) -g $(INCLUDE) -o $I benchmark $I benchmark.c $(LDLIBS) -Wall ------------------------------------------------------------------------- //benchmark.h /*type definitions*/ typedef GLfloat xyPoint[2]; typedef int xyPointInt[2]; typedef GLdouble xyzPoint[3]; typedef GLfloat color3f[3]; typedef GLfloat color4f[4]; ------------------------------------------------------------------------- //benchmark.c /*************************************************************** * This program allows two people to play checkers interactively **************************************************************/ #include #include #include #include #include #include #include #include "benchmark.h" #define NONE 0 #define ONE 1 #define BOTH 2 #define DONT_DRAW 0 #define DRAW 1 #define MENU_QUIT 0 #define MENU_RESET_CORNERS 1 #define NUM_OF_LINES 1000 #define UNCLIPPED 0 #define CSALGO 1 #define LBALGO 2 #define PERFORMANCE 3 #define BC_TOP 1 #define BC_LEFT 8 #define BC_RIGHT 4 #define BC_BOTTOM 2 int cornersSet = NONE; int displayWhich = UNCLIPPED; xyPoint randomStartPoints[NUM_OF_LINES]; xyPoint randomEndPoints[NUM_OF_LINES]; xyPoint CSclippedStartPoints[NUM_OF_LINES]; xyPoint CSclippedEndPoints[NUM_OF_LINES]; int drawMe[NUM_OF_LINES]; xyPoint LBclippedStartPoints[NUM_OF_LINES]; xyPoint LBclippedEndPoints[NUM_OF_LINES]; xyPoint topLeft; xyPoint botRight; float UNtime, CStime, LBtime; /*function definition*/ void setWindowVals(xyPoint windowSize, xyPoint windowLoc, char * windowName); void initSystem(color4f color, color3f drawColor, xyPoint startDisplay, xyPoint endDisplay, float ptSize); void defineMenu(); void keyboardReader(unsigned char key, int x, int y); void mouseReader(int button, int state, int x, int y); void menu_function(); void textWriter (char * text, int posX, int posY); void defineLists(); void runBenchmark(); void CSBenchmark(); void LBBenchmark(); int CSclippit(int index); int LBclippit(int index); int calculateRegion(int x, int y); /*int main(int argc, char **argv) * This function initializes the board, glut, the window, the GL system * the menu, the gl lists, and then calls the glutMainLoop*/ int main(int argc, char **argv){ xyPoint startDisplay = {0.0,0.0};//location to start the display at xyPoint endDisplay = {500.0,500.0};//location to finish the display at color4f winBGColor = {0.6, 0.6, 0.6, 1.0};//window background color color3f drawColor = {1.0, 1.0, 1.0};//color we are drawing in xyPoint windowSize = {500, 500};//size of the window xyPoint windowLoc = {0, 0};//location on screen of the window float ptsize = 0.5;//the point size char * windowName = "Benchmark";//name of the window /*initiate interaction with the local window system*/ glutInit(&argc, argv); /*initialize the window variables*/ setWindowVals(windowSize, windowLoc, windowName); /*initialize the system*/ initSystem(winBGColor, drawColor, startDisplay, endDisplay, ptsize); defineMenu(); glutMainLoop(); } /*void display() * This is the display function. It handles all drawing to the screen*/ void display(){ char buffer[25]; int counter = 0; glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); if(displayWhich == PERFORMANCE){ snprintf(buffer, 25, "%f sec\n", CStime); textWriter("Cohen Sutherland\n", -200, 208); textWriter(buffer, -200, 192); textWriter("4 Additions\n", -200, 176); textWriter("6 Subtractions\n", -200, 160); textWriter("4 Multiplications\n", -200, 144); textWriter("4 Divisions\n", -200, 128); textWriter("(Operation counts are for this implementation only)\n", -200, 106); snprintf(buffer, 25, "%f sec\n", LBtime); textWriter("Liang Barsky\n", 50, 208); textWriter(buffer, 50, 192); textWriter("4 Additions\n", 50, 176); textWriter("4 Subtractions\n", 50, 160); textWriter("4 Multiplications\n", 50, 144); textWriter("4 Divisions\n", 50, 128); } else{ /*print out instructions*/ if(cornersSet == NONE){ glColor3f(1,1,1); textWriter("Click where you would like the top left corner\n", -197, 235); }else if(cornersSet == ONE){ glColor3f(1,1,1); textWriter("Click where you would like the bottom right corner\n", -200, 235); }else if(cornersSet == BOTH){ /*draw all the lines*/ glColor3f(1,0,1); for(counter = 0; counter < NUM_OF_LINES; counter++){ /*display unclipped lines*/ if(displayWhich == UNCLIPPED){ glBegin(GL_LINES); glVertex2f(randomStartPoints[counter][0], randomStartPoints[counter][1]); glVertex2f(randomEndPoints[counter][0], randomEndPoints[counter][1]); glEnd(); } /*display cohen sutherland lines*/ if(displayWhich == CSALGO){ if(drawMe[counter] == DRAW){ glBegin(GL_LINES); glVertex2f(CSclippedStartPoints[counter][0], CSclippedStartPoints[counter][1]); glVertex2f(CSclippedEndPoints[counter][0], CSclippedEndPoints[counter][1]); glEnd(); } } /*display liang barsky*/ if(displayWhich == LBALGO){ if(drawMe[counter] == DRAW){ glBegin(GL_LINES); glVertex2f(LBclippedStartPoints[counter][0], LBclippedStartPoints[counter][1]); glVertex2f(LBclippedEndPoints[counter][0], LBclippedEndPoints[counter][1]); glEnd(); } } } /*draw the clipping rectangle*/ glColor3f(0,0,0); glBegin(GL_LINE_LOOP); glVertex2f(topLeft[0], topLeft[1]); glVertex2f(topLeft[0], botRight[1]); glVertex2f(botRight[0], botRight[1]); glVertex2f(botRight[0], topLeft[1]); glEnd(); } } /*swap the buffers*/ glutSwapBuffers(); } /*Function textWriter (char * text) * This function writes a null terminated character string to the screen * starting at position X, Y*/ void textWriter (char * text, int posX, int posY){ int counter = 0; /*set the position for the text*/ glRasterPos2i(posX, posY); while ((char)text[counter] != '\n') { glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text[counter]); counter++; } } /* initSystem(color4f winColor, color3f drawColor, xyPoint startDisp, xyPoint endDisp, float ptSize) * This function initializes all of display variables*/ void initSystem(color4f winColor, color3f drawColor, xyPoint startDisp, xyPoint endDisp, float ptSize){ /*set the background color*/ glClearColor(winColor[0], winColor[1], winColor[2], winColor[3]); /*clear the window*/ glClear(GL_COLOR_BUFFER_BIT); /*set the drawing color*/ glColor3f(drawColor[0], drawColor[1], drawColor[2]); glMatrixMode(GL_PROJECTION); glLoadIdentity(); /*set the portion of the world to display*/ gluOrtho2D(-250.0, 250.0, -250.0, 250.0); glMatrixMode(GL_MODELVIEW); /*set the point size*/ glPointSize(ptSize); /*set the callback functions*/ glutDisplayFunc(display); glutKeyboardFunc(keyboardReader); glutMouseFunc(mouseReader); } /*setWindowVals(xyPoint size, xyPoint pos, char * name) * Set the variables for the window*/ void setWindowVals(xyPoint size, xyPoint pos, char * name){ /*set number and type of buffers*/ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); /*set the window size*/ glutInitWindowSize(size[0], size[1]); /*position the window*/ glutInitWindowPosition(pos[0], pos[1]); /*actually create the window*/ glutCreateWindow(name); } /*void keyboardReader(unsigned char key, int x, int y) * this function handles all keyboard interaction*/ void keyboardReader(unsigned char key, int x, int y){ if ((key == 'Q') || (key == 'q')){ exit(1); } } /*void mouseReader(int button, int state, int x, int y) * This function handles all mouse interaction*/ void mouseReader(int button, int state, int x, int y){ int counter = 0; if ((state == GLUT_DOWN) && (button == GLUT_LEFT_BUTTON)){ /*set the first point*/ if(cornersSet == NONE){ topLeft[0] = x - 250; topLeft[1] = 250 - y; cornersSet = ONE; } /*set the second point and then create the random array*/ else if(cornersSet == ONE){ botRight[0] = x - 250; botRight[1] = 250 - y; cornersSet = BOTH; displayWhich = UNCLIPPED; for(counter = 0; counter < NUM_OF_LINES; counter++){ /*create the random start and end points*/ randomStartPoints[counter][0] = (rand() % 500) - 250; randomStartPoints[counter][1] = (rand() % 500) - 250; randomEndPoints[counter][0] = (rand() % 500) - 250; randomEndPoints[counter][1] = (rand() % 500) - 250; /*fill the cohen sutherland array*/ CSclippedStartPoints[counter][0] = randomStartPoints[counter][0]; CSclippedStartPoints[counter][1] = randomStartPoints[counter][1]; CSclippedEndPoints[counter][0] = randomEndPoints[counter][0]; CSclippedEndPoints[counter][1] = randomEndPoints[counter][1]; /*fill the liang barsky array*/ LBclippedStartPoints[counter][0] = randomStartPoints[counter][0]; LBclippedStartPoints[counter][1] = randomStartPoints[counter][1]; LBclippedEndPoints[counter][0] = randomEndPoints[counter][0]; LBclippedEndPoints[counter][1] = randomEndPoints[counter][1]; drawMe[counter] = DRAW; } runBenchmark(); } } glutPostRedisplay(); } /*Function defineMenu() * This function defines the main menu for the program and attaches * values to each of the choices*/ void defineMenu(){ glutCreateMenu(menu_function); glutAddMenuEntry("Reset Corners", MENU_RESET_CORNERS); glutAddMenuEntry("Quit", MENU_QUIT); glutAttachMenu(GLUT_RIGHT_BUTTON); } /*Function menu_function(int ID) * This is the callback function for the menu. It handles all menu * interaction*/ void menu_function(int ID){ switch(ID){ case MENU_QUIT: { exit(1); } case MENU_RESET_CORNERS:{ displayWhich = UNCLIPPED; cornersSet = NONE; topLeft[0] = 0; topLeft[1] = 0; botRight[0] = 0; botRight[1] = 0; glutPostRedisplay(); break; } } } /* Functionvoid runBenchmark() * This function runs the two benchmark programs and times them. It also * controls the display*/ void runBenchmark(){ int counter = 0; long secDiff, usecDiff; struct timeval start; struct timeval end; float temp; displayWhich = UNCLIPPED; gettimeofday(&start, NULL); display(); gettimeofday(&end, NULL); secDiff = end.tv_sec - start.tv_sec; usecDiff = end.tv_usec - start.tv_usec; if(usecDiff < 0){ usecDiff = usecDiff + 1000000; secDiff--; } sleep(3); /*prepare the system for the cohen sutherland algorithm*/ for(counter = 0; counter < NUM_OF_LINES; counter++){ drawMe[counter] = DRAW; } displayWhich = CSALGO; /*start the timer and run the benchmark*/ gettimeofday(&start, NULL); CSBenchmark(); display(); gettimeofday(&end, NULL); secDiff = end.tv_sec - start.tv_sec; usecDiff = end.tv_usec - start.tv_usec; if(usecDiff < 0){ usecDiff = usecDiff + 1000000; secDiff--; } temp = usecDiff; temp = temp/1000000; CStime = secDiff + temp; sleep(3); /*prepare the system for to show unclipped*/ for(counter = 0; counter < NUM_OF_LINES; counter++){ drawMe[counter] = DRAW; } displayWhich = UNCLIPPED; /*start the timer and run the benchmark*/ gettimeofday(&start, NULL); display(); gettimeofday(&end, NULL); secDiff = end.tv_sec - start.tv_sec; usecDiff = end.tv_usec - start.tv_usec; if(usecDiff < 0){ usecDiff = usecDiff + 1000000; secDiff--; } temp = usecDiff; temp = temp/1000000; UNtime = secDiff + temp; sleep(3); /*prepare the system for the liang barsky algorithm*/ for(counter = 0; counter < NUM_OF_LINES; counter++){ drawMe[counter] = DRAW; } displayWhich = LBALGO; /*start the timer and run the benchmark*/ gettimeofday(&start, NULL); LBBenchmark(); display(); gettimeofday(&end, NULL); secDiff = end.tv_sec - start.tv_sec; usecDiff = end.tv_usec - start.tv_usec; if(usecDiff < 0){ usecDiff = usecDiff + 1000000; secDiff--; } temp = usecDiff; temp = temp/1000000; LBtime = secDiff + temp; sleep(3); displayWhich = PERFORMANCE; } /* Function void LBBenchmark() * This runs the entire Liang-Barsky clipping algorithm*/ void LBBenchmark(){ int counter = 0; /*figure out what region the start point is in*/ for(counter = 0; counter < NUM_OF_LINES; counter++){ drawMe[counter] = LBclippit(counter); } } /* Function void CSBenchmark() * This runs the entire Cohen-Sutherland clipping algorithm*/ void CSBenchmark(){ int counter = 0; /*figure out what region the start point is in*/ for(counter = 0; counter < NUM_OF_LINES; counter++){ drawMe[counter] = CSclippit(counter); } } /* Function int clippit(int index) * This functions figures out where the lines are, and clips as necessary*/ int LBclippit(int index){ float minimum = 0; float maximum = 1; float left, right, top, bottom; int dx, dy; /*calculate change in x and change in y directions*/ dx = LBclippedEndPoints[index][0] - LBclippedStartPoints[index][0]; dy = LBclippedEndPoints[index][1] - LBclippedStartPoints[index][1]; /*vertical line*/ if(dx == 0){ /*see if the line is in the drawing area*/ if(LBclippedEndPoints[index][0] < topLeft[0]){ return DONT_DRAW; } if(LBclippedEndPoints[index][0] > botRight[0]){ return DONT_DRAW; } /*if they show, calculate them*/ if(LBclippedStartPoints[index][0] == LBclippedEndPoints[index][0]){ /*if the point is too far up, make it the top side*/ if(LBclippedStartPoints[index][1] > topLeft[1]){ LBclippedStartPoints[index][1] = topLeft[1]; /*if the point is too far down, make it the bottom*/ }else if(LBclippedStartPoints[index][1] < botRight[1]){ LBclippedStartPoints[index][1] = botRight[1]; } /*if the point is too far up, make it the top side*/ if(LBclippedEndPoints[index][1] > topLeft[1]){ LBclippedEndPoints[index][1] = topLeft[1]; /*if the point is too far down, make it the bottom*/ }else if(LBclippedEndPoints[index][1] < botRight[1]){ LBclippedEndPoints[index][1] = botRight[1]; } /*if it's the same point, then the line was outside drawing area*/ if(LBclippedEndPoints[index][1] != LBclippedStartPoints[index][1]){ return DRAW; } else{ return DONT_DRAW; } } } /*horizontal line*/ if(dy == 0){ /*see if the line is in the drawing area*/ if(LBclippedEndPoints[index][1] > topLeft[1]){ return DONT_DRAW; } if(LBclippedEndPoints[index][1] < botRight[1]){ return DONT_DRAW; } /*if they show, calculate them*/ if(LBclippedStartPoints[index][1] == LBclippedEndPoints[index][1]){ /*if the point is too far left, make it the left side*/ if(LBclippedStartPoints[index][0] < topLeft[0]){ LBclippedStartPoints[index][0] = topLeft[0]; /*if the point is too far right, make it the right side*/ }else if(LBclippedStartPoints[index][0] > botRight[0]){ LBclippedStartPoints[index][0] = botRight[0]; } /*if the point is too far left, make it the left side*/ if(LBclippedEndPoints[index][0] < topLeft[0]){ LBclippedEndPoints[index][0] = topLeft[0]; /*if the point is too far right, make it the right side*/ }else if(LBclippedEndPoints[index][0] > botRight[0]){ LBclippedEndPoints[index][0] = botRight[0]; } /*if it's the same point, then the line was outside drawing area*/ if(LBclippedEndPoints[index][0] != LBclippedStartPoints[index][0]){ return DRAW; } else{ return DONT_DRAW; } } } /*calculate the side values*/ left = (topLeft[0] - LBclippedStartPoints[index][0])/dx; right = (botRight[0] - LBclippedStartPoints[index][0])/dx; top = (topLeft[1] - LBclippedStartPoints[index][1])/dy; bottom = (botRight[1] - LBclippedStartPoints[index][1])/dy; /*ignore if intersection isn't part of the line*/ if((left >= minimum) && (left <= maximum)){ /*check if the line is entering or exiting the intersection*/ if(dx > 0){ /*entering*/ minimum = left; } else if (dx < 0){ /*exiting*/ maximum = left; } } /*ignore if intersection isn't part of the line*/ if((right >= minimum) && (right <= maximum)){ /*check if the line is entering or exiting the intersection*/ if(dx < 0){ /*entering*/ minimum = right; } else if (dx > 0){ /*exiting*/ maximum = right; } } /*ignore if intersection isn't part of the line*/ if((top >= minimum) && (top <= maximum)){ /*check if the line is entering or exiting the intersection*/ if(dy < 0){ /*entering*/ minimum = top; } else if (dy > 0){ /*exiting*/ maximum = top; } } /*ignore if intersection isn't part of the line*/ if((bottom >= minimum) && (bottom <= maximum)){ /*check if the line is entering or exiting the intersection*/ if(dy > 0){ /*entering*/ minimum = bottom; } else if (dy < 0){ /*exiting*/ maximum = bottom; } } /*if max is less than min, don't draw*/ if(minimum < maximum){ /*if max has changed, recalculate the point*/ if(maximum < 1){ LBclippedEndPoints[index][0] = LBclippedStartPoints[index][0] + dx * maximum; LBclippedEndPoints[index][1] = LBclippedStartPoints[index][1] + dy * maximum; } /*if min has changed, recalculate the point*/ if(minimum > 0){ LBclippedStartPoints[index][0] = LBclippedStartPoints[index][0] + dx * minimum; LBclippedStartPoints[index][1] = LBclippedStartPoints[index][1] + dy * minimum; } /*compensate for rounding errors*/ /*round left*/ if((LBclippedStartPoints[index][0] < topLeft[0]) && (LBclippedStartPoints[index][0] > (topLeft[0] - 1))){ LBclippedStartPoints[index][0] = topLeft[0]; } /*round right*/ if((LBclippedStartPoints[index][0] > botRight[0]) && (LBclippedStartPoints[index][0] < (botRight[0] + 1))){ LBclippedStartPoints[index][0] = botRight[0]; } /*round top*/ if((LBclippedStartPoints[index][1] > topLeft[1]) && (LBclippedStartPoints[index][1] < (topLeft[1] + 1))){ LBclippedStartPoints[index][1] = topLeft[1]; } /*round bottom*/ if((LBclippedStartPoints[index][1] < botRight[1]) && (LBclippedStartPoints[index][1] > (botRight[1] - 1))){ LBclippedStartPoints[index][1] = botRight[1]; } /*round left*/ if((LBclippedEndPoints[index][0] < topLeft[0]) && (LBclippedEndPoints[index][0] > (topLeft[0] - 1))){ LBclippedEndPoints[index][0] = topLeft[0]; } /*round right*/ if((LBclippedEndPoints[index][0] > botRight[0]) && (LBclippedEndPoints[index][0] < (botRight[0] + 1))){ LBclippedEndPoints[index][0] = botRight[0]; } /*round top*/ if((LBclippedEndPoints[index][1] > topLeft[1]) && (LBclippedEndPoints[index][1] < (topLeft[1] + 1))){ LBclippedEndPoints[index][1] = topLeft[1]; } /*round bottom*/ if((LBclippedEndPoints[index][1] < botRight[1]) && (LBclippedEndPoints[index][1] > (botRight[1] - 1))){ LBclippedEndPoints[index][1] = botRight[1]; } /*make sure the line is in the box*/ /*check endpoint X value*/ if(LBclippedEndPoints[index][0] >= topLeft[0] && LBclippedEndPoints[index][0] <= botRight[0]){ /*check endpoint Y value*/ if(LBclippedEndPoints[index][1] <= topLeft[1] && LBclippedEndPoints[index][1] >= botRight[1]){ /*check startpoint X value*/ if(LBclippedStartPoints[index][0] >= topLeft[0] && LBclippedStartPoints[index][0] <= botRight[0]){ /*check startpoint Y value*/ if(LBclippedStartPoints[index][1] <= topLeft[1] && LBclippedStartPoints[index][1] >= botRight[1]){ return DRAW; } } } } } return DONT_DRAW; } /* Function int clippit(int index) * This functions figures out where the lines are, and clips as necessary*/ int CSclippit(int index){ int startRegion, endRegion; int rise, run; int x, y; /*calculate the regions that the points are in*/ startRegion = calculateRegion(CSclippedStartPoints[index][0], CSclippedStartPoints[index][1]); endRegion = calculateRegion(CSclippedEndPoints[index][0], CSclippedEndPoints[index][1]); /*the line doesn't pass through the box. get rid of it.*/ if((startRegion & endRegion) != 0){ return DONT_DRAW; } /*both ends are inside the box. accept it*/ if((startRegion | endRegion) == 0){ return DRAW; } /*now we deal with vertical lines*/ if(CSclippedStartPoints[index][0] == CSclippedEndPoints[index][0]){ /*if the point is too far up, make it the top side*/ if(CSclippedStartPoints[index][1] > topLeft[1]){ CSclippedStartPoints[index][1] = topLeft[1]; /*if the point is too far down, make it the bottom*/ }else if(CSclippedStartPoints[index][1] < botRight[1]){ CSclippedStartPoints[index][1] = botRight[1]; } /*if the point is too far up, make it the top side*/ if(CSclippedEndPoints[index][1] > topLeft[1]){ CSclippedEndPoints[index][1] = topLeft[1]; /*if the point is too far down, make it the bottom*/ }else if(CSclippedEndPoints[index][1] < botRight[1]){ CSclippedEndPoints[index][1] = botRight[1]; } return DRAW; } /*now we deal with horizontal lines*/ if(CSclippedStartPoints[index][1] == CSclippedEndPoints[index][1]){ /*if the point is too far left, make it the left side*/ if(CSclippedStartPoints[index][0] < topLeft[0]){ CSclippedStartPoints[index][0] = topLeft[0]; /*if the point is too far right, make it the right side*/ }else if(CSclippedStartPoints[index][0] > botRight[0]){ CSclippedStartPoints[index][0] = botRight[0]; } /*if the point is too far left, make it the left side*/ if(CSclippedEndPoints[index][0] < topLeft[0]){ CSclippedEndPoints[index][0] = topLeft[0]; /*if the point is too far right, make it the right side*/ }else if(CSclippedEndPoints[index][0] > botRight[0]){ CSclippedEndPoints[index][0] = botRight[0]; } return DRAW; } /*do the clipping for the top*/ if((startRegion & BC_TOP) == BC_TOP){ /*calculate the slope*/ rise = (CSclippedEndPoints[index][1] - CSclippedStartPoints[index][1]); run = (CSclippedEndPoints[index][0] - CSclippedStartPoints[index][0]); /*calculate the new points*/ x = CSclippedStartPoints[index][0] + (topLeft[1] - CSclippedStartPoints[index][1])*run/rise; y = topLeft[1]; /*Save the new points*/ CSclippedStartPoints[index][0] = x; CSclippedStartPoints[index][1] = y; return CSclippit(index); /*do the clipping for the left side*/ }else if((startRegion & BC_LEFT) == BC_LEFT){ /*calculate the slope*/ rise = (CSclippedEndPoints[index][1] - CSclippedStartPoints[index][1]); run = (CSclippedEndPoints[index][0] - CSclippedStartPoints[index][0]); /*calculate the new points*/ x = topLeft[0]; y = CSclippedStartPoints[index][1] + rise*(topLeft[0] - CSclippedStartPoints[index][0])/run; /*Save the new points*/ CSclippedStartPoints[index][0] = x; CSclippedStartPoints[index][1] = y; return CSclippit(index); /*do the clipping for the bottom*/ }else if((startRegion & BC_BOTTOM) == BC_BOTTOM){ /*calculate the slope*/ rise = (CSclippedEndPoints[index][1] - CSclippedStartPoints[index][1]); run = (CSclippedEndPoints[index][0] - CSclippedStartPoints[index][0]); /*calculate the new points*/ x = CSclippedStartPoints[index][0] + (botRight[1] - CSclippedStartPoints[index][1])*run/rise; y = botRight[1]; /*Save the new points*/ CSclippedStartPoints[index][0] = x; CSclippedStartPoints[index][1] = y; return CSclippit(index); /*do the clipping for the right*/ }else if((startRegion & BC_RIGHT) == BC_RIGHT){ /*calculate the slope*/ rise = (CSclippedEndPoints[index][1] - CSclippedStartPoints[index][1]); run = (CSclippedEndPoints[index][0] - CSclippedStartPoints[index][0]); /*calculate the new points*/ x = botRight[0]; y = CSclippedStartPoints[index][1] + rise*(botRight[0] - CSclippedStartPoints[index][0])/run; /*Save the new points*/ CSclippedStartPoints[index][0] = x; CSclippedStartPoints[index][1] = y; return CSclippit(index); } if((endRegion & BC_TOP) == BC_TOP){ /*calculate the slope*/ rise = (CSclippedEndPoints[index][1] - CSclippedStartPoints[index][1]); run = (CSclippedEndPoints[index][0] - CSclippedStartPoints[index][0]); /*calculate the new points*/ x = CSclippedEndPoints[index][0] + (topLeft[1] - CSclippedEndPoints[index][1])*run/rise; y = topLeft[1]; /*Save the new points*/ CSclippedEndPoints[index][0] = x; CSclippedEndPoints[index][1] = y; return CSclippit(index); }else if((endRegion & BC_LEFT) == BC_LEFT){ /*calculate the slope*/ rise = (CSclippedEndPoints[index][1] - CSclippedStartPoints[index][1]); run = (CSclippedEndPoints[index][0] - CSclippedStartPoints[index][0]); /*calculate the new points*/ x = topLeft[0]; y = CSclippedEndPoints[index][1] + rise*(topLeft[0] - CSclippedEndPoints[index][0])/run; /*Save the new points*/ CSclippedEndPoints[index][0] = x; CSclippedEndPoints[index][1] = y; return CSclippit(index); }else if((endRegion & BC_BOTTOM) == BC_BOTTOM){ /*calculate the slope*/ rise = (CSclippedEndPoints[index][1] - CSclippedStartPoints[index][1]); run = (CSclippedEndPoints[index][0] - CSclippedStartPoints[index][0]); /*calculate the new points*/ x = CSclippedEndPoints[index][0] + (botRight[1] - CSclippedEndPoints[index][1])*run/rise; y = botRight[1]; /*Save the new points*/ CSclippedEndPoints[index][0] = x; CSclippedEndPoints[index][1] = y; return CSclippit(index); }else if((endRegion & BC_RIGHT) == BC_RIGHT){ /*calculate the slope*/ rise = (CSclippedEndPoints[index][1] - CSclippedStartPoints[index][1]); run = (CSclippedEndPoints[index][0] - CSclippedStartPoints[index][0]); /*calculate the new points*/ x = botRight[0]; y = CSclippedEndPoints[index][1] + rise*(botRight[0] - CSclippedEndPoints[index][0])/run; /*Save the new points*/ CSclippedEndPoints[index][0] = x; CSclippedEndPoints[index][1] = y; return CSclippit(index); } } /* Function int calculateRegion(float x, float y) * Figure out which region the point is located in and return*/ int calculateRegion(int x, int y){ /*we're in the left third*/ if(x < topLeft[0]){ /*we're at the top third*/ if(y > topLeft[1]){ return BC_LEFT + BC_TOP; } /*we're at the bottom third*/ else if(y < botRight[1]){ return BC_LEFT + BC_BOTTOM; } /*we're in the middle*/ else { return BC_LEFT; } } /*we're in the right third*/ else if(x > botRight[0]){ /*we're at the top third*/ if(y > topLeft[1]){ return BC_RIGHT + BC_TOP; } /*we're at the bottom third*/ else if(y < botRight[1]){ return BC_RIGHT + BC_BOTTOM; } /*we're in the middle*/ else { return BC_RIGHT; } } /*we're in the center*/ else{ /*we're at the top third*/ if(y > topLeft[1]){ return BC_TOP; } /*we're at the bottom third*/ else if(y < botRight[1]){ return BC_BOTTOM; } /*we're in the middle*/ else { return 0; } } }