| 1 | # Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Math-PlanePath. | ||
| 4 | # | ||
| 5 | # Math-PlanePath is free software; you can redistribute it and/or modify | ||
| 6 | # it under the terms of the GNU General Public License as published by the | ||
| 7 | # Free Software Foundation; either version 3, or (at your option) any later | ||
| 8 | # version. | ||
| 9 | # | ||
| 10 | # Math-PlanePath 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 Math-PlanePath. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | |||
| 19 | package Math::PlanePath::PyramidSides; | ||
| 20 | use 5.004; | ||
| 21 | use strict; | ||
| 22 | |||
| 23 | use vars '$VERSION', '@ISA'; | ||
| 24 | $VERSION = 128; | ||
| 25 | use Math::PlanePath; | ||
| 26 | @ISA = ('Math::PlanePath'); | ||
| 27 | |||
| 28 | use Math::PlanePath::Base::Generic | ||
| 29 | 'round_nearest'; | ||
| 30 | *_sqrtint = \&Math::PlanePath::_sqrtint; | ||
| 31 | |||
| 32 | # uncomment this to run the ### lines | ||
| 33 | #use Smart::Comments; | ||
| 34 | |||
| 35 | |||
| 36 | use constant class_y_negative => 0; | ||
| 37 | use constant n_frac_discontinuity => 0.5; | ||
| 38 | *xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_visited_quad12; | ||
| 39 | |||
| 40 | use constant parameter_info_array => | ||
| 41 | [ Math::PlanePath::Base::Generic::parameter_info_nstart1() ]; | ||
| 42 | |||
| 43 | sub x_negative_at_n { | ||
| 44 | my ($self) = @_; | ||
| 45 | return $self->n_start + 1; | ||
| 46 | } | ||
| 47 | use constant dx_maximum => 1; | ||
| 48 | use constant dy_minimum => -1; | ||
| 49 | use constant dy_maximum => 1; | ||
| 50 | use constant absdx_minimum => 1; | ||
| 51 | use constant dsumxy_maximum => 2; # NE diagonal | ||
| 52 | use constant ddiffxy_maximum => 2; # SE diagonal | ||
| 53 | |||
| 54 | use constant dir_minimum_dxdy => (1,1); # North-East | ||
| 55 | use constant dir_maximum_dxdy => (1,-1); # South-East | ||
| 56 | |||
| 57 | use constant turn_any_left => 0; # only right or straight | ||
| 58 | |||
| 59 | |||
| 60 | #------------------------------------------------------------------------------ | ||
| 61 | |||
| 62 | sub new { | ||
| 63 | my $self = shift->SUPER::new(@_); | ||
| 64 | if (! defined $self->{'n_start'}) { | ||
| 65 | $self->{'n_start'} = $self->default_n_start; | ||
| 66 | } | ||
| 67 | return $self; | ||
| 68 | } | ||
| 69 | |||
| 70 | # starting each left side at 0.5 before | ||
| 71 | # | ||
| 72 | # d = [ 0, 1, 2, 3, 4 ] | ||
| 73 | # n = [ 0-0.5, 1-0.5, 4-0.5, 9-0.5, 16-0.5 ] | ||
| 74 | # N = (d^2 - 1/2) | ||
| 75 | # = ($d**2 - 1/2) | ||
| 76 | # d = 0 + sqrt(1 * $n + 1/2) | ||
| 77 | # = sqrt(4*$n+2)/2 | ||
| 78 | # | ||
| 79 | sub n_to_xy { | ||
| 80 | my ($self, $n) = @_; | ||
| 81 | ### PyramidSides n_to_xy: $n | ||
| 82 | |||
| 83 | # adjust to N=0 at origin X=0,Y=0 | ||
| 84 | $n = $n - $self->{'n_start'}; | ||
| 85 | |||
| 86 | my $d; | ||
| 87 | { | ||
| 88 | my $r = 4*$n + 2; | ||
| 89 | if ($r < 0) { | ||
| 90 | return; # N < -0.5 | ||
| 91 | } | ||
| 92 | $d = int( _sqrtint($r) / 2 ); | ||
| 93 | } | ||
| 94 | $n -= $d*($d+1); # to $n=0 on Y axis, so X=$n | ||
| 95 | ### remainder: $n | ||
| 96 | |||
| 97 | return ($n, | ||
| 98 | - abs($n) + $d); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub xy_to_n { | ||
| 102 | my ($self, $x, $y) = @_; | ||
| 103 | ### PyramidSides xy_to_n(): $x, $y | ||
| 104 | |||
| 105 | $y = round_nearest ($y); | ||
| 106 | if ($y < 0) { | ||
| 107 | return undef; | ||
| 108 | } | ||
| 109 | $x = round_nearest ($x); | ||
| 110 | |||
| 111 | my $d = abs($x) + $y; | ||
| 112 | return $d*$d + $x+$d + $self->{'n_start'}; | ||
| 113 | } | ||
| 114 | |||
| 115 | # exact | ||
| 116 | sub rect_to_n_range { | ||
| 117 | my ($self, $x1,$y1, $x2,$y2) = @_; | ||
| 118 | |||
| 119 | $x1 = round_nearest ($x1); | ||
| 120 | $y1 = round_nearest ($y1); | ||
| 121 | $x2 = round_nearest ($x2); | ||
| 122 | $y2 = round_nearest ($y2); | ||
| 123 | |||
| 124 | if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1); } # swap to y1<=y2 | ||
| 125 | if ($y2 < 0) { | ||
| 126 | return (1, 0); # rect all negative, no N | ||
| 127 | } | ||
| 128 | if ($y1 < 0) { $y1 *= 0; } # "*=" to preserve bigint y1 | ||
| 129 | |||
| 130 | my ($xlo, $xhi) = (abs($x1) < abs($x2) # lo,hi by absolute value | ||
| 131 | ? ($x1, $x2) | ||
| 132 | : ($x2, $x1)); | ||
| 133 | if ($x2 == -$x1) { | ||
| 134 | # when say x1=-5 x2=+5 then x=+5 is the bigger N | ||
| 135 | $xhi = abs($xhi); | ||
| 136 | } | ||
| 137 | if (($x1 >= 0) ^ ($x2 >= 0)) { | ||
| 138 | # if x1>=0 and x2<0 or other way around then x=0 is covered and is the | ||
| 139 | # smallest N | ||
| 140 | $xlo *= 0; # "*=" to preserve bigint | ||
| 141 | } | ||
| 142 | |||
| 143 | return ($self->xy_to_n ($xlo, $y1), | ||
| 144 | $self->xy_to_n ($xhi, $y2)); | ||
| 145 | } | ||
| 146 | |||
| 147 | 1; | ||
| 148 | __END__ |