#!/usr/bin/env perl # Plot a tsp route. # Needs the "coords" file in the current directory. # Adam Sampson, use CGI; use CGI::Carp qw(fatalsToBrowser set_message); set_message("If the above message isn\'t helpful, contact Adam " . "at ats\@offog.org."); use GD; $query = new CGI; $in = $query->param('route'); die "you must specify a route" unless $in; open COORDS, "coords" or die "can't get coords"; %x = (); %y = (); %label = (); @ordered = (); $minx = $minx = 999; $maxx = $maxy = -999; while () { /^(.*) (\d+)(n|s)(\d+) (\d+)(e|w)(\d+)$/; $y = ($3 eq "n" ? -1 : 1) * ($2 + ($4/60)); $x = ($6 eq "e" ? 1 : -1) * ($5 + ($7/60)); $label = $1; ($t = $label) =~ s/\s//g; $label{$t} = $label; $x{$t} = $x; $y{$t} = $y; push @ordered, $t; $minx = $x if $x < $minx; $maxx = $x if $x > $maxx; $miny = $y if $y < $miny; $maxy = $y if $y > $maxy; } close COORDS; $xsize = 400; $ysize = 800; $xborder = 50; $yborder = 50; $dotsize = 3; $im = new GD::Image($xsize + 2*$xborder, $ysize + 2*$yborder); $white = $im->colorAllocate(255,255,255); $black = $im->colorAllocate(0,0,0); $red = $im->colorAllocate(255,0,0); $blue = $im->colorAllocate(0,0,200); $green = $im->colorAllocate(0,160,0); $xscale = $xsize / ($maxx - $minx); $xoffset = -$minx; $yscale = $ysize / ($maxy - $miny); $yoffset = -$miny; $in =~ s/[\[\]"\s]//g; @towns = split ",", $in; %ord = (); foreach $x (0..$#towns) { $towns[$x] = $ordered[$towns[$x]] if $towns[$x] =~ /^\d+$/; die "Where on earth is " . $towns[$x] . "?" unless $x{$towns[$x]}; $ord{$towns[$x]} = $x; } $townlabels = $query->param('townlabels'); $numberlabels = $query->param('numberlabels'); $alltowns = $query->param('alltowns'); foreach $t (keys %x) { $x{$t} = $xborder + $xscale * ($x{$t} + $xoffset); $y{$t} = $yborder + $yscale * ($y{$t} + $yoffset); } foreach $t ($alltowns ? keys %x : @towns) { $im->arc($x{$t}, $y{$t}, $dotsize * 2, $dotsize * 2, 0, 360, $black); $im->string(gdTinyFont, $x{$t} + $dotsize, $y{$t} + $dotsize, $label{$t}, $blue) if ($townlabels); $im->string(gdTinyFont, $x{$t} + $dotsize, $y{$t} + $dotsize - 14, $ord{$t}, $green) if ($numberlabels); } while ($#towns > 0) { $a = $towns[0]; $b = $towns[1]; $im->line($x{$a}, $y{$a}, $x{$b}, $y{$b}, $red); shift @towns; } print "Content-type: image/png\r\n\r\n"; binmode STDOUT; print $im->png;