/* * Chris' implementation of Graham's Scan * Copyright (C) 2003 Chris Harrison * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include #include #include "SDL.h" #include "SDL_gfxPrimitives.h" //--------------------POINT DATA STRUCTURE--------------------------- struct point { double x; //X POSITION double y; //Y POSITION }; //--------------------GLOBAL VARIABLES--------------------------- int NumPoints = 15; // n<1000 point points[1000]; //GLOBAL POINT ARRAY int usedPoints[1000]; //GLOBAL LIST OF POINTS IN CONVEX HULL int minPoint, maxPoint, currPoint; //GLOBAL VARIABLES SDL_Surface *screen; //GLOBAL SDL GRAPHICS SURFACE POINTER //--------------------JARVIS' MARCH FUNCTIONS--------------------------- void jarvisMain(); //RANDOMLY GENERATES POINTS, RUNS JARVIS' MARCH, AND DISPLAYS RESULT double findAngle(double x1, double y1, double x2, double y2); //FIND ANGLE GIVEN TWO POINTS bool notUsed(int y); //CHECK IF POINT INDEX HAS ALREADY BEEN ADDED TO THE CONVEX HULL void addUsedPoint(int index); //ADD POINT TO CONVEX HULL void jarvis(); //FINDS MIN POINT, AND USES GLOBAL POINT ARRAY TO COMPUTER CONVEX HULL //--------------------AUXILARY GRAPHICS FUNCTIONS--------------------------- void initScreen(); //SETUP THE GRAPHICS SURFACE AND WINDOW void drawPoints(); //DRAW POINTS FROM GLOBAL DOUBLELY LINKED LIST void drawInnerLine(point A, point B); //DRAW A LINE BETWEEN TWO POINTS void drawPermeter(); //DRAWS COMPLETED PERIMETER (CONVEX HULL) void graphicsLoop(); //MAIN GRAPHICS LOOP //--------------------MAIN--------------------------- int main(int argc, char *argv[]) { initScreen(); //INITIALIZE THE GRAPHICS WINDOW srand(time(NULL)); //SEED THE RANDOM NUMBER GENERATER WITH THE TIME jarvisMain(); //EXECUTE JARVIS' MARCH graphicsLoop(); //WAIT UNTIL USER EXITS return 0; //EXIT } void jarvisMain() { for (int i=0;ipoints[maxPoint].y) maxPoint=k; //cout<<"Max: "<findAngle(points[currPoint].x,points[currPoint].y,points[y].x,points[y].y) && (notUsed(y) || y==maxPoint) && findAngle(points[currPoint].x,points[currPoint].y,points[y].x,points[y].y)>=90) { minAngle=y; } } currPoint=minAngle; //cout<<"Selected Point: "<=0 && deltaY>=0) angle=90+angle; else if (deltaX>=0 && deltaY<0) angle=90+angle; else if (deltaX<0 && deltaY>0) angle=270+angle; else if (deltaX<0 && deltaY<=0) angle=270+angle; return angle; } bool notUsed(int y) { for (int i=0;i