Cantor set: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Ring}}: added zkl header)
(→‎{{header|zkl}}: added code)
Line 218: Line 218:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl></lang>
<lang zkl>const WIDTH=81, HEIGHT=5;
var lines=HEIGHT.pump(List,List.createLong(WIDTH,"\U2588;").copy); // full block
<lang zkl></lang>
fcn cantor(start,len,index){
(seg:=len/3) or return();
foreach i,j in ([index..HEIGHT-1], [start + seg .. start + seg*2 - 1]){
lines[i][j]=" ";
}
cantor(start, seg, index + 1);
cantor(start + seg*2, seg, index + 1);
}(0,WIDTH,1);

lines.pump(Console.println,"concat");</lang>
{{out}}
{{out}}
<pre>
<pre>
█████████████████████████████████████████████████████████████████████████████████
███████████████████████████ ███████████████████████████
█████████ █████████ █████████ █████████
███ ███ ███ ███ ███ ███ ███ ███
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
</pre>
</pre>

Revision as of 23:49, 21 April 2018

Cantor set 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.

Draw Cantor set. See details: Cantor set


ALGOL 68

<lang algol68>BEGIN

   # draw a Cantor Set using ASCII                                            #
   INT    lines     = 5; # number of lines for the set                        #
   # we must choose the line width so that the width of each segment is       #
   # divisible by 3 ( except for the final line where the segment width will  #
   # be 1 )                                                                   #
   INT    set width = 3 ^ ( lines - 1 );
   [ set width ]CHAR set;
   # start with a complete line #
   FOR i TO set width DO set[ i ] := "#" OD;
   print( ( set, newline ) );
   # repeatedly modify the line, replacing the middle third of each segment   #
   # with blanks                                                              #
   INT   segment width := set width OVER 3;
   WHILE segment width > 0 DO
       INT   set pos := 1;
       WHILE set pos < ( set width - segment width ) DO
           set pos   +:= segment width;
           FOR char pos FROM set pos TO ( set pos + segment width ) - 1 DO
               set[ char pos ] := " "
           OD;
           set pos +:= segment width
       OD;
       print( ( set, newline ) );
       segment width OVERAB 3
   OD

END</lang>

Output:
#################################################################################
###########################                           ###########################
#########         #########                           #########         #########
###   ###         ###   ###                           ###   ###         ###   ###
# #   # #         # #   # #                           # #   # #         # #   # #

C

Translation of: Kotlin

<lang c>#include <stdio.h>

  1. define WIDTH 81
  2. define HEIGHT 5

char lines[HEIGHT][WIDTH];

void init() {

   int i, j;
   for (i = 0; i < HEIGHT; ++i) {
       for (j = 0; j < WIDTH; ++j) lines[i][j] = '*';
   }

}

void cantor(int start, int len, int index) {

   int i, j, seg = len / 3;
   if (seg == 0) return;
   for (i = index; i < HEIGHT; ++i) {
       for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' ';
   }
   cantor(start, seg, index + 1);
   cantor(start + seg * 2, seg, index + 1);

}

void print() {

   int i, j;
   for (i = 0; i < HEIGHT; ++i) {
       for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]);
       printf("\n");
   }

}

int main() {

   init();
   cantor(0, WIDTH, 1);
   print();
   return 0;

}</lang>

Output:
*********************************************************************************
***************************                           ***************************
*********         *********                           *********         *********
***   ***         ***   ***                           ***   ***         ***   ***
* *   * *         * *   * *                           * *   * *         * *   * *

Kotlin

Simple terminal drawing. <lang scala>// Version 1.2.31

const val WIDTH = 81 const val HEIGHT = 5

val lines = List(HEIGHT) { CharArray(WIDTH) { '*' } }

fun cantor(start: Int, len: Int, index: Int) {

   val seg = len / 3
   if (seg == 0) return
   for (i in index until HEIGHT) {
       for (j in start + seg until start + seg * 2) lines[i][j] = ' '
   }
   cantor(start, seg, index + 1)
   cantor(start + seg * 2, seg, index + 1)

}

fun main(args: Array<String>) {

   cantor(0, WIDTH, 1)
   lines.forEach { println(it) }

}</lang>

Output:
*********************************************************************************
***************************                           ***************************
*********         *********                           *********         *********
***   ***         ***   ***                           ***   ***         ***   ***
* *   * *         * *   * *                           * *   * *         * *   * *

Perl 6

Translation of: Kotlin

<lang perl6>sub cantor ( Int $height ) {

   my $width = 3 ** ($height - 1);
   my @lines = ( "\c[FULL BLOCK]" x $width ) xx $height;
   my sub _trim_middle_third ( $len, $start, $index ) {
       my $seg = $len div 3
           or return;
       for ( $index ..^ $height ) X ( 0 ..^ $seg ) -> ( $i, $j ) {
           @lines[$i].substr-rw( $start + $seg + $j, 1 ) = ' ';
       }
       _trim_middle_third( $seg, $start + $_, $index + 1 ) for 0, $seg * 2;
   }
   _trim_middle_third( $width, 0, 1 );
   return @lines;

}

.say for cantor(5);</lang>

Output:
█████████████████████████████████████████████████████████████████████████████████
███████████████████████████                           ███████████████████████████
█████████         █████████                           █████████         █████████
███   ███         ███   ███                           ███   ███         ███   ███
█ █   █ █         █ █   █ █                           █ █   █ █         █ █   █ █

Ring

<lang ring>

  1. Project : Cantor set
  2. Date  : 2018/04/20
  3. Author : Gal Zsolt [~ CalmoSoft ~]
  4. Email  : <calmosoft@gmail.com>

load "guilib.ring" paint = null

new qapp

       {
       win1 = new qwidget() {
                 setwindowtitle("")
                 setgeometry(100,100,800,600)
                 label1 = new qlabel(win1) {
                             setgeometry(10,10,800,600)
                             settext("")
                 }
                 new qpushbutton(win1) {
                         setgeometry(150,500,100,30)
                         settext("draw")
                         setclickevent("draw()")
                 }
                 show()
       }
       exec()
       }

func draw

       p1 = new qpicture()
              color = new qcolor() {
              setrgb(0,0,255,255)
       }
       pen = new qpen() {
                setcolor(color)
                setwidth(10)
       }
       paint = new qpainter() {
                 begin(p1)
                 setpen(pen)
       cantor(10,20,600)
       endpaint()
       }
       label1 { setpicture(p1) show() }
       return

func cantor(x,y,lens)

       if lens >= 10
          paint.drawline(x,y,x+lens,y)
          y = y + 20
          cantor(x,y,floor(lens/3))
          cantor(x+floor(lens*2/3),y,floor(lens/3))
       ok

</lang> Output image:

Cantor set

zkl

<lang zkl>const WIDTH=81, HEIGHT=5; var lines=HEIGHT.pump(List,List.createLong(WIDTH,"\U2588;").copy); // full block

fcn cantor(start,len,index){

  (seg:=len/3) or return();
  foreach i,j in ([index..HEIGHT-1], [start + seg .. start + seg*2 - 1]){
     lines[i][j]=" ";
  }
  cantor(start, seg, index + 1);
  cantor(start + seg*2, seg, index + 1);

}(0,WIDTH,1);

lines.pump(Console.println,"concat");</lang>

Output:
█████████████████████████████████████████████████████████████████████████████████
███████████████████████████                           ███████████████████████████
█████████         █████████                           █████████         █████████
███   ███         ███   ███                           ███   ███         ███   ███
█ █   █ █         █ █   █ █                           █ █   █ █         █ █   █ █