use GD; use strict; my($width) = 500; my($height) = 200; my($image) = new GD::Image($width, $height); my($white) = $image->colorAllocate(255, 255, 255); my($black) = $image->colorAllocate(0, 0, 0); my($red) = $image->colorAllocate(255, 0, 0); $image->transparent($white); $image->interlaced('true'); my($center) = $height/2; my($grid) = 30; my($checkline) = 3; #foreach (1..$width/$grid) { # $image->line($_*$grid, 0, $_*$grid, $height, $black); # $image->line(0, $_*$grid, $width, $_*$grid, $black); #} #$image->line($center, 0, $center, $height, $red); #$image->line(0, $center, $width, $center, $red); #&drawgraph; my($start) = {}; my($end) = {}; $start->{'x'} = 0; $start->{'y'} = $height-1; $end->{'x'} = $width-1; $end->{'y'} = $height-1; my(@point) = &koch($start, $end); for (my($i) = 0; $i+1 < @point; $i++) { $image->line($point[$i]{'x'}, $point[$i]{'y'}, $point[$i+1]{'x'}, $point[$i+1]{'y'}, $black); } open(OUT, "> graph.png"); binmode(OUT); print OUT $image->png; close(OUT); #sub forsize { # $elements = @_; # @tmp = sort({$a <=> $b} @_); # $max = $tmp[0]; # $min = $tmp[@tmp]; # # $w = $elements * 50; # $h = 100; # return ($w, $h); #} # ‹éŒ`”g sub square { my($x) = shift; my($r) = 0; $x *= 1/2; foreach (1..30) { $_ = $_*2-1; $r += sin($x*$_)/$_; } $r *= 5; return $r; } sub drawgraph { my(%prev); for (my($i) = 0; $i < $width; $i++) { my($x) = ($i - $center) / $grid; my($y); $y = &square($x); # $y = $x**$x; my($w) = $center + $x * $grid; my($h) = $center - $y * $grid; if (!%prev) { $prev{'w'} = $w; $prev{'h'} = $h; } $image->line($prev{'w'}, $prev{'h'}, $w, $h, $black); $prev{'w'} = $w; $prev{'h'} = $h; } } sub koch { my($start) = shift; my($end) = shift; my(@pt); if (&dist($start, $end) <= 1) { push(@pt, $start); push(@pt, $start); return @pt; } my($middle_left) = &pointing($start, $end, 3, 1); my($middle_right) = &pointing($start, $end, 3, 2); my($top) = &rotate($middle_left, $middle_right); push(@pt, &koch($start, $middle_left)); push(@pt, &koch($middle_left, $top)); push(@pt, &koch($top, $middle_right)); push(@pt, &koch($middle_right, $end)); push(@pt, $end); return @pt; } sub pointing { my($st) = shift; my($en) = shift; my($div) = shift; my($rate) = shift; my($pt) = {}; $pt->{'x'} = (($div-$rate)*$st->{'x'} + $rate*$en->{'x'}) / $div; $pt->{'y'} = (($div-$rate)*$st->{'y'} + $rate*$en->{'y'}) / $div; return $pt; } sub dist { my($st) = shift; my($en) = shift; return sqrt(abs($st->{'x'}-$en->{'x'})**2 + abs($en->{'y'}-$en->{'y'})**2); } sub rotate { my($PI) = 3.1415; my($center) = shift; my($target) = shift; my($pt) = {}; $pt->{'x'} = $center->{'x'} + cos(-$PI/3)*($target->{'x'}-$center->{'x'}) - sin(-$PI/3)*($target->{'y'}-$center->{'y'}); $pt->{'y'} = $center->{'y'} + sin(-$PI/3)*($target->{'x'}-$center->{'x'}) + cos(-$PI/3)*($target->{'y'}-$center->{'y'}); return $pt; } sub drawpoint { my($pt) = shift; $image->setPixel($pt->{'x'}, $pt->{'y'}, $black); }