#!/usr/bin/perl
use strict;
use warnings;
use Tk;
main();
exit;
sub main {
my $mw = MainWindow->new();
$mw->title('RDW #A - Betrachter');
# Anzeigefeld:
my $cnv = $mw->Canvas(-background => 'white',
-state => 'disabled',
-width => 500,
-height => 500,
-relief => 'flat',
-cursor => 'crosshair',
)
->pack(-side => 'bottom',
-expand => 1,
-fill => 'none',
);
# Eingabefeld und Button:
my $f = $mw->Frame(-relief => 'groove',
-borderwidth => 2,
)
-> pack(-side => 'top',
-expand => 1,
-fill => 'x',
);
my $ent = $f->Entry(-width => 75,
)
->pack (-side => 'left',
-expand => 1,
-fill => 'x',
-anchor => 'w',
);
my $btn = $f->Button(-text => 'Anzeigen',
-command => [ \&show, $mw, $cnv, $ent ],
)
->pack (-side => 'top',
-expand => 1,
-fill => 'x',
);
$mw->bind ('<Return>', sub{$btn->invoke()} );
$mw->bind ('<Escape>', sub{$mw->destroy()} );
$ent->focus();
MainLoop();
}
sub show {
my ($mw, $cnv, $ent) = @_;
$cnv->delete('all'); # canvas leeren
(my $rect = $ent->get()) =~ tr~[] ~~d; # müll wegwerfen
my @koord = split /,/, $rect;
unless (@koord > 3 and @koord % 4 == 0) {
print "\a";
return;
}
# Koordinatentransformation:
my ($smin, $smax) = (15, 490);
my $min;
my $max;
$min = $max = $koord[0];
for (@koord) {
$min = $_ if $_ < $min;
$max = $_ if $_ > $max;
}
my $faktor = ($smax-$smin) / ($max-$min);
$_ = int(($_-$min)*$faktor) + $smin for @koord;
# Rechtecke einzeichnen:
while (@koord) {
my $xmin = shift @koord;
my $ymin = 500 - (shift @koord);
my $xmax = shift @koord;
my $ymax = 500 - (shift @koord);
$cnv->createRectangle($xmin, $ymin, $xmax, $ymax);
}
}