Peano curve: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Go)
(Added C implementation)
Line 5: Line 5:
Produce a graphical or ASCII-art representation of a [[wp:Peano curve|Peano curve]] of at least order 3.
Produce a graphical or ASCII-art representation of a [[wp:Peano curve|Peano curve]] of at least order 3.


=={{header|C}}==
Adaptation of the C program in the [https://www.researchgate.net/profile/Christoph_Schierz2/publication/228982573_A_recursive_algorithm_for_the_generation_of_space-filling_curves/links/0912f505c2f419782c000000/A-recursive-algorithm-for-the-generation-of-space-filling-curves.pdf Breinholt-Schierz paper] , requires the [http://www.cs.colorado.edu/~main/bgi/cs1300/ WinBGIm] library.
<lang C>
/*Abhishek Ghosh, 14th September 2018*/

#include <graphics.h>
#include <math.h>

void Peano(int x, int y, int lg, int i1, int i2) {

if (lg == 1) {
lineto(3*x,3*y);
return;
}
lg = lg/3;
Peano(x+(2*i1*lg), y+(2*i1*lg), lg, i1, i2);
Peano(x+((i1-i2+1)*lg), y+((i1+i2)*lg), lg, i1, 1-i2);
Peano(x+lg, y+lg, lg, i1, 1-i2);
Peano(x+((i1+i2)*lg), y+((i1-i2+1)*lg), lg, 1-i1, 1-i2);
Peano(x+(2*i2*lg), y+(2*(1-i2)*lg), lg, i1, i2);
Peano(x+((1+i2-i1)*lg), y+((2-i1-i2)*lg), lg, i1, i2);
Peano(x+(2*(1-i1)*lg), y+(2*(1-i1)*lg), lg, i1, i2);
Peano(x+((2-i1-i2)*lg), y+((1+i2-i1)*lg), lg, 1-i1, i2);
Peano(x+(2*(1-i2)*lg), y+(2*i2*lg), lg, 1-i1, i2);
}

int main(void) {

initwindow(1000,1000,"Peano, Peano");

Peano(0, 0, 1000, 0, 0); /* Start Peano recursion. */
getch();
cleardevice();
return 0;
}
</lang>
=={{header|Go}}==
=={{header|Go}}==
{{libheader|Go Graphics}}
{{libheader|Go Graphics}}

Revision as of 20:54, 14 September 2018

Peano curve is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.


Task

Produce a graphical or ASCII-art representation of a Peano curve of at least order 3.

C

Adaptation of the C program in the Breinholt-Schierz paper , requires the WinBGIm library. <lang C> /*Abhishek Ghosh, 14th September 2018*/

  1. include <graphics.h>
  2. include <math.h>

void Peano(int x, int y, int lg, int i1, int i2) {

if (lg == 1) { lineto(3*x,3*y); return; }

lg = lg/3; Peano(x+(2*i1*lg), y+(2*i1*lg), lg, i1, i2); Peano(x+((i1-i2+1)*lg), y+((i1+i2)*lg), lg, i1, 1-i2); Peano(x+lg, y+lg, lg, i1, 1-i2); Peano(x+((i1+i2)*lg), y+((i1-i2+1)*lg), lg, 1-i1, 1-i2); Peano(x+(2*i2*lg), y+(2*(1-i2)*lg), lg, i1, i2); Peano(x+((1+i2-i1)*lg), y+((2-i1-i2)*lg), lg, i1, i2); Peano(x+(2*(1-i1)*lg), y+(2*(1-i1)*lg), lg, i1, i2); Peano(x+((2-i1-i2)*lg), y+((1+i2-i1)*lg), lg, 1-i1, i2); Peano(x+(2*(1-i2)*lg), y+(2*i2*lg), lg, 1-i1, i2); }

int main(void) {

initwindow(1000,1000,"Peano, Peano");

Peano(0, 0, 1000, 0, 0); /* Start Peano recursion. */

getch(); cleardevice();

return 0; } </lang>

Go

Library: Go Graphics


The following is based on the recursive algorithm and C code in this paper scaled up to 81 x 81 points. The image produced is a variant known as a Peano-Meander curve (see Figure 1(b) here). <lang go>package main

import "github.com/fogleman/gg"

var points []gg.Point

const width = 81

func peano(x, y, lg, i1, i2 int) {

   if lg == 1 {
       px := float64(width-x) * 10
       py := float64(width-y) * 10
       points = append(points, gg.Point{px, py})
       return
   }
   lg /= 3
   peano(x+2*i1*lg, y+2*i1*lg, lg, i1, i2)
   peano(x+(i1-i2+1)*lg, y+(i1+i2)*lg, lg, i1, 1-i2)
   peano(x+lg, y+lg, lg, i1, 1-i2)
   peano(x+(i1+i2)*lg, y+(i1-i2+1)*lg, lg, 1-i1, 1-i2)
   peano(x+2*i2*lg, y+2*(1-i2)*lg, lg, i1, i2)
   peano(x+(1+i2-i1)*lg, y+(2-i1-i2)*lg, lg, i1, i2)
   peano(x+2*(1-i1)*lg, y+2*(1-i1)*lg, lg, i1, i2)
   peano(x+(2-i1-i2)*lg, y+(1+i2-i1)*lg, lg, 1-i1, i2)
   peano(x+2*(1-i2)*lg, y+2*i2*lg, lg, 1-i1, i2)

}

func main() {

   peano(0, 0, width, 0, 0)
   dc := gg.NewContext(820, 820)
   dc.SetRGB(1, 1, 1) // White background
   dc.Clear()
   for _, p := range points {
       dc.LineTo(p.X, p.Y)
   }
   dc.SetRGB(1, 0, 1) // Magenta curve
   dc.SetLineWidth(1)
   dc.Stroke()
   dc.SavePNG("peano.png")

}</lang>

Perl 6

Works with: Rakudo version 2018.06

<lang perl6>use SVG;

role Lindenmayer {

   has %.rules;
   method succ {
       self.comb.map( { %!rules{$^c} // $c } ).join but Lindenmayer(%!rules)
   }

}

my $peano = 'L' but Lindenmayer( { 'L' => 'LFRFL-F-RFLFR+F+LFRFL', 'R' => 'RFLFR+F+LFRFL-F-RFLFR' } );

$peano++ xx 4; my @points = (10, 10);

for $peano.comb {

   state ($x, $y) = @points[0,1];
   state $d = 0 + 8i;
   when 'F' { @points.append: ($x += $d.re).round(1), ($y += $d.im).round(1) }
   when /< + - >/ { $d *= "{$_}1i" }
   default { }

}

say SVG.serialize(

   svg => [
       :660width, :660height, :style<stroke:lime>,
       :rect[:width<100%>, :height<100%>, :fill<black>],
       :polyline[ :points(@points.join: ','), :fill<black> ],
   ],

);</lang>

See: Peano curve (SVG image)

zkl

Using a Lindenmayer system and turtle graphics & turned 90°: <lang zkl>lsystem("L", // axiom

 Dictionary("L","LFRFL-F-RFLFR+F+LFRFL", "R","RFLFR+F+LFRFL-F-RFLFR"), # rules
 "+-F", 4)				  	// constants, order
turtle(_);

fcn lsystem(axiom,rules,consts,n){ // Lindenmayer system --> string

  foreach k in (consts){ rules.add(k,k) }
  buf1,buf2 := Data(Void,axiom).howza(3), Data().howza(3);  // characters
  do(n){
     buf1.pump(buf2.clear(), rules.get);
     t:=buf1; buf1=buf2; buf2=t;	// swap buffers
  }
  buf1.text		// n=4 --> 16,401  characters

}</lang> Using Image Magick and the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl <lang zkl>fcn turtle(koch){

  const D=10.0;
  dir,angle, x,y := 0.0, (90.0).toRad(), 20.0, 830.0; // turtle; x,y are float
  img,color := PPM(850,850), 0x00ff00;
  foreach c in (koch){
     switch(c){

case("F"){ // draw forward dx,dy := D.toRectangular(dir); tx,ty := x,y; x,y = (x+dx),(y+dy); img.line(tx.toInt(),ty.toInt(), x.toInt(),y.toInt(), color); } case("-"){ dir-=angle } // turn right case("+"){ dir+=angle } // turn left

     }
  }
  img.writeJPGFile("peanoCurve.zkl.jpg");

}</lang>

Output:

Image at Peano curve