Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

A hexidecimal slide-rule in PDF.

by perlmoth (Hermit)
on Jun 01, 2008 at 18:39 UTC ( #689589=CUFP: print w/ replies, xml ) Need Help??

I wanted a circular slide rule to help me with some hexadecimal calculations. Searching the web came up blank with respect to hexadecimal slide rules so I decided to make my own.

Here is the code I came up with:

use strict; use warnings; use PDF::API2; use constant PAGE_WIDTH => 595; use constant PAGE_HEIGHT => 842; use constant LINE_WIDTH => 0.5; use constant THIN_LINE_WIDTH => 0.25; use constant CM => 28.346457; # 1 cm in points sub log16 { my $n = shift; return log($n) / log(16); } sub angle { my $theta = shift; return -360.0 * log16($theta) } sub run { my $pdf = PDF::API2->new; my $font = $pdf->corefont("TimesNewRoman"); $pdf->mediabox(PAGE_HEIGHT, PAGE_WIDTH); my $gfx = $pdf->page->gfx; $gfx->strokecolor( '#000' ); $gfx->linewidth( THIN_LINE_WIDTH ); my $yc = PAGE_WIDTH / 2.0; my $radius = PAGE_HEIGHT / 4.0 - 1 * CM; my @tick16 = map( $_ / 16.0, (16 .. 255) ); my %outerDisk = (centre => PAGE_HEIGHT / 4.0, dir => +1, bigLetter => 0.35 * CM, smallLetter => 0.18 * CM ); my %innerDisk = (centre => 3.0 * PAGE_HEIGHT / 4.0, dir => -1, bigLetter => -0.6 * CM, smallLetter => -0.3 * CM ); for my $disk ( \%outerDisk, \%innerDisk ) { $gfx->save(); $gfx->translate($$disk{centre}, $yc); $gfx->linewidth( LINE_WIDTH ); $gfx->circle(0, 0, 1); $gfx->circle(0, 0, $radius); $gfx->circle(0, 0, $radius + 0.75 * CM * $$disk{dir}); $gfx->stroke(); $gfx->linewidth( THIN_LINE_WIDTH ); $gfx->move(0, -0.5 * CM); $gfx->line(0, 0.5 * CM); $gfx->stroke(); $gfx->move(-0.5 * CM, 0); $gfx->line(0.5 * CM, 0); $gfx->stroke(); for my $i (@tick16) { my $angle = angle($i); $gfx->rotate($angle); if ($i * 16 % 16 == 0) { $gfx->move(0, $radius); $gfx->line(0, $radius + 0.3 * CM * $$disk{dir}); $gfx->stroke; $gfx->textlabel(0, $radius + $$disk{bigLetter}, $font, 10, spr +intf("%X", $i), -align => 'center'); } elsif ($i * 16 % 4 == 0) { $gfx->move(0, $radius); $gfx->line(0, $radius + 0.15 * CM * $$disk{dir}); $gfx->stroke; } else { $gfx->move(0, $radius); $gfx->line(0, $radius + 0.1 * CM * $$disk{dir}); $gfx->stroke; } if ($i < 4.0 and $i * 16 % 16 != 0) { $gfx->textlabel(0, $radius + $$disk{smallLetter}, $font, 5, sp +rintf("%X", $i * 16 % 16), -align => 'center'); } if ($i > 4.0 and $i < 8.0 and $i * 16 % 2 == 0 and $i * 16 % 16 +!= 0) { $gfx->textlabel(0, $radius + $$disk{smallLetter}, $font, 5, sp +rintf("%X", $i * 16 % 16), -align => 'center'); } if ($i > 8.0 and $i * 16 % 4 == 0 and $i * 16 % 16 != 0) { $gfx->textlabel(0, $radius + $$disk{smallLetter}, $font, 5, sp +rintf("%X", $i * 16 % 16), -align => 'center'); } $gfx->rotate(-$angle); } $gfx->restore(); } $pdf->saveas("hex_slide_rule.pdf"); } run();


Instructions:
  1. Run program and print out resulting pdf.
  2. Carefully cut out disks.
  3. Put the inner disk over the outer disk with the centres carefully aligned and pin together.

Here are instructions in how to use slide rules. My rule is the equivalent of the C and D scales of a normal slide rule, except in hex. The principle of use is the same.

Comment on A hexidecimal slide-rule in PDF.
Download Code
Re: A hexidecimal slide-rule in PDF.
by graff (Chancellor) on Jun 02, 2008 at 04:20 UTC
    Wow. I am impressed. (++!) But... damn! A hex slide rule?? (a circular one, at that)

    Isn't that like the clearest possible symptom of chronic, near-fatal geekiness? I remember working with the old (straight-edge) slide rules -- it always made me nervous. As for hexadecimal arithmetic, I prefer it over octal, but it still tends to give me a headache.

    So if I just try to imagine doing hex arithmetic with a slide rule... I think I'd rather go to a dentist appointment.

      It's only geekiness if it goes in a leather case on your belt when not in use...

      --hsm

      "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
Re: A hexidecimal slide-rule in PDF.
by hsmyers (Canon) on Jun 02, 2008 at 16:09 UTC
    At least(++), probably(+++)! I'd suggest adding optional instructions about going to Kinko's and printing on heavy card stock or maybe laminating each part...

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://689589]
Approved by FunkyMonk
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2014-09-16 19:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (45 votes), past polls