#!/usr/bin/env perl# -*- cperl -*-BEGIN { $^W = 1 }use strict;use diagnostics;use warnings;use Carp;use English;use Math::Trig;use GD;use constant PI => 4*atan(1);# June 30, 2007# Allan Peda## Ulam prime number spiral# http://mathworld.wolfram.com/PrimeSpiral.html# http://en.wikipedia.org/wiki/Ulam_spiral## we iterate over a grid, but we want to follow a spiral.# Direction is cartesian based x being horizontal motion,# and y being vertical. Up and right are postive motion.unless(defined($ARGV[0])){die "Please provide a png file to save to.";}my $pngfile = 'ulam_spiral.png';if($ARGV[0] =~ m/\.png$/i){$pngfile = $ARGV[0];} else {print "Supplied file not suffixed with .png defaulting to \"$pngfile\".\n";}# how many turns to make in the spiralmy $num_corners = 1600;my @turning_points = mkturns($num_corners);# which directions do we followmy $max = $turning_points[$#turning_points];print "Generating map for first $max primes.\n";my ($x_width, $y_width) = (0,0);$x_width = $y_width = int(sqrt($max+0.5));print "Dimensions are: $x_width x $y_width\n";my %primes = %{genprimes($max)};my $im = new GD::Image($x_width, $y_width);my $white = $im->colorAllocate(255,255,255);my $black = $im->colorAllocate(0,0,0);my $origin_color = $im->colorAllocate(255,0,0);my $position=$turning_points[0];my ($x, $y) = (0,0);my ($delx,$dely) = (1,0); # start out moving to the rightsub gdx { return( shift()+($x_width/2) ) }sub gdy { return( ($y_width/2)-shift()) }
Add a Comment