# include # include # include # include # include "tsp_display.h" /******************************************************************************/ void tsp_display ( char *prefix, int n, double *x2 ) /******************************************************************************/ /* Purpose: tsp_display() plots cities and a tour connecting them. Discussion: If a "round trip" is desired, then the x2 array must repeat the first city at the end. Licensing: This code is distributed under the MIT license. Modified: 12 June 2026 Author: John Burkardt Input: char *prefix: a string that defines the name of the problem. It is also used to create file names needed for input to gnuplot. int n: the number of cities. double x[n*2]: the x and y coordinates of the cities. */ { char command_filename[80]; FILE *command; FILE *data; char data_filename[80]; int i; /* Create the data file. */ strcpy ( data_filename, prefix ); strcat ( data_filename, "_data.txt" ); data = fopen ( data_filename, "wt" ); for ( i = 0; i < n; i++ ) { fprintf ( data, "%14.6g %14.6g\n", x2[2*i], x2[2*i+1] ); } fclose ( data ); printf ( " Created data file '%s'\n", data_filename ); /* Create the command file. */ strcpy ( command_filename, prefix ); strcat ( command_filename, "_commands.txt" ); command = fopen ( command_filename, "wt" ); fprintf ( command, "# %s\n", command_filename ); fprintf ( command, "#\n" ); fprintf ( command, "# Usage:\n" ); fprintf ( command, "# gnuplot < %s\n", command_filename ); fprintf ( command, "#\n" ); fprintf ( command, "set term png\n" ); fprintf ( command, "set output '%s.png'\n", prefix ); fprintf ( command, "set xlabel '<--- X --->'\n" ); fprintf ( command, "set ylabel '<--- Y --->'\n" ); fprintf ( command, "set title '%s'\n", prefix ); fprintf ( command, "set grid\n" ); fprintf ( command, "unset key\n" ); fprintf ( command, "set style data lines\n" ); fprintf ( command, "plot '%s' using 1:2 lw 3 linecolor rgb 'blue', \\\n", data_filename ); fprintf ( command, " '%s' using 1:2 with points pointtype 7 pointsize 3 linecolor rgb 'green'\n", data_filename ); fprintf ( command, "quit\n" ); fclose ( command ); printf ( " Created command file '%s'\n", command_filename ); return; }