Octave fun: Difference between revisions
Jump to navigation
Jump to search
(Added a script to generate ASCII Christmas tree.) |
(→Octave tree: Use a gif for illustration.) |
||
(6 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
Writing programs (not only in Octave) is often seen as a boring activity. | |||
To see this is not true one can look at the samples below: | |||
== Octave | == Octave tree == | ||
[[File:OctaveTree.gif|frame|Output of {{Path|OctaveTree.m}}.]] | |||
{{File|OctaveTree.m|<syntaxhighlight lang="octave"> | |||
N = 25; # Height | |||
H = 7; # Trunk height | |||
g = 0.2; # Density of decoration (0 <= g <= 1). | |||
for i = 1:N | |||
for j = 1:N-i | |||
printf (" "); | |||
endfor | |||
for j = 1:2*i-1 | |||
z = rand (); | |||
if (z < g) | |||
printf ("@"); | |||
else | |||
printf ("*"); | |||
endif | |||
endfor | |||
printf ("\n"); | |||
endfor | |||
for i = 1:H | |||
for j = 1:N-2 | |||
printf (" "); | |||
endfor | |||
printf ("***\n"); | |||
endfor | |||
i = 0; | |||
printf ("2019"); | |||
while (true) | |||
i = mod (i, 2*N); | |||
for j = 1:i | |||
printf (" "); | |||
endfor | |||
if (i < 2*N-2) | |||
printf ("mmmDDD"); | |||
else | |||
printf (" 2020"); | |||
endif | |||
pause (0.1) | |||
printf ("\r2019"); | |||
i++; | |||
endwhile | |||
</syntaxhighlight>}} | |||
[[Category:Examples]] |
Latest revision as of 21:25, 13 June 2019
Writing programs (not only in Octave) is often seen as a boring activity. To see this is not true one can look at the samples below:
Octave tree[edit]
File: OctaveTree.m
N = 25; # Height
H = 7; # Trunk height
g = 0.2; # Density of decoration (0 <= g <= 1).
for i = 1:N
for j = 1:N-i
printf (" ");
endfor
for j = 1:2*i-1
z = rand ();
if (z < g)
printf ("@");
else
printf ("*");
endif
endfor
printf ("\n");
endfor
for i = 1:H
for j = 1:N-2
printf (" ");
endfor
printf ("***\n");
endfor
i = 0;
printf ("2019");
while (true)
i = mod (i, 2*N);
for j = 1:i
printf (" ");
endfor
if (i < 2*N-2)
printf ("mmmDDD");
else
printf (" 2020");
endif
pause (0.1)
printf ("\r2019");
i++;
endwhile