Cantor set

From Rosetta Code
Revision as of 12:36, 20 April 2018 by PureFox (talk | contribs) (Added Kotlin)
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

Kotlin

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

const val WIDTH = 81 const val HEIGHT = 5

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

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:
*********************************************************************************
***************************                           ***************************
*********         *********                           *********         *********
***   ***         ***   ***                           ***   ***         ***   ***
* *   * *         * *   * *                           * *   * *         * *   * *


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