| 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 | package Graph::Maker::QuartetTree; | ||
| 21 | 1 | ▶ | use 5.004; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | 1 | ▶ | use Graph::Maker; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 24 | |||
| 25 | use vars '$VERSION','@ISA'; | ||
| 26 | $VERSION = 17; | ||
| 27 | @ISA = ('Graph::Maker'); | ||
| 28 | |||
| 29 | # uncomment this to run the ### lines | ||
| 30 | # use Smart::Comments; | ||
| 31 | |||
| 32 | sub _default_graph_maker { | ||
| 33 | require Graph; | ||
| 34 | Graph->new(@_); | ||
| 35 | } | ||
| 36 | |||
| 37 | # return $x,$y multiplied by 2+i | ||
| 38 | # GP-Test (2+I)*(x+I*y) == (2*x-y) + (x+2*y)*I | ||
| 39 | sub _mul { | ||
| 40 | my ($x,$y) = @_; | ||
| 41 | return (2*$x - $y, | ||
| 42 | $x + 2*$y); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub _xy_rotate_plus90 { | ||
| 46 | my ($x,$y, $multiple) = @_; | ||
| 47 | $multiple %= 4; | ||
| 48 | foreach (1 .. $multiple) { | ||
| 49 | ($x,$y) = (-$y,$x); # rotate +90 | ||
| 50 | } | ||
| 51 | return ($x,$y); | ||
| 52 | } | ||
| 53 | # @ E | ||
| 54 | # < | | ||
| 55 | # v | ||
| 56 | # S---->*---->* @ expand on right | ||
| 57 | # v v ^ | ||
| 58 | # | ^ | ||
| 59 | # @ *---->* @ | ||
| 60 | # v | ||
| 61 | # | ||
| 62 | # *---->* S @ | ||
| 63 | # v | ||
| 64 | |||
| 65 | sub init { | ||
| 66 | my ($self, %params) = @_; | ||
| 67 | |||
| 68 | my $level = delete($params{'level'}) || 0; | ||
| 69 | my $vertex_names = delete($params{'vertex_names'}) || 'xy'; | ||
| 70 | my $graph_maker = delete($params{graph_maker}) || \&_default_graph_maker; | ||
| 71 | |||
| 72 | my $graph = $graph_maker->(%params); | ||
| 73 | $graph->set_graph_attribute (name => "Quartet Tree $level"); | ||
| 74 | $graph->set_graph_attribute (vertex_name_type_xy => 1); | ||
| 75 | |||
| 76 | my @ex = (1); | ||
| 77 | my @ey = (0); | ||
| 78 | foreach (1 .. $level) { | ||
| 79 | my ($ex,$ey) = _mul($ex[-1],$ey[-1]); | ||
| 80 | push @ex, $ex; | ||
| 81 | push @ey, $ey; | ||
| 82 | } | ||
| 83 | |||
| 84 | my $straight = ($vertex_names =~ /straight/); | ||
| 85 | my $add_edge = ($graph->is_directed ? 'add_cycle' : 'add_edge'); | ||
| 86 | my $recurse; | ||
| 87 | $recurse = sub { | ||
| 88 | my ($x,$y, $dir, $k) = @_; | ||
| 89 | ### recurse: "$x,$y dir=$dir k=$k" | ||
| 90 | ### assert: $k >= 0 | ||
| 91 | if ($k == 0) { | ||
| 92 | my $from = "$x,$y"; | ||
| 93 | my ($dx,$dy) = _xy_rotate_plus90(1,0, $dir); | ||
| 94 | $x += $dx; | ||
| 95 | $y += $dy; | ||
| 96 | my $to = "$x,$y"; | ||
| 97 | $graph->$add_edge($from,$to); | ||
| 98 | } else { | ||
| 99 | $k--; | ||
| 100 | my $dx = $ex[$k]; | ||
| 101 | my $dy = $ey[$k]; | ||
| 102 | ($dx,$dy) = _xy_rotate_plus90($dx,$dy, $dir); | ||
| 103 | my ($lx,$ly) = _xy_rotate_plus90($dx,$dy, 1); | ||
| 104 | $recurse->($x, $y, $dir, $k); | ||
| 105 | $recurse->($x+$dx, $y+$dy, $dir, $k); | ||
| 106 | $recurse->($x+2*$dx-$lx, $y+2*$dy-$ly, $dir+1, $k); | ||
| 107 | if ($straight) { | ||
| 108 | $recurse->($x+2*$dx+2*$lx, $y+2*$dy+2*$ly, $dir+1, $k); | ||
| 109 | } else { | ||
| 110 | $recurse->($x+$dx-$lx, $y+$dy-$ly, $dir, $k); | ||
| 111 | } | ||
| 112 | $recurse->($x+2*$dx+$lx, $y+2*$dy+$ly, $dir-1, $k); | ||
| 113 | } | ||
| 114 | }; | ||
| 115 | $recurse->(0,0, 0, $level); | ||
| 116 | |||
| 117 | return $graph; | ||
| 118 | } | ||
| 119 | |||
| 120 | Graph::Maker->add_factory_type('quartet_tree' => __PACKAGE__); | ||
| 121 | 1; | ||
| 122 | |||
| 123 | __END__ | ||