The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

App::MathImage::PlanePath::ReplicatingSquares -- replicating L shapes

SYNOPSIS

 use App::MathImage::PlanePath::ReplicatingSquares;
 my $path = App::MathImage::PlanePath::ReplicatingSquares->new;
 my ($x, $y) = $path->n_to_xy (123);

DESCRIPTION

This path puts points in a pattern of replicating squares

      7  | 43  44  47  48  59  60  63  64
      6  | 41  42  45  46  57  58  61  62
      5  | 35  36  39  40  51  52  55  56
      4  | 33  34  37  38  49  50  53  54
      3  | 11  12  15  16  27  28  31  32
      2  |  9  10  13  14  25  26  29  30
      1  |  3   4   7   8  19  20  23  24
     y=0 |  1   2   5   6  17  18  21  22  ...
         |
         +--------------------------------
          x=0   1   2   3   4   5   6   7

The start is the 2x2 square 1,2,3,4. That layout is then replicated to make a 4x4 square out of 2x2 parts, and so on doubling in size.

  |
  +--------+--------+
  | 11  12 | 15  16 |
  |  9  10 | 13  14 |
  +--------+--------+
  |  3   4 |  7   8 |
  |  1   2 |  5   6 |
  +--------+--------+-

The coordinate calculation is simple. X and Y are simply every second bit of N-1. So if N-1 = binary 101010 then X=000 and Y=111 in binary, which is the N=43 shown above at X=0,Y=7.

Within a 2x2, 4x4, 8x8, 16x16 etc 2^(2^k) size square square all the N values 1 to 2^(2*(2^k)) fall within the square, so they're just a certain arrangement within that area. The top left corner 4, 16, 64, 256 etc is the 2^(2*(2^k)) maximum in each.

Wider

An optional wider parameter extends the squares horizontally to make rectangles.

FUNCTIONS

$path = App::MathImage::PlanePath::ReplicatingSquares->new ()

Create and return a new path object.

($x,$y) = $path->n_to_xy ($n)

Return the X,Y coordinates of point number $n on the path. Points begin at 1 and if $n < 1 then the return is an empty list.

Currently there are no fractional positions between then integer positions and in the current code $n is rounded to the nearest integer. Perhaps this will change, maybe something like unit diagonals through each square.

$n = $path->xy_to_n ($x,$y)

Return an integer point number for coordinates $x,$y. Each integer N is considered the centre of a unit square an $x,$y within that square returns N.

SEE ALSO

Math::PlanePath, Math::PlanePath::Diagonals