very cool.
A few alterations to use GD, a bit of randomness
in the number of points, and it makes a sweet Eterm
background generator.
It takes a little while of course,
since it's plotting a pixel at a time, but the good news is:
it's perfectly tile-able
#!/usr/bin/perl
use strict;
use GD;
my $numColors = 128;
my $size = 200;
my ($XMIN, $XMAX) = qw(0 1);
my ($YMIN, $YMAX) = qw(0 1);
my $POINTS = 3 + int(rand(6));
my $xfact = ($XMAX - $XMIN) / $size;
my $yfact = ($YMAX - $YMIN) / $size;
# allocate some colors
my @colors;
my $img = new GD::Image($size, $size);
for (my $i = 0; $i < $numColors; $i++) {
push @colors, $img->colorAllocate(1, 1, $i * (256 / $numColors));
}
my (@xs, @ys);
# Pick some random points
for (1..$POINTS) {
my ($xrand, $yrand) = (rand(), rand());
for my $xoffset (-1..1) {
for my $yoffset (-1..1) {
push (@xs, $xrand + $xoffset);
push (@ys, $yrand + $yoffset);
}
}
}
# Calculate screen
for (my $yi = 0; $yi < $size; $yi++) {
my $y = $YMIN + $yi * $yfact;
for (my $xi = 0; $xi < $size; $xi++) {
my $x = $XMIN + $xi * $xfact;
my ($best, $good) = closest($x, $y, \@xs, \@ys);
$img->setPixel($xi, $yi, $colors[int($numColors *
($best / $good))]);
}
}
binmode STDOUT;
print $img->png();
sub closest {
my ($x, $y, $xs, $ys) = @_;
my ($dist, $best, $good);
for (my $i = 0; $i < @$xs; $i++) {
$dist = sqrt(($x - $xs->[$i])**2 + ($y - $ys->[$i])**2);
if ($i == 0 || $dist < $best) { ($good, $best) = ($best, $d
+ist); }
elsif ($i == 1 || $dist < $good) { $good = $dist; }
}
return ($best, $good);
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.