| 1 |
#!/usr/bin/perl -w |
| 2 |
use strict; |
| 3 |
|
| 4 |
# A simple CGI which runs a random set of points through the module |
| 5 |
# and displays the resulting polygons using the <canvas> tag supported |
| 6 |
# by recent versions of Firefox and Safari. You can run it as a CGI |
| 7 |
# or from the command line like: |
| 8 |
# |
| 9 |
# perl ./voronoi.cgi > foo.html |
| 10 |
|
| 11 |
use Math::Geometry::Voronoi; |
| 12 |
use HTML::Template; |
| 13 |
use CGI qw/header/; |
| 14 |
|
| 15 |
my $text = do { local $/; <DATA> }; |
| 16 |
my $template = HTML::Template->new(scalarref => \$text, |
| 17 |
die_on_bad_params => 0, |
| 18 |
loop_context_vars => 1); |
| 19 |
|
| 20 |
my $cgi = CGI->new(); |
| 21 |
my $n = $cgi->param('n') || 1000; |
| 22 |
$template->param(n => $n); |
| 23 |
|
| 24 |
die "What are you trying to do to me?" |
| 25 |
if $n > 5000; |
| 26 |
|
| 27 |
my $xsize = $cgi->param('xsize') || 1000; |
| 28 |
$template->param(xsize => $xsize); |
| 29 |
|
| 30 |
my $ysize = $cgi->param('ysize') || 500; |
| 31 |
$template->param(ysize => $ysize); |
| 32 |
|
| 33 |
my @points = map { [int(rand($xsize)), int(rand($ysize))] } (0 .. $n - 1); |
| 34 |
my %seen; |
| 35 |
@points = grep { not $seen{$_->[0], $_->[1]}++ } @points; |
| 36 |
|
| 37 |
my $geo = Math::Geometry::Voronoi->new(points => \@points); |
| 38 |
$geo->compute; |
| 39 |
my @polygons = $geo->polygons(normalize_vertices => sub { int($_[0]) }); |
| 40 |
|
| 41 |
my @poly; |
| 42 |
foreach my $poly ($geo->polygons) { |
| 43 |
my ($p, @verts) = @$poly; |
| 44 |
|
| 45 |
next if grep { $_->[0] > $xsize + 100 } @verts; |
| 46 |
next if grep { $_->[1] > $ysize + 100 } @verts; |
| 47 |
next if grep { $_->[0] < -100 } @verts; |
| 48 |
next if grep { $_->[1] < -100 } @verts; |
| 49 |
|
| 50 |
push @poly, |
| 51 |
{ points => [ |
| 52 |
map { {x => int($_->[0]), y => int($_->[1])} } (@verts, $verts[0]) |
| 53 |
], |
| 54 |
color => rand_color()}; |
| 55 |
} |
| 56 |
|
| 57 |
$template->param(polygons => \@poly); |
| 58 |
|
| 59 |
print header(), $template->output; |
| 60 |
|
| 61 |
sub rand_color { |
| 62 |
return join(',', map int(rand(255)), (1 .. 3)); |
| 63 |
} |
| 64 |
|
| 65 |
__DATA__ |
| 66 |
<html> |
| 67 |
<head> |
| 68 |
<title>Math::Geometry::Voronoi Demo</title> |
| 69 |
</head> |
| 70 |
<body> |
| 71 |
|
| 72 |
<h1>Math::Geometry::Voronoi Demo</h1> |
| 73 |
|
| 74 |
<p>This is a demo of |
| 75 |
the <a href="http://search.cpan.org/~samtregar/Math-Geometry-Voronoi">Math::Geometry::Voronoi</a> |
| 76 |
Perl module. To learn |
| 77 |
more, <a href="http://search.cpan.org/~samtregar/Math-Geometry-Voronoi/lib/Math/Geometry/Voronoi.pm">read |
| 78 |
the fine manual</a>.</p> |
| 79 |
|
| 80 |
<p>The image below shows a diagram containing <tmpl_var n> random |
| 81 |
points. Only the complete polygons returned from the polygons() |
| 82 |
method are shown, which is why nothing is drawn around the edges. If |
| 83 |
you don't see anything please try again with Firefox - your browser |
| 84 |
may not support the new HTML <canvas> tag.</p> |
| 85 |
|
| 86 |
<canvas style="border: 1px solid black" id="map" width="<tmpl_var xsize>" height="<tmpl_var ysize>"></canvas> |
| 87 |
|
| 88 |
<script language="javascript"> |
| 89 |
var canvas = document.getElementById('map'); |
| 90 |
var ctx = canvas.getContext('2d'); |
| 91 |
|
| 92 |
<tmpl_loop polygons> |
| 93 |
ctx.fillStyle = 'rgb(<tmpl_var color>)'; |
| 94 |
ctx.beginPath(); |
| 95 |
<tmpl_loop points> |
| 96 |
<tmpl_if __first__> |
| 97 |
ctx.moveTo(<tmpl_var x>,<tmpl_var y>); |
| 98 |
<tmpl_else> |
| 99 |
ctx.lineTo(<tmpl_var x>,<tmpl_var y>); |
| 100 |
</tmpl_if> |
| 101 |
</tmpl_loop> |
| 102 |
ctx.stroke(); |
| 103 |
ctx.fill(); |
| 104 |
</tmpl_loop> |
| 105 |
|
| 106 |
</script> |