int main(void) { struct Point quickHullPoints[MAX_POINTS]; int numberOfquickHullPoints=readPoints(quickHullPoints); if(numberOfquickHullPoints>0) { displayPoints(quickHullPoints,numberOfquickHullPoints); struct Point convexHullSet[numberOfquickHullPoints]; int numberOfConvexPoints=quickHull(quickHullPoints,convexHullSet,numberOfquickHullPoints); displayConvexPoints(convexHullSet,numberOfConvexPoints); } }
int main(){ vector<City> tsp; city_parser("input-test2.txt", tsp); FILE * o_f; o_f = fopen("output-test2.txt", "w"); City data_set[6] = City(); data_set[0] = City(1,15,3); data_set[1] = City(2,9,0); data_set[2] = City(3,2,8); data_set[3] = City(4,11,6); data_set[4] = City(5,2,2); data_set[5] = City(6,8,9); // for(int i = 0; i< tsp.size(); ++i) // tsp[i].Print("before"); vector<City> hull_points = quickHull(tsp); int remove_index = 0; for(int i =0; i <hull_points.size();i++){ remove_index = find(tsp.begin(), tsp.end(), hull_points[i]) - tsp.begin(); tsp.erase(tsp.begin() + remove_index); } // for(int i = 0; i< tsp.size(); ++i) // tsp[i].Print("after"); // for(int i =0; i <hull_points.size();++i) // hull_points[i].Print("tsp"); hull_sort(hull_points, 0); vector<Line> hull_lines = draw_lines(hull_points, tsp); // for(int i = 0; i <tsp.size();i++) // tsp[i].Print(); /* for(int i =0; i<hull_points.size();i++) hull_points[i].Print(); */ vector< pair<Line, vector<City> > > closest_lines = determine_closest_lines(hull_lines, tsp); // for(int i = 0; i < tsp.size(); i++) // closest_lines[i].second.push_back(tsp[i]); for(int i =0; i < closest_lines.size(); i++){ int *lengths = new int[closest_lines[i].second.size()]; for(int j=0; j<closest_lines[i].second.size();j++){ Vec2 temp = closest_lines[i].first.points().Unit(); lengths[j] = closest_lines[i].second[j].point().Dot(temp); } quickCitySort(closest_lines[i].second, lengths, 0, closest_lines[i].second.size()-1); } // for(int i =0; i<closest_lines.size();i++){ // hull_points[i].Print(); // for(int j=0; j <closest_lines[i].second.size(); j++){ // closest_lines[i].second[j].Print(); // } // } // compute our walk. float tot_len = 0; vector<City> walk; for(int i = 0; i < closest_lines.size();++i){ walk.push_back(hull_points[i]); for(int j = 0; j < closest_lines[i].second.size(); ++j){ walk.push_back(closest_lines[i].second[j]); } } //compute total length Vec2 temp; float tempy; for(int i =0; i < walk.size(); i++){ float a0 = walk[i].point().x(); float b0 = walk[i-1].point().x(); float a1 = walk[i].point().y(); float b1 = walk[i-1].point().y(); float dx = a0 - b0; float dy = a1 - b1; tempy = rint(sqrt(dx*dx + dy*dy)); tot_len += tempy; } //print solutionte total length fprintf(o_f, " %i\n",int(tot_len)); for(int i =0; i < walk.size(); i++){ walk[i].Print("",o_f); } // cout << "total path length is: " << tot_len <<" units of space" <<endl; return 0; }
int main(int argc, char** argv) { // Parse cmd line args. int pointCount = 0; char* outSVG = 0; if(argc == 2) { pointCount = atoi(argv[1]); outSVG = 0; } else if (argc == 3) { pointCount = atoi(argv[1]); outSVG = argv[2]; } else { printf("usage: quickhull <points> [out.svg]\n"); exit(1); } // Initialise the vector to hold the hull. Vector* hull = vector_new(pointCount); // Use random points for test data. Vector* points = vector_new(pointCount); double originX = 300; double originY = 300; long maxDist = 250; srandom(170); for (int i = 0; i < pointCount; i++) { double r = (random() % (maxDist * 2)) - maxDist; double a = M_PI * (random() % 360) / 360; vector_append ( points , originX + r * cos (a) , originY + r * sin (a)); } // Timing setup struct timeval start, finish; struct rusage start_ru, finish_ru; gettimeofday( &start, NULL ); getrusage( RUSAGE_SELF, &start_ru ); // Do the deed. int depth = quickHull (points, hull); // Print how long it took. gettimeofday( &finish, NULL ); getrusage( RUSAGE_SELF, &finish_ru ); // printf("depth = %d\n", depth); // printf("points on hull = %d\n", hull->length); sub_timeval( &finish, &start ); sub_timeval( &finish_ru.ru_utime, &start_ru.ru_utime ); sub_timeval( &finish_ru.ru_stime, &start_ru.ru_stime ); add_timeval( &finish_ru.ru_utime, &finish_ru.ru_stime ); printf("elapsedTimeMS = "); print_timeval( &finish ); putchar( '\n' ); printf("cpuTimeMS = "); print_timeval( &finish_ru.ru_utime); putchar( '\n' ); // Write output to file if requested. if(outSVG != 0) { FILE* file = fopen(outSVG, "w"); dumpSVG (file, points, hull); fclose (file); } }