File: complex-plus.pl

package info (click to toggle)
libmath-planepath-perl 129-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 8,100 kB
  • sloc: perl: 115,748; ansic: 299; sh: 272; lisp: 73; makefile: 13
file content (138 lines) | stat: -rw-r--r-- 3,389 bytes parent folder | download
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/perl -w

# Copyright 2019 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/>.

use 5.010;
use strict;
use warnings;
use POSIX;
use List::Util 'min', 'max';
use Math::BaseCnv;
use Math::PlanePath::Base::Digits
  'digit_split_lowtohigh',
  'digit_join_lowtohigh';
use Math::PlanePath::ComplexPlus;
use lib 'xt';
use MyOEIS;

# uncomment this to run the ### lines
# use Smart::Comments;


{
  # picture of ComplexPlus Gray one bit different, cf TA

  require Image::Base::GD;
  my $width = 900;
  my $height = 600;
  my $scale = 20;
  my $ox = int($width * .7);
  my $oy = int($height * .7);

  my $transform = sub {
    my ($x,$y) = @_;
    $x *= $scale;
    $y *= $scale;
    $x += $ox;
    $y += $oy;
    return ($x,$height-1-$y);
  };

  my @dir4_to_dx = (1,0,-1,0);
  my @dir4_to_dy = (0,1,0,-1);

  require Math::PlanePath::ComplexPlus;
  my $path = Math::PlanePath::ComplexPlus->new;
  my $image = Image::Base::GD->new (-height => $height,
                                    -width  => $width);
  $image->rectangle(0,0, $width-1,$height-1, 'black');
  $image->ellipse($transform->(-.2,-.2),
                  $transform->(.2,.2),
                  'red', 1);
  foreach my $n (0 .. 2**8-1) {
    my ($x,$y) = $path->n_to_xy($n);
    my $n_gray = Gray($n);

    foreach my $dir4 (0 .. 3) {
      my $dx = $dir4_to_dx[$dir4];
      my $dy = $dir4_to_dy[$dir4];
      my $x2 = $x + $dx;
      my $y2 = $y + $dy;
      my $n_dir = $path->xy_to_n($x2,$y2) // next;
      my $n_dir_gray = Gray($n_dir);
      ### neighbour: sprintf "%d to %d  Gray %b to %b", $n, $n_dir, $n_gray, $n_dir_gray
      if (CountOneBits($n_gray ^ $n_dir_gray) == 1) {
        $image->line($transform->($x,$y),
                     $transform->($x2,$y2),
                     'white');
      }
    }
  }
  my $filename = '/tmp/gray.png';
  $image->save($filename);
  require IPC::Run;
  IPC::Run::start(['xzgv',$filename],'&');
  exit 0;
}
{
  # Gray codes
  my $path = Math::PlanePath::ComplexPlus->new;

  # {
  #   my $n = $path->xy_to_n(0,-1);
  #   print "$n\n";
  #   $n = $path->xy_to_n(0,1);
  #   print "$n\n";
  # }

  foreach my $y (reverse -5 .. 5) {
    foreach my $x (-5 .. 5) {
      my $n = $path->xy_to_n($x,$y);
      if (defined $n) {
        $n = sprintf '%b', Gray($n);
      } else {
        $n = '';
      }
      printf " %8s", $n;
    }
    print "\n";
  }
  exit 0;
}



sub Gray {
  my ($n) = @_;
  require Math::PlanePath::GrayCode;
  my $digits = [ digit_split_lowtohigh($n,2) ];
  Math::PlanePath::GrayCode::_digits_to_gray_reflected($digits,2);
  return digit_join_lowtohigh($digits,2);
}
CHECK {
  Gray(0) == 0 or die;
}

sub CountOneBits {
  my ($n) = @_;
  my $count = 0;
  for ( ; $n; $n>>=1) {
    $count += ($n & 1);
  }
  return $count;
}