1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
#!/usr/bin/perl
# Linseg helper perltk script.
# written april 2002 by Matthew Gilliard
use Tk;
use strict;
use vars qw($main);
my $i;
my $olt;
my $pc = 0;
my $max_x = 1;
my $min_y = 0;
my $max_y = 1;
my $main = new MainWindow;
my @xlist;
my @ylist;
my @point;
my %iinfo;
# First of all, put everything on the screen:
$main->Label(-text => 'linseg drawer')->pack;
# Frames for arranging things in
my $bf = $main->Frame->pack;
my $bft = $bf->Frame->pack(qw/-side top/);
my $bfb = $bf->Frame->pack(qw/-side bottom -fill x/);
my $ftl = $bft->Frame->pack(qw/-side left -fill y/);
my $ftr = $bft->Frame->pack(qw/-side right/);
# X-axis max entry. There is no x-min (see help file).
$bfb->Entry(-relief => 'sunken', -textvariable => \$max_x,
-width => 4)->pack(qw/-side right/);
# Y-axis entries
$ftl->Entry(-relief => 'sunken', -textvariable => \$min_y,
-width => 4)->pack(qw/-side bottom/);
$ftl->Entry(-relief => 'sunken', -textvariable => \$max_y,
-width => 4)->pack(qw/-side top/);
#Drawing canvs (the graph area)
my $c = $ftr->Canvas(relief => 'raised', width => 308, height => 311,
cursor => 'top_left_arrow');
$c->pack(qw/-side right/);
# Background square (new dot if right-clicked)
my $rect = $c->createRectangle(4,4,304,304, -fill => 'white');
$c->addtag("background", 'withtag', $rect);
$c->bind("background", '<3>' => [sub{new_dot(shift, $Tk::event->x,
$Tk::event->y)}]);
# Start point of graph
my $leftpoint = $c->createOval(1,307,7,301,-width => 1,
-outline => 'black', -fill => 'darkred');
$c->addtag("left_point", 'withtag', $leftpoint);
$c->bind("left_point", '<Enter>' => [sub{shift->itemconfigure(@_)},
'current', -fill => 'Skyblue2']);
$c->bind("left_point", '<Leave>' => [sub{shift->itemconfigure(@_)},
'current', -fill => 'darkred']);
$c->bind("left_point", '<B1-Motion>' => [sub{drag_edge(shift,
$Tk::event->x,
$Tk::event->y, \%iinfo)}]);
$c->bind("left_point", '<1>' => [sub{start_drag_point(shift,
$Tk::event->x,
$Tk::event->y, \%iinfo)}]);
# End point of graph
my $rightpoint = $c->createOval(301,1,307,7,-width => 1,
-outline => 'black', -fill => 'darkred');
$c->addtag("right_point", 'withtag', $rightpoint);
$c->bind("right_point", '<Enter>' => [sub{shift->itemconfigure(@_)},
'current', -fill => 'Skyblue2']);
$c->bind("right_point", '<Leave>' => [sub{shift->itemconfigure(@_)},
'current', -fill => 'darkred']);
$c->bind("right_point", '<B1-Motion>' => [sub{drag_edge(shift,
$Tk::event->x,
$Tk::event->y, \%iinfo)}]);
$c->bind("right_point", '<1>' => [sub{start_drag_point(shift,
$Tk::event->x,
$Tk::event->y, \%iinfo)}]);
# output line ("linseg ...)
my $outline = $main->Entry(-textvariable => \$olt)->pack(qw/-fill x/);
# Row of buttons (Print, Refresh, Exit)
my $f = $main->Frame->pack;
$f->Button(-relief => 'raised', -text => 'Exit',
-command => sub{exit;})->pack(qw/-side right/);
$f->Button(-relief => 'raised', -text => 'Print',
-command => sub{print "$olt\n";})->pack(qw/-side left/);
$f->Button(-relief => 'raised', -text => 'Refresh',
-command => sub{draw_lines($c)} )->pack(qw/-side left/);
# Initialise screen
draw_lines($c);
# Start responding to user input
MainLoop;
#
#Start of subroutines
#
sub new_dot(){
# This is called by a right-click on the background
# It adds the dot, and redraws the lines to include the new dot.
my ($c, $x, $y) = @_;
$x = $c->canvasx($x);
$y = $c->canvasy($y);
draw_dot($x, $y, ++$pc);
draw_lines($c);
}
sub draw_dot(){
# This is called (only) by new_dot()
# It draws the new dot in the right place and sets its behaviour
my ($xpos, $ypos) = @_;
$point[$pc] = $c->createOval($xpos-3,$ypos-3,$xpos+3,$ypos+3,
-width => 1, -outline => 'black', -fill => 'red');
$c->addtag("p$pc", 'withtag', $point[$pc]);
$c->bind("p$pc", '<Enter>' => [sub{shift->itemconfigure(@_)},
'current', -fill => 'Skyblue2']);
$c->bind("p$pc", '<Leave>' => [sub{shift->itemconfigure(@_)},
'current', -fill => 'Red']);
$c->bind("p$pc", '<B1-Motion>' => [sub{drag_point(shift,
$Tk::event->x,
$Tk::event->y, \%iinfo)}]);
$c->bind("p$pc", '<1>' => [sub{start_drag_point(shift,
$Tk::event->x,
$Tk::event->y, \%iinfo)}]);
$c->bind("p$pc", '<3>' => [sub{remove_dot(shift)}]);
}
sub remove_dot(){
# This is called when a dot is right-clicked on
# It deletes the dot in question, resorts the array of coords and redraws the lines
my ($c) = @_;
$c->delete('current');
getsort();
draw_lines($c);
}
sub getsort(){
# This is called whenever a dot is moved, added or removed.
# It finds all the dots' coordinates, sticks them in 2 arrays, and sorts them
my $i;
my $j = 0;
my $tx;
my $ty;
my $temp;
# clear old arrays
@xlist = ();
@ylist = ();
# find dots, and populate arrays
for ($i=0; $i<=100; $i++){
($tx,,$ty,) = $c->coords("p$i");
if ($tx && $ty){
$xlist[$j] = int $tx - 1;
$ylist[$j++] = int $ty - 1;
}
}
$pc = $j;
# Sort arrays in order of increasing x value.
for ($j=0; $j<$pc; $j++){
for ($i=0; $i<$pc; $i++){
if ($xlist[$i] > $xlist[$i+1]){
$temp = $xlist[$i];
$xlist[$i] = $xlist[$i+1];
$xlist[$i+1] = $temp;
$temp = $ylist[$i];
$ylist[$i] = $ylist[$i+1];
$ylist[$i+1] = $temp;
}
}
}
}
sub start_drag_point(){
# Called when a dot is clicked on
# Gets the dot's coords, ready for it to be dragged
my ($c, $x, $y, $iinfo) = @_;
$iinfo->{lastx} = $c->canvasx($x);
$iinfo->{lasty} = $c->canvasy($y);
}
sub drag_point(){
# Called when a dot is dragged
my ($c, $x, $y, $iinfo) = @_;
my $cx;
my $cy;
$x = $c->canvasx($x);
$y = $c->canvasy($y);
($cx,,$cy,) = $c->coords('current');
my $xamount = $x - $iinfo->{lastx};
my $yamount = $y - $iinfo->{lasty};
# Keep the dot inside the square, not just the canvas
if (($cx + $x - $iinfo->{lastx} < 3) ||
($cx + $x - $iinfo->{lastx} > 300)){
$xamount = 0;
}
if (($cy + $y - $iinfo->{lasty} > 300) ||
($cy + $y - $iinfo->{lasty} < 1)){
$yamount = 0;
}
$c->move('current', $xamount, $yamount);
$iinfo->{lastx} = $x;
$iinfo->{lasty} = $y;
# Redraw lines
draw_lines($c);
}
sub drag_edge(){
# The same as drag_point, but for the end dots, only up and down.
my ($c, $x, $y, $iinfo) = @_;
my $cx;
my $cy;
$x = $c->canvasx($x);
$y = $c->canvasy($y);
($cx,,$cy,) = $c->coords('current');
# Not off the top or bottom, please
if (($cy + $y - $iinfo->{lasty} > 0) &&
($cy + $y - $iinfo->{lasty} <= 300)){
$c->move('current', 0, $y-$iinfo->{lasty});
$iinfo->{lastx} = $x;
$iinfo->{lasty} = $y;
}
# Redraw lines
draw_lines($c);
}
sub draw_lines(){
# Called whenever the lines need redrawing (all the time)
my ($c) = @_;
my $i;
# Refresh array of points
getsort();
# Delete all lines
for ($i=0; $i<=100; $i++){
$c->delete("line$i");
}
# Use end points
my ($i,$starty,,) = $c->coords('left_point');
my ($i,$endy,,) = $c->coords('right_point');
$starty -= 1;
$endy -= 1;
if ($pc == 0){
# Line only has 2 points
my $line = $c->createLine(4, $starty+4, 304, $endy+4);
$c->addtag('line0', 'withtag', $line);
$c->lower('line0', 'left_point');
}else{
# First section
my $line = $c->createLine(4, $starty+4, $xlist[1]+4, $ylist[1]+4);
$c->addtag('line0', 'withtag', $line);
$c->lower('line0', 'left_point');
# Middle sections
for ($i=1; $i<$pc; $i++){
my $line = $c->createLine($xlist[$i]+4, $ylist[$i]+4,
$xlist[$i+1]+4, $ylist[$i+1]+4);
$c->addtag("line$i", 'withtag', $line);
$c->lower("line$i", 'left_point');
}
# Last section
my $line = $c->createLine($xlist[$pc]+4, $ylist[$pc]+4, 304, $endy+4);
$c->addtag("line$pc", 'withtag', $line);
$c->lower("line$pc", 'left_point');
}
# Generate the new "linseg ..." line
gen_linseg($c);
}
sub gen_linseg(){
# Updates the "linseg ..." line
my ($c) = @_;
my $i;
my $x;
my $y;
my $tx;
my $lastx = 0;
my $linseg = "linseg ";
my ($i,$starty,,) = $c->coords('left_point');
my ($i,$endy,,) = $c->coords('right_point');
$starty -= 1;
$endy -= 1;
# First section
$y = int ((300 - $starty)/3)/100;
$linseg .= $y*($max_y-$min_y) + $min_y . ", ";
# Middle sections
for ($i=1; $i<=$pc; $i++){
$x = int(($xlist[$i])/3.0)/100.0;
$tx = $x - $lastx;
# Account for x_max being a p-field
if ($max_x =~ m/\Ap\d*/){
$tx .= "*$max_x";
}else{
$tx *= $max_x;
}
$y = int((300 - $ylist[$i])/3)/100 * ($max_y-$min_y) + $min_y;
$linseg .= "$tx, $y, ";
$lastx = $x;
}
# Last section
$tx = 1 - $lastx;
$y = int((300 - $endy)/3)/100 * ($max_y-$min_y) + $min_y;
if ($max_x =~ m/\Ap\d*/){
$tx .= "*$max_x";
}else{
$tx *= $max_x;
}
$linseg .= "$tx, $y";
# Display the line
$olt = $linseg;
}
|