Cantor set: Difference between revisions
Content added Content deleted
(→{{header|Kotlin}}: Small improvement.) |
(Added C) |
||
Line 4: | Line 4: | ||
See details: [https://en.wikipedia.org/wiki/Cantor_set Cantor set] |
See details: [https://en.wikipedia.org/wiki/Cantor_set Cantor set] |
||
=={{header|C}}== |
|||
{{trans|Kotlin}} |
|||
<lang c>#include <stdio.h> |
|||
#define WIDTH 81 |
|||
#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}} |
|||
<pre> |
|||
********************************************************************************* |
|||
*************************** *************************** |
|||
********* ********* ********* ********* |
|||
*** *** *** *** *** *** *** *** |
|||
* * * * * * * * * * * * * * * * |
|||
</pre> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
Revision as of 13:40, 20 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
C
<lang c>#include <stdio.h>
- define WIDTH 81
- 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:
********************************************************************************* *************************** *************************** ********* ********* ********* ********* *** *** *** *** *** *** *** *** * * * * * * * * * * * * * * * *
Ring
<lang ring>
- Project : Cantor set
- Date : 2018/04/20
- Author : Gal Zsolt [~ CalmoSoft ~]
- 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: