File: koch-svg.pl

package info (click to toggle)
libmath-planepath-perl 117-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,988 kB
  • ctags: 5,587
  • sloc: perl: 99,131; ansic: 299; sh: 233; lisp: 73; makefile: 4
file content (93 lines) | stat: -rw-r--r-- 2,760 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/perl -w

# Copyright 2011, 2012 Kevin Ryde

# This file is part of Math-PlanePath.
#
# Math-PlanePath is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later
# version.
#
# Math-PlanePath is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Math-PlanePath.  If not, see <http://www.gnu.org/licenses/>.


# Usage: perl koch-svg.pl >output.svg
#        perl koch-svg.pl LEVEL >output.svg
#
# Print SVG format graphics to standard output for a Koch snowflake curve of
# given LEVEL fineness.  The default level is 4.
#
# The range of N values to plot follows the formulas in the
# Math::PlanePath::KochSnowflakes module POD.
#
# The svg output size is a fixed 300x300, but of course the point of svg is
# that it can be resized by a graphics viewer program.

use 5.006;
use strict;
use warnings;
use List::Util 'min';
use Math::PlanePath::KochSnowflakes;

my $path = Math::PlanePath::KochSnowflakes->new;

my $level = $ARGV[0] || 4;
my $width = 300;
my $height = 300;

# use the svg transform="translate()" to centre the origin in the viewport,
# but don't use its scale() to shrink the path X,Y coordinates, just in case
# the factor 1/4^level becomes very small
my $xcentre = $width / 2;
my $ycentre = $height / 2;

print <<"HERE";
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg"
     width="$width" height="$height">
<title>Koch Snowflake level $level</title>
<!-- Generated by Math-PlanePath example koch-svg.pl -->
<g transform="translate($xcentre,$ycentre)">
HERE


# factor to make equilateral triangles from the integer Y out of KochSnowflakes
my $y_equilateral = sqrt(3);

my $path_width = 2 * 3**$level;
my $path_height = 2 * (2/3) * 3**$level * $y_equilateral;

my $scale = 0.9 * min ($width / $path_width,
                       $height / $path_height);

my $linewidth = 1/$level;

# N range for $level, per KochSnowflakes POD
my $n_lo = 4**$level;
my $n_hi = 4**($level+1) - 1;

my $points = '';
foreach my $n ($n_lo .. $n_hi) {
  my ($x, $y) = $path->n_to_xy($n);
  $x *= $scale;
  $y *= $scale;
  $y *= $y_equilateral;
  $points .= "\n  $x, $y";
}

print <<"HERE"
<polygon fill="none"
         stroke="#FF00FF"
         stroke-width="$linewidth"
         points="$points"/>
</g>
</svg>
HERE