| 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=0.0
| |||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | 1 | ▶ | use vars '$VERSION','@ISA'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 32 | 1 | $VERSION = 19; | |||||||||||||||||
| 33 | 1 | @ISA = ('Graph::Maker'); | |||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | # uncomment this to run the ### lines | ||||||||||||||||||
| 36 | # use Smart::Comments; | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | |||||||||||||||||||
| 39 | 4 | ▶ | sub _default_graph_maker { | ||||||||||||||||
|
Subroutine
_default_graph_maker: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 40 | 4 | require Graph; | |||||||||||||||||
| 41 | 4 | return Graph->new(@_); | |||||||||||||||||
| 42 | } | ||||||||||||||||||
| 43 | 4 | ▶ | sub _make_graph { | ||||||||||||||||
|
Subroutine
_make_graph: called CC=2 SCAR=7.1
| |||||||||||||||||||
| 44 | 4 | my ($params) = @_; | |||||||||||||||||
| 45 | 4 | ▶ | my $graph_maker = delete($params->{'graph_maker'}) || \&_default_graph_maker; | ||||||||||||||||
|
Condition50%1/2
delete $params->{'graph_maker'} || \&Graph::Maker::HexGrid::_default_graph_maker
MC/DC0%0/1
delete $params->{'graph_maker'} || \&Graph::Maker::HexGrid::_default_graph_maker
delete $params->{'graph_maker'}
Truth table50%1/2
A
A= delete $params->{'graph_maker'}
| |||||||||||||||||||
| 46 | 4 | return $graph_maker->(%$params); | |||||||||||||||||
| 47 | } | ||||||||||||||||||
| 48 | |||||||||||||||||||
| 49 | 18 | ▶ | sub _rotate_plus60 { | ||||||||||||||||
|
Subroutine
_rotate_plus60: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 50 | 18 | my ($x, $y) = @_; | |||||||||||||||||
| 51 | 18 | return (($x-3*$y)/2, # rotate +60 | |||||||||||||||||
| 52 | ($x+$y)/2); | ||||||||||||||||||
| 53 | } | ||||||||||||||||||
| 54 | 24 | ▶ | sub _rotate_plus120 { | ||||||||||||||||
|
Subroutine
_rotate_plus120: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 55 | 24 | my ($x, $y) = @_; | |||||||||||||||||
| 56 | 24 | return (($x+3*$y)/-2, # rotate +120 | |||||||||||||||||
| 57 | ($x-$y)/2); | ||||||||||||||||||
| 58 | } | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | 1 | my @hexagon = ([0,0], [2,0], [3,1], [2,2], [0,2], [-1,1]); | |||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | 4 | ▶ | sub init { | ||||||||||||||||
|
Subroutine
init: called CC=10 SCAR=23.1
Pod
undocumented
| |||||||||||||||||||
| 63 | 4 | my ($self, %params) = @_; | |||||||||||||||||
| 64 | ### HexGrid new: %params | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | 4 | ▶ | my $dims = delete($params{'dims'}) || []; | ||||||||||||||||
|
Condition50%1/2
delete $params{'dims'} || []
MC/DC0%0/1
delete $params{'dims'} || []
delete $params{'dims'}
Truth table50%1/2
A
A= delete $params{'dims'}
| |||||||||||||||||||
| 67 | 4 | ▶ | my @dims = (defined $dims->[0] ? $dims->[0] : 1, | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 68 | defined $dims->[1] ? $dims->[1] : 1, | ||||||||||||||||||
| 69 | defined $dims->[2] ? $dims->[2] : 1); | ||||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | 4 | my $graph = _make_graph(\%params); | |||||||||||||||||
| 72 | 4 | $graph->set_graph_attribute (name => "Hex Grid ".join(',',@dims)); | |||||||||||||||||
| 73 | 4 | $graph->set_graph_attribute (vertex_name_type_xy => 1); | |||||||||||||||||
| 74 | 4 | $graph->set_graph_attribute (vertex_name_type_xy_triangular => 1); | |||||||||||||||||
| 75 | 4 | ▶ | my $add_edge = ($graph->is_directed ? 'add_cycle' : 'add_edge'); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | 4 | my $seg_dx = 2; | |||||||||||||||||
| 78 | 4 | my $seg_dy = 0; | |||||||||||||||||
| 79 | 4 | my $step_dx = 3; | |||||||||||||||||
| 80 | 4 | my $step_dy = -1; | |||||||||||||||||
| 81 | 4 | my $top_dx = 0; | |||||||||||||||||
| 82 | 4 | my $top_dy = 2; | |||||||||||||||||
| 83 | 4 | my $x = 0; | |||||||||||||||||
| 84 | 4 | my $y = 0; | |||||||||||||||||
| 85 | 4 | foreach (1 .. 3) { | |||||||||||||||||
| 86 | ### @dims | ||||||||||||||||||
| 87 | 12 | my $limit = $dims[2]; | |||||||||||||||||
| 88 | 12 | my $top_side = $dims[1]; | |||||||||||||||||
| 89 | 12 | foreach my $i (1 .. $dims[0] + $dims[1] - 1) { | |||||||||||||||||
| 90 | ### $i | ||||||||||||||||||
| 91 | ### $limit | ||||||||||||||||||
| 92 | 6 | foreach my $j (0 .. $limit) { | |||||||||||||||||
| 93 | 12 | my $x = $x + $j*$top_dx; | |||||||||||||||||
| 94 | 12 | my $y = $y + $j*$top_dy; | |||||||||||||||||
| 95 | 12 | my $from = "$x,$y"; | |||||||||||||||||
| 96 | 12 | my $to = ($x+$seg_dx).','.($y+$seg_dy); | |||||||||||||||||
| 97 | ### edge: "$from to $to" | ||||||||||||||||||
| 98 | 12 | $graph->$add_edge($from, $to); | |||||||||||||||||
| 99 | } | ||||||||||||||||||
| 100 | 6 | ▶ | if ($i == $dims[0]) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 101 | 6 | ($step_dx,$step_dy) = _rotate_plus60($step_dx,$step_dy); | |||||||||||||||||
| 102 | } | ||||||||||||||||||
| 103 | 6 | $x += $step_dx; | |||||||||||||||||
| 104 | 6 | $y += $step_dy; | |||||||||||||||||
| 105 | 6 | $limit += ($i < $dims[1]); | |||||||||||||||||
| 106 | 6 | $limit -= ($i >= $dims[0]); | |||||||||||||||||
| 107 | } | ||||||||||||||||||
| 108 | |||||||||||||||||||
| 109 | 12 | unshift @dims, pop @dims; | |||||||||||||||||
| 110 | 12 | ($step_dx,$step_dy) = _rotate_plus60($step_dx,$step_dy); | |||||||||||||||||
| 111 | 12 | ($top_dx,$top_dy) = _rotate_plus120($top_dx,$top_dy); | |||||||||||||||||
| 112 | 12 | ($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 | 4 | return $graph; | |||||||||||||||||
| 121 | } | ||||||||||||||||||
| 122 | |||||||||||||||||||
| 123 | 1 | Graph::Maker->add_factory_type('hex_grid' => __PACKAGE__); | |||||||||||||||||
| 124 | 1 | 1; | |||||||||||||||||
| 125 | |||||||||||||||||||
| 126 | __END__ | ||||||||||||||||||