unsigned measure_route(struct city** cities) { unsigned distance = 0; while (cities[1]) { unsigned leg = distance_to(cities[0], cities[1]->name); if (!leg) /* hit a dead end */ return 0; distance += leg; ++cities; } return distance; }
int fftMatch_projList(char *inFile, char *descFile) { FILE *fp = FOPEN(inFile, "r"); if (!fp) asfPrintError("Failed to open %s\n", inFile); char line[255], master[255]; float x_offs[255], y_offs[255]; int n=0; while (NULL != fgets(line, 255, fp)) { if (line[strlen(line)-1]=='\n') line[strlen(line)-1] = '\0'; if (line[0] == '#' || line[0] == '\0') continue; if (n==0) { strcpy(master, line); } else { float certainty; fftMatch_proj(master, line, &x_offs[n-1], &y_offs[n-1], &certainty); } ++n; if (n>=255) asfPrintError("Too many granules: max 255"); } FCLOSE(fp); FILE *fpd=NULL; if (descFile) { fpd = FOPEN(descFile, "w"); fprintf(fpd, "master,slave,offsetX,offsetY,total offsets\n"); } int num=n-1; n=0; char best[255]; double min_dist; fp = FOPEN(inFile, "r"); while (NULL != fgets(line, 255, fp)) { if (line[strlen(line)-1]=='\n') line[strlen(line)-1] = '\0'; if (line[0] == '#' || line[0] == '\0') continue; if (n==0) { min_dist = distance_to(-1, x_offs, y_offs, num); fprintf(fpd ? fpd : stdout, "%s,%s,%.5f,%.5f,%.5f\n", master, master, 0., 0., min_dist); strcpy(best, master); } else { double d = distance_to(n-1, x_offs, y_offs, num); fprintf(fpd ? fpd : stdout, "%s,%s,%.5f,%.5f,%.5f\n", master, line, x_offs[n-1], y_offs[n-1], d); if (d < min_dist) { min_dist = d; strcpy(best, line); } } ++n; } asfPrintStatus("Best is %s\n", best); if (descFile) { asfPrintStatus("Generated match file (%s)!\n", descFile); FCLOSE(fpd); } FCLOSE(fp); if (strcmp(master, best) == 0) { asfPrintStatus("Reference granule is already the best: %s\n", master); } else { char *new = appendExt(inFile, ".new"); fpd = FOPEN(new, "w"); fprintf(fpd,"%s\n", best); fp = FOPEN(inFile, "r"); while (NULL != fgets(line, 255, fp)) { if (line[strlen(line)-1]=='\n') line[strlen(line)-1] = '\0'; if (line[0] == '#' || line[0] == '\0') continue; if (strcmp(line, best) != 0) { fprintf(fpd,"%s\n", line); } } FCLOSE(fpd); FCLOSE(fp); } return 0; }
void calculate_diameter() { freopen("./diameter.txt", "w", stdout); // output file int diameter = 0; int current_max = 0; double average_max = 0; double average_average = 0; vector< vector<int> > distribution(V, vector<int> (0)); vector< vector<int> > max_path; vector<int> farthest_node; for (int i = 0; i < V; i++) { vector<int> distance_to(V, 0); vector<int> path(V,-1); usedbfs.clear(); usedbfs.resize(V, false); bfs(i, distance_to, path); current_max = 0; for (int j = 0; j < V; j++) { average_average += distance_to[j]; if (distance_to[j] > current_max) current_max = distance_to[j]; if (distance_to[j] > diameter) { diameter = distance_to[j]; farthest_node.clear(); max_path.clear(); max_path.push_back(path); farthest_node.push_back(j); } else if (distance_to[j] == diameter){ max_path.push_back(path); farthest_node.push_back(j); } } average_max += current_max; distribution[current_max].push_back(i); } average_max /= V; average_average /= V*V; cout << "Diameter: " << diameter << endl; cout << "Average path: " << average_average << endl; cout << "Average farthest path: " << average_max << endl; vector < vector<int> > farthest_path; for (int i = 0; i < farthest_node.size(); i++) { farthest_path.push_back(vector<int>()); for (int u = farthest_node[i]; u != -1; u = max_path[i][u]) farthest_path[i].push_back(u); } cout << "Farthest paths:" << endl; for (int i = 0; i < farthest_path.size(); i++) { cout << i+1 << ": "; for (vector<int>::iterator j = farthest_path[i].begin(); j != farthest_path[i].end(); j++) cout << name[*j] << ". "; cout << endl; } int minimum = V; cout << "Farthest paths distribution:" << endl; for (int i = 0; i <= diameter; i++) { if (distribution[i].size() != 0 && i < minimum) minimum = i; cout << i << ": " << distribution[i].size() << endl; } cout << "Characters with minimum farthest path:" << endl; for (vector<int>::iterator i = distribution[minimum].begin(); i != distribution[minimum].end(); i++) { cout << name[*i] << endl; } }
// Called from the constructor. // Set the initial bed shape from a list of points. // Deduce the bed shape type(rect, circle, custom) // This routine shall be smart enough if the user messes up // with the list of points in the ini file directly. void BedShapePanel::set_shape(ConfigOptionPoints* points) { auto polygon = Polygon::new_scale(points->values); // is this a rectangle ? if (points->size() == 4) { auto lines = polygon.lines(); if (lines[0].parallel_to(lines[2]) && lines[1].parallel_to(lines[3])) { // okay, it's a rectangle // find origin // the || 0 hack prevents "-0" which might confuse the user int x_min, x_max, y_min, y_max; x_max = x_min = points->values[0].x; y_max = y_min = points->values[0].y; for (auto pt : points->values){ if (x_min > pt.x) x_min = pt.x; if (x_max < pt.x) x_max = pt.x; if (y_min > pt.y) y_min = pt.y; if (y_max < pt.y) y_max = pt.y; } if (x_min < 0) x_min = 0; if (x_max < 0) x_max = 0; if (y_min < 0) y_min = 0; if (y_max < 0) y_max = 0; auto origin = new ConfigOptionPoints{ Pointf(-x_min, -y_min) }; m_shape_options_book->SetSelection(SHAPE_RECTANGULAR); auto optgroup = m_optgroups[SHAPE_RECTANGULAR]; optgroup->set_value("rect_size", new ConfigOptionPoints{ Pointf(x_max - x_min, y_max - y_min) });//[x_max - x_min, y_max - y_min]); optgroup->set_value("rect_origin", origin); update_shape(); return; } } // is this a circle ? { // Analyze the array of points.Do they reside on a circle ? auto center = polygon.bounding_box().center(); std::vector<double> vertex_distances; double avg_dist = 0; for (auto pt: polygon.points) { double distance = center.distance_to(pt); vertex_distances.push_back(distance); avg_dist += distance; } bool defined_value = true; for (auto el: vertex_distances) { if (abs(el - avg_dist) > 10 * SCALED_EPSILON) defined_value = false; break; } if (defined_value) { // all vertices are equidistant to center m_shape_options_book->SetSelection(SHAPE_CIRCULAR); auto optgroup = m_optgroups[SHAPE_CIRCULAR]; boost::any ret = wxNumberFormatter::ToString(unscale(avg_dist * 2), 0); optgroup->set_value("diameter", ret); update_shape(); return; } } if (points->size() < 3) { // Invalid polygon.Revert to default bed dimensions. m_shape_options_book->SetSelection(SHAPE_RECTANGULAR); auto optgroup = m_optgroups[SHAPE_RECTANGULAR]; optgroup->set_value("rect_size", new ConfigOptionPoints{ Pointf(200, 200) }); optgroup->set_value("rect_origin", new ConfigOptionPoints{ Pointf(0, 0) }); update_shape(); return; } // This is a custom bed shape, use the polygon provided. m_shape_options_book->SetSelection(SHAPE_CUSTOM); // Copy the polygon to the canvas, make a copy of the array. m_canvas->m_bed_shape = points->values; update_shape(); }