Thread Apfelmännchen (11 answers)
Opened by Mampfgnom at 2010-11-10 08:42

topeg
 2010-11-11 00:09
#142626 #142626
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
Hier mal ein Beispiel in Gtk2:
Code (perl): (dl )
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);
}


EDIT: Fehler im Berechnen der Werte. :-/
Last edited: 2010-11-11 22:57:31 +0100 (CET)

View full thread Apfelmännchen