-
Notifications
You must be signed in to change notification settings - Fork 34
/
chaos_game_tetrahedron.pl
executable file
·53 lines (40 loc) · 1.07 KB
/
chaos_game_tetrahedron.pl
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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 25 August 2016
# https://github.com/trizen
# Chaos game, generating a Sierpinski Tetrahedron.
# https://en.wikipedia.org/wiki/Chaos_game
use 5.010;
use strict;
use warnings;
use Imager;
my $width = 2000;
my $height = 2000;
my @points = (
[int($width/2), 0],
[ 0, int($height-$height/4)],
[ $width-1, int($height-$height/4)],
[int($width/2), $height-1],
);
my $img = Imager->new(
xsize => $width,
ysize => $height,
channels => 3,
);
my $color = Imager::Color->new('#ff0000');
my $r = [int(rand($width)), int(rand($height))];
foreach my $i (1 .. 200000) {
my $p = $points[rand @points];
my $h = [
int(($p->[0] + $r->[0]) / 2),
int(($p->[1] + $r->[1]) / 2),
];
$img->setpixel(
x => $h->[0],
y => $h->[1],
color => $color,
);
$r = $h;
}
$img->write(file => 'chaos_game_tetrahedron.png');