Math-PlanePath

 view release on metacpan or  search on metacpan

devel/dragon.pl  view on Meta::CPAN

  exit 0;
}

{
  # boundary squares

  #         k=0                k=1        *      k=2
  #                                       |
  #          left=1         *   left=1    *---*   left=2
  #          right=1        |   right=2       |   right=3
  #  *---*              *---*             *---*
  #
  my $path = Math::PlanePath::DragonCurve->new;
  foreach my $side ('left',
                    'right',
                   ) {
    my @values;
    foreach my $k (
                   # 1,
                   0 .. 10
                  ) {
      my $n_limit = 2**$k;
      # print "k=$k  n_limit=$n_limit\n";
      my $points = MyOEIS::path_boundary_points ($path, $n_limit,
                                                 side => $side,
                                                );
      # if ($side eq 'left') {
      #   @$points = reverse @$points;
      # }
      my %seen;
      my $count_edges  = 0;
      my $count_squares = 0;
      foreach my $i (1 .. $#$points) {
        my $p1 = $points->[$i-1];
        my $p2 = $points->[$i];
        my ($x1,$y1) = @$p1;
        my ($x2,$y2) = @$p2;
        ### edge: "$x1,$y1 to $x2,$y2"
        my $dx = $x2-$x1;
        my $dy = $y2-$y1;
        my $sx = 2*$x1 + ($dx + $dy);
        my $sy = 2*$y1 + ($dy - $dx);
        ### square: "$sx,$sy"
        $count_edges++;
        if (! $seen{"$sx,$sy"}++) {
          $count_squares++;
        }
      }
      print "k=$k edges=$count_edges squares=$count_squares\n";
      push @values, $count_squares;
    }

    # shift @values; shift @values; shift @values; shift @values;
    require Math::OEIS::Grep;
    Math::OEIS::Grep->search(array=>\@values);
  }
  exit 0;
}

{
  # convex hull iterations
  #
  require Math::Geometry::Planar;
  my $points = [ [0,0], [1,0], [1,1] ];
  my $nx = 1;
  my $ny = 1;
  foreach my $k (1 .. 20) {
    ($nx,$ny) = ($nx-$ny, $ny+$nx); # add rotate +90
    my $num_points = scalar(@$points);
    print "k=$k  nxy=$nx,$ny  count=$num_points\n";

    my @new_points = @$points;
    foreach my $p (@$points) {
      my ($x,$y) = @$p;
      ($x,$y) = ($y,-$x);  # rotate -90
      $x += $nx;
      $y += $ny;
      print "  $x,$y";
      push @new_points, [ $nx + $x, $ny + $y ];
    }
    print "\n";
    $points = \@new_points;

    # foreach my $i (0 .. $#new_points) {
    #   my $p = $new_points[$i];
    #   my ($x,$y) = @$p;
    # }

    my $planar = Math::Geometry::Planar->new;
    $planar->points($points);
    $planar = $planar->convexhull2;
    $points = $planar->points;

    next if @$points < 10;

    my $max_i = 0;
    my $max_p = $points->[0];
    foreach my $j (1 .. $#$points) {
      if ($points->[$j]->[0] > $max_p->[0]
          || ($points->[$j]->[0] == $max_p->[0]
              && $points->[$j]->[1] < $max_p->[1])) {
        $max_i = $j;
        $max_p = $points->[$j];
      }
    }
    $points = points_sort_by_dir($points, [$nx,$ny]);
    foreach my $i (0 .. $#$points) {
      my $p = $points->[$i - $max_i];
      my ($x,$y) = @$p;
      print "  $x,$y";
    }
    print "\n";
  }
  exit 0;

  sub points_sort_by_dir {
    my ($points, $point_start) = @_;
    ### $points
    require Math::NumSeq::PlanePathDelta;
    my $start = Math::NumSeq::PlanePathDelta::_dxdy_to_dir4(@$point_start) + 0;
    return [ sort {



( run in 0.733 second using v1.01-cache-2.11-cpan-71847e10f99 )