File: greek-key.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 (133 lines) | stat: -rw-r--r-- 2,953 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
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
#!/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/>.

use 5.010;
use strict;
use warnings;
use POSIX 'floor';
use List::Util 'min', 'max';
use Math::PlanePath::GreekKeySpiral;

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

{
  {
    package Math::PlanePath::GreekKeySpiral;
    sub new {
      my $self = shift->SUPER::new (@_);

      my $turns = $self->{'turns'};
      if (! defined $turns) {
        $turns = 2;
      } elsif ($turns < 0) {
      }
      $self->{'turns'} = $turns;

      $self->{'centre_x'} = int($turns/2);
      $self->{'centre_y'} = int(($turns+1)/2);

      $self->{'midpoint'} = ($turns+1)*$turns/2;

      return $self;
    }
  }
  sub _n_part_to_xy {
    my ($self, $n) = @_;
    ### _n_part_to_xy(): $n


    # if ($rot & 2) {
    #   $y = -$y;
    # }
    # if ($d & 1) {
    #   $x = -$x;
    # }
    #
    #   my $d = int((sqrt(-8*$n-7) + 1) / 2);
    #   $x = $n;
    #   $y = 0;
    # } elsif (($n -= 1) < 0) {
    #   ### centre ...
    #   $x =  + $n;
    #   $y = $self->{'centre_y'};
    #   $rot = $self->{'turns'};
    # } else {
    #   $rot = $d;
    #   $x = $n;
    #   $y = 0;
    # }
  }

  my $turns = 6;
  my $self = Math::PlanePath::GreekKeySpiral->new (turns => $turns);
  ### $self
  foreach my $n (# 20 .. ($turns+1)**2
                 0, 6, 11, 15, 18, 20, 21,
                 21.25,
                 21.75,
                  22, 23, 25, 28, 32, 37, 43, 49
                ) {
    my $nn = $n;
    my $n = $n;
    my $rot = $self->{'turns'};

    my $centre_x = $self->{'centre_x'};
    my $centre_y = $self->{'centre_y'};
    if (($n -= $self->{'midpoint'}) <= 0) {
      $n = -$n;
      $rot += 0;
      $centre_x += 1;
    } elsif ($n < 1) {
      $rot -= 1;
      $centre_x += 1;
    } else {
      $n -= 1;
      $rot += 2;
    }

    my $d = int((sqrt(8*$n + 1) + 1) / 2);
    $n -= $d*($d-1)/2;

    my $half = int($d/2);
    my $x = $half - $n;
    my $y = $n*0 - $half;
    if (($d % 4) == 2) {
      $x -= 1;
    }
    if (($d % 4) == 3) {
      $y -= 1;
    }

    $rot -= $d;
    if ($rot & 2) {
      $x = -$x;
      $y = -$y;
    }
    if ($rot & 1) {
      ($x,$y) = (-$y,$x);
    }
    $x += $centre_x;
    $y += $centre_y;

    $rot &= 3;
    print "$nn  $d,$n,rot=$rot   $x,$y\n";
  }
  exit 0;
}