Leser: 28
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
#!/usr/bin/perl -w use strict; use Tk; my $mw = MainWindow->new(-title=>"Apfelmaennchen"); my $width = $mw->screenwidth; my $height = $mw->screenheight-200; $mw->geometry($width.'x'.($height+200)); my $bt1 = $mw->Button(-text=>"Beenden", -command=>sub{exit})->pack(-side=>'bottom'); my $bt2 = $mw->Button(-text=>"Malen", -command=>\&malen)->pack(-side=>'bottom'); my $p = $mw->Photo(-width=>800, -height=>800); my $c = $mw->Canvas(-width=>800, -height=>800, -bg=>'white')->pack(-side=>'top'); my $img = $c->createImage(0,0,-image=>$p,-anchor=>'nw'); $c->CanvasBind('<ButtonPress-1>'=>[\&koords,Ev('x'),Ev('y')]); MainLoop; ############################################## # koords # ############################################## sub koords{ shift; print "@_\n"; } ############################################## # malen # ############################################## sub malen{ my $n = 500; my @punkt; for my $i(1..800){ my $x = $i/200-2; for my $j(1..800){ my $y = $j/200-2; ${$punkt[$i]}[$j] = &iterieren($x,$y,$n); if (${$punkt[$i]}[$j] == 0){$p->put('black', -to => $i,$j);} elsif (${$punkt[$i]}[$j] == 1){$p->put('red', -to => $i,$j);} elsif (${$punkt[$i]}[$j] == 2){$p->put('green', -to => $i,$j);} elsif (${$punkt[$i]}[$j] == 3){$p->put('yellow', -to => $i,$j);} elsif (${$punkt[$i]}[$j] == 4){$p->put('cyan', -to => $i,$j);} elsif (${$punkt[$i]}[$j] == 5){$p->put('red', -to => $i,$j);} elsif (${$punkt[$i]}[$j] == 6){$p->put('magenta', -to => $i,$j);} elsif (${$punkt[$i]}[$j] == 7){$p->put('yellow', -to => $i,$j);} elsif (${$punkt[$i]}[$j] == 8){$p->put('red', -to => $i,$j);} elsif (${$punkt[$i]}[$j] >= 9){$p->put('blue', -to => $i,$j);} } } } ############################################## # iterieren # ############################################## sub iterieren{ my $x1 = $_[0]; my $xN = 0; my $y1 = $_[1]; my $yN = 0; my $count = 0; for my $i(1..$_[2]){ my $xNplus = $xN**2 - $yN**2 + $x1; my $yNplus = 2 * $xN * $yN + $y1; my $a = sqrt($xNplus**2 + $yNplus**2); $xN = $xNplus; $yN = $yNplus; if ($a > 2){$count = $i;last;} } return $count; }
1 2 3 4 5 6 7 8 9
my $dc = $frame->{dc} = Wx::MemoryDC->new(); my $bmp = $frame->{bmp} = Wx::Bitmap->new( $maltafelgroesse, $maltafelgroesse ); $dc->SelectObject( $bmp ); leere_zeichenbrett(); my $maltafel = $frame->{tafel} = Wx::StaticBitmap->new( $panel, -1, $bmp); $dc->Clear(); $dc->SelectObject( $bmp ); $dc->SetTextForeground( Wx::Colour->new( 0,0,128) ); $dc->DrawText( $ziffer, $x, $y );
1 2 3 4 5 6 7
push @farben, Wx::Colour->new( @$_ ) for @farbdef; push @pinsel, [Wx::Pen->new($_,5,&Wx::wxSOLID), Wx::Pen->new($_, 1, &Wx::wxSOLID), Wx::Brush->new($_, &Wx::wxSOLID) ] for @farben; $dc->SetPen( $pinsel[$nr][0] ); $dc->DrawLine( $xmax-30, $y, $xmax, $y);
1
2
3
4
5
sub erneure_maltafel {
$frame->{tafel}->SetBitmap( $frame->{bmp} );
$frame->{dc}->SelectObject( $frame->{bmp} );
$frame->{tafel}->Refresh();
}
1 2
EVT_LEFT_DOWN ( $panel, sub {$button[1]->SetLabel( 'touch the panel with left click' ) }); EVT_LEFT_UP ( $panel, sub {$button[1]->SetLabel( 'release the panel from left click' ) });
EVT_MOTION ( $button[1], sub { });
1 2 3
$p = new GD::Image(100,100); my $c = $mw->Canvas(-width=>800, -height=>800, -bg=>'white')->pack(-side=>'top'); my $img = $c->createImage(0,0,-image=>$p,-anchor=>'nw');
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
#!usr/bin/perl use strict; use warnings; use Gtk2 '-init'; use Data::Dumper; my $width=800; my $height=800; my $max_iter=500; my $bit_per_color=8; my $zoom=200; my $shift_x=-2; my $shift_y=-2; my @colortable=map{[int(rand((2**$bit_per_color)-1)), int(rand((2**$bit_per_color)-1)), int(rand((2**$bit_per_color)-1))]}(0..$max_iter+1); my $window = Gtk2::Window->new; $window->signal_connect (delete_event => sub {Gtk2->main_quit; exit;}); $window->set_title('Mandelbrot'); my $vbox = Gtk2::VBox->new(0,0); $window->add($vbox); my $txt=Gtk2::Label->new('START'); $vbox->pack_start($txt, 0, 1, 0); my $image = Gtk2::Image->new_from_pixbuf(Gtk2::Gdk::Pixbuf->new ('GDK_COLORSPACE_RGB', 0, $bit_per_color, $width, $height)); $vbox->pack_start($image, 1, 1, 5); my $b1=Gtk2::Button->new_with_label('BERECHNEN'); $b1->signal_connect('clicked' => sub{ my $data=render_mandelrot($width,$height,$shift_x,$shift_y,$zoom,$max_iter,$txt); $txt->set_text("FERTIG"); $image->set_from_pixbuf(data_to_pixbuf($data,$width,$height,$bit_per_color)); }); $vbox->pack_start($b1, 0, 1, 5); my $b2=Gtk2::Button->new_with_label('ENDE'); $b2->signal_connect('clicked' => sub{Gtk2->main_quit; exit;}); $vbox->pack_start($b2, 0, 1, 5); $window->show_all; Gtk2->main; sub render_mandelrot { my $mx=shift; my $my=shift; my $sx=shift; my $sy=shift; my $zoom=shift; my $mi=shift; my $txt=shift; my $data=''; for my $x (1..$mx) { $txt->set_text("BEARBEITE ZEILE:".($x+1)); $x=$x/$zoom + $sx; for my $y (1..$my) { $y=$y/$zoom + $sy; my ($i,$rx,$ry) = (0,0,0); # update GUI Gtk2->main_iteration while Gtk2->events_pending; while( $i++ <= $mi ) { my $rxn = $rx**2 - $ry**2 + $x; my $ryn = 2 * $rx * $ry + $y; ($rx,$ry)=($rxn,$ryn); last if( $rx**2 + $ry**2 > 4); } $data.=pack("C3",@{$colortable[$i]}); } } return $data; } sub data_to_pixbuf { my ($data,$b,$h,$bpc)=@_; return Gtk2::Gdk::Pixbuf->new_from_data($data,'GDK_COLORSPACE_RGB',0,$bpc,$b,$h,$b*3); }
2010-11-15T09:03:28 topegÜbrigens wieder eine schlaflose Nacht: