Draw an exponential ruler

From CodeCodex

Implementations[edit]

Perl[edit]

#
# This software is in the public domain.
#

use strict;
use GD;
use POSIX qw(log10)

my $width = 850;
my $height = 200;
my $end_space = 40;
my $marked_width = $width - $end_space - $end_space;
my $min_power = 0;
my $max_power = 6;
my $inter_space = $marked_width / ($max_power - $min_power);

my $im = new GD::Image($width, $height);

my $white = $im->colorAllocate(255,255,255);
my $black = $im->colorAllocate(0,0,0);
my $red = $im->colorAllocate(255,0,0);
my $blue = $im->colorAllocate(0,0,255);

$im->setThickness(2);
$im->line(0, 100, $width, 100, $black);

for my $i ($min_power..$max_power) {
  $im->setThickness(2);
  my $x = $inter_space*$i + $end_space;
  $im->line($x, 75, $x, 125, $black);
  my $str = 10**$i;
  $str = (' ' x (7 - length($str))) . $str;
  $im->stringUp(&gdTinyFont, $x-4, 160, $str, $black);
  next if $i == $max_power;
  $im->setThickness(1);
  for my $j (2..9) {
    my $x2 = $x + (log10($j) * $inter_space);
    my $str = sprintf '%g', log10(10**$i*$j);
    $im->stringUp(&gdTinyFont, $x2-4, 75, $str, $black);
    $im->line($x2, 80, $x2, 120, $blue);
  }

}

open my $image_out, "> ruler.png";
binmode $image_out;
$image_out->print($im->png);