NAME
Math::PlanePath::Diagonals -- points in diagonal stripes
SYNOPSIS
use Math::PlanePath::Diagonals;
my $path = Math::PlanePath::Diagonals->new;
my ($x, $y) = $path->n_to_xy (123);
DESCRIPTION
This path follows successive diagonals going from the Y axis down to the X axis.
6 | 22
5 | 16 23
4 | 11 17 24
3 | 7 12 18 ...
2 | 4 8 13 19
1 | 2 5 9 14 20
Y=0 | 1 3 6 10 15 21
+-------------------------
X=0 1 2 3 4 5
The horizontal sequence 1,3,6,10,etc at Y=0 is the triangular numbers s*(s+1)/2. If you plot them on a graph don't confuse that line with the axis or border!
Direction
Option direction => 'up'
reverses the order within each diagonal to count upward from the X axis.
direction => "up"
5 | 21
4 | 15 20
3 | 10 14 19 ...
2 | 6 9 13 18 24
1 | 3 5 8 12 17 23
Y=0 | 1 2 4 7 11 16 22
+-----------------------------
X=0 1 2 3 4 5 6
This is merely a transpose changing X,Y to Y,X, but it's the same as in DiagonalsOctant and can be handy to control the direction when combining Diagonals with some other path or calculation.
N Start
The default is to number points starting N=1 as shown above. An optional n_start
can give a different start, in the same diagonals sequence. For example to start at 0,
n_start => 0 n_start=>0, direction=>"up"
4 | 10 | 14
3 | 6 11 | 9 13
2 | 3 7 12 | 5 8 12
1 | 1 4 8 13 | 2 4 7 11
Y=0 | 0 2 5 9 14 | 0 1 3 6 10
+----------------- +-----------------
X=0 1 2 3 4 X=0 1 2 3 4
FUNCTIONS
See "FUNCTIONS" in Math::PlanePath for behaviour common to all path classes.
$path = Math::PlanePath::Diagonals->new ()
$path = Math::PlanePath::Diagonals->new (direction => $str, n_start => $integer)
-
Create and return a new path object. The
direction
option (a string) can bedirection => "down" the default direction => "up" number upwards from the X axis
($x,$y) = $path->n_to_xy ($n)
-
Return the X,Y coordinates of point number
$n
on the path.For
$n < 0.5
the return is an empty list, it being considered the path begins at 1. $n = $path->xy_to_n ($x,$y)
-
Return the point number for coordinates
$x,$y
.$x
and$y
are each rounded to the nearest integer, which has the effect of treating each point$n
as a square of side 1, so the quadrant x>=-0.5, y>=-0.5 is entirely covered. ($n_lo, $n_hi) = $path->rect_to_n_range ($x1,$y1, $x2,$y2)
-
The returned range is exact, meaning
$n_lo
and$n_hi
are the smallest and biggest in the rectangle.
FORMULAS
Rectangle to N Range
Within each row increasing X is increasing N, and in each column increasing Y is increasing N. So in a rectangle the lower left corner is the minimum N and the upper right is the maximum N.
| \ \ N max
| \ ----------+
| | \ |\
| |\ \ |
| \| \ \ |
| +----------
| N min \ \ \
+-------------------------
OEIS
Entries in Sloane's Online Encyclopedia of Integer Sequences related to this path include
http://oeis.org/A023531 (etc)
direction=down
A002262 X coordinate, runs 0 to k
A025581 Y coordinate, runs k to 0
A003056 X+Y coordinate sum, k repeated k+1 times
A114327 Y-X coordinate diff
A049581 abs(X-Y) coordinate diff
A004247 X*Y coordinate product
A048147 X^2+Y^2
A127949 dY, change in Y coordinate
A000124 N on Y axis
A001844 N on X=Y diagonal
direction=down, n_start=0
A023531 dSum = dX+dY, being 1 at N=triangular+1 (and 0)
A129184 turn 1=left,0=right
direction=up
Likewise but swapping X,Y.
SEE ALSO
Math::PlanePath, Math::PlanePath::DiagonalsAlternating, Math::PlanePath::DiagonalsOctant, Math::PlanePath::Corner, Math::PlanePath::Rows, Math::PlanePath::Columns
HOME PAGE
http://user42.tuxfamily.org/math-planepath/index.html
LICENSE
Copyright 2010, 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/>.