HexGrid.pm

filter: Statementstmt 75.0% 6 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 81.8% 9 / 11 CC 1 SCAR 0.9
CC
1
Cov
75%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« GosperIsland.pm ↑ Summary Johnson.pm »
1 # Copyright 2017, 2018, 2019, 2020, 2021 Kevin Ryde
2 #
3 # This file is part of Graph-Maker-Other.
4 #
5 # This file is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # This file is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Graph-Maker-Other. See the file COPYING. If not, see
17 # <http://www.gnu.org/licenses/>.
18  
19  
20 #
21 # For interest, 3x3x3 appears in Emden R. Gansner, Yifan Hu, Stephen
22 # G. Kobourov, "On Touching Triangle Graphs", with a drawing by triangles
23 # (each vertex is a triangle with sides touching its neighbours).
24  
25  
26 package Graph::Maker::HexGrid;
27 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Graph::Maker;
Subroutine BEGIN: called CC=1 SCAR=2.6
30  
31 use vars '$VERSION','@ISA';
32 $VERSION = 16;
33 @ISA = ('Graph::Maker');
34  
35 # uncomment this to run the ### lines
36 # use Smart::Comments;
37  
38  
39 sub _default_graph_maker {
40 require Graph;
41 return Graph->new(@_);
42 }
43 sub _make_graph {
44 my ($params) = @_;
45 my $graph_maker = delete($params->{'graph_maker'}) || \&_default_graph_maker;
46 return $graph_maker->(%$params);
47 }
48  
49 sub _rotate_plus60 {
50 my ($x, $y) = @_;
51 return (($x-3*$y)/2, # rotate +60
52 ($x+$y)/2);
53 }
54 sub _rotate_plus120 {
55 my ($x, $y) = @_;
56 return (($x+3*$y)/-2, # rotate +120
57 ($x-$y)/2);
58 }
59  
60 my @hexagon = ([0,0], [2,0], [3,1], [2,2], [0,2], [-1,1]);
61  
62 sub init {
63 my ($self, %params) = @_;
64 ### HexGrid new: %params
65  
66 my $dims = delete($params{'dims'}) || [];
67 my @dims = (defined $dims->[0] ? $dims->[0] : 1,
68 defined $dims->[1] ? $dims->[1] : 1,
69 defined $dims->[2] ? $dims->[2] : 1);
70  
71 my $graph = _make_graph(\%params);
72 $graph->set_graph_attribute (name => "Hex Grid ".join(',',@dims));
73 $graph->set_graph_attribute (vertex_name_type_xy => 1);
74 $graph->set_graph_attribute (vertex_name_type_xy_triangular => 1);
75 my $add_edge = ($graph->is_directed ? 'add_cycle' : 'add_edge');
76  
77 my $seg_dx = 2;
78 my $seg_dy = 0;
79 my $step_dx = 3;
80 my $step_dy = -1;
81 my $top_dx = 0;
82 my $top_dy = 2;
83 my $x = 0;
84 my $y = 0;
85 foreach (1 .. 3) {
86 ### @dims
87 my $limit = $dims[2];
88 my $top_side = $dims[1];
89 foreach my $i (1 .. $dims[0] + $dims[1] - 1) {
90 ### $i
91 ### $limit
92 foreach my $j (0 .. $limit) {
93 my $x = $x + $j*$top_dx;
94 my $y = $y + $j*$top_dy;
95 my $from = "$x,$y";
96 my $to = ($x+$seg_dx).','.($y+$seg_dy);
97 ### edge: "$from to $to"
98 $graph->$add_edge($from, $to);
99 }
100 if ($i == $dims[0]) {
101 ($step_dx,$step_dy) = _rotate_plus60($step_dx,$step_dy);
102 }
103 $x += $step_dx;
104 $y += $step_dy;
105 $limit += ($i < $dims[1]);
106 $limit -= ($i >= $dims[0]);
107 }
108  
109 unshift @dims, pop @dims;
110 ($step_dx,$step_dy) = _rotate_plus60($step_dx,$step_dy);
111 ($top_dx,$top_dy) = _rotate_plus120($top_dx,$top_dy);
112 ($seg_dx,$seg_dy) = _rotate_plus120($seg_dx,$seg_dy);
113 # $top_dx = -$top_dx;
114 # $top_dy = -$top_dy;
115 # $x += $step_dx;
116 # $y += $step_dy;
117 # $x += $seg_dx;
118 # $y += $seg_dy;
119 }
120 return $graph;
121 }
122  
123 Graph::Maker->add_factory_type('hex_grid' => __PACKAGE__);
124 1;
125  
126 __END__
« GosperIsland.pm ↑ Summary Johnson.pm »