Chaos game: Difference between revisions

Content deleted Content added
Thundergnat (talk | contribs)
m syntax highlighting fixup automation
Thundergnat (talk | contribs)
m Automated syntax highlighting fixup (second round - minor fixes)
Line 15: Line 15:
* [http://www.geoastro.de/ChaosSpiel/ChaosEnglish.html The Game of Chaos]
* [http://www.geoastro.de/ChaosSpiel/ChaosEnglish.html The Game of Chaos]
<br><br>
<br><br>

=={{header|8086 Assembly}}==
=={{header|8086 Assembly}}==
{{trans|BASIC}}
{{trans|BASIC}}
Line 21: Line 20:
This program will run on a PC with CGA-compatible graphics. It will keep running until a key is pressed.
This program will run on a PC with CGA-compatible graphics. It will keep running until a key is pressed.


<syntaxhighlight lang=asm> cpu 8086
<syntaxhighlight lang="asm"> cpu 8086
bits 16
bits 16
vmode: equ 0Fh ; Get current video mode
vmode: equ 0Fh ; Get current video mode
Line 143: Line 142:
xchg cx,di
xchg cx,di
ret</syntaxhighlight>
ret</syntaxhighlight>

=={{header|Action!}}==
=={{header|Action!}}==
<syntaxhighlight lang=Action!>PROC Main()
<syntaxhighlight lang="action!">PROC Main()
INT x,w=[220],h=[190]
INT x,w=[220],h=[190]
BYTE y,i,CH=$02FC,COLOR1=$02C5,COLOR2=$02C6
BYTE y,i,CH=$02FC,COLOR1=$02C5,COLOR2=$02C6
Line 175: Line 173:
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Chaos_game.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Chaos_game.png Screenshot from Atari 8-bit computer]

=={{header|Amazing Hopper}}==
=={{header|Amazing Hopper}}==
{{trans|BASIC256}}
{{trans|BASIC256}}
<p>Considerar que Hopper no usa modos gráficos, y solo imprime el caracter ascii 219 achicando el tamaño de caracteres de la terminal, dado la ilusión de un modo gráfico "arcaico".</p>
<p>Considerar que Hopper no usa modos gráficos, y solo imprime el caracter ascii 219 achicando el tamaño de caracteres de la terminal, dado la ilusión de un modo gráfico "arcaico".</p>
<syntaxhighlight lang=Amazing Hopper>
<syntaxhighlight lang="amazing hopper">
/* Chaos game - JAMBO hopper */
/* Chaos game - JAMBO hopper */


Line 227: Line 224:
End
End
</syntaxhighlight>
</syntaxhighlight>


=={{header|BASIC}}==
=={{header|BASIC}}==
This should require minimal adaptation to work with any of the older Microsoft-style BASICs. Users of other dialects will need to replace lines <tt>10</tt> and <tt>150</tt> with the appropriate statements to select a graphics output mode (if necessary) and to plot a pixel at <tt>x,y</tt> in colour <tt>v</tt>; they should also add <tt>LET</tt> throughout and <tt>170 END</tt> if their dialects require those things.
This should require minimal adaptation to work with any of the older Microsoft-style BASICs. Users of other dialects will need to replace lines <tt>10</tt> and <tt>150</tt> with the appropriate statements to select a graphics output mode (if necessary) and to plot a pixel at <tt>x,y</tt> in colour <tt>v</tt>; they should also add <tt>LET</tt> throughout and <tt>170 END</tt> if their dialects require those things.
<syntaxhighlight lang=basic>10 SCREEN 1
<syntaxhighlight lang="basic">10 SCREEN 1
20 X = INT(RND(0) * 200)
20 X = INT(RND(0) * 200)
30 Y = INT(RND(0) * 173)
30 Y = INT(RND(0) * 173)
Line 249: Line 244:
==={{header|Applesoft BASIC}}===
==={{header|Applesoft BASIC}}===
Adapted from the code given above.
Adapted from the code given above.
<syntaxhighlight lang=basic>10 HGR2
<syntaxhighlight lang="basic">10 HGR2
20 X = INT(RND(1) * 200)
20 X = INT(RND(1) * 200)
30 Y = INT(RND(1) * 173)
30 Y = INT(RND(1) * 173)
Line 268: Line 263:


==={{header|BASIC256}}===
==={{header|BASIC256}}===
<syntaxhighlight lang=BASIC256>
<syntaxhighlight lang="basic256">
#Chaos game
#Chaos game


Line 305: Line 300:
==={{header|Locomotive Basic}}===
==={{header|Locomotive Basic}}===
Adapted from the generic BASIC version. In [https://benchmarko.github.io/CPCBasic/cpcbasic.html CPCBasic] this program completes in less than a second. But on a real CPC (or equivalent emulator), the same program takes over six minutes to run. So using CPCBasic is strongly advised. On CPCBasic, one can also use "mode 3" instead of mode 1 in line 10 and increase iterations to e.g. 2000000 in line 40, resulting in a higher-resolution image.
Adapted from the generic BASIC version. In [https://benchmarko.github.io/CPCBasic/cpcbasic.html CPCBasic] this program completes in less than a second. But on a real CPC (or equivalent emulator), the same program takes over six minutes to run. So using CPCBasic is strongly advised. On CPCBasic, one can also use "mode 3" instead of mode 1 in line 10 and increase iterations to e.g. 2000000 in line 40, resulting in a higher-resolution image.
<syntaxhighlight lang=locobasic>10 mode 1:randomize time:defint a-z
<syntaxhighlight lang="locobasic">10 mode 1:randomize time:defint a-z
20 x = 640 * rnd
20 x = 640 * rnd
30 y = 400 * rnd
30 y = 400 * rnd
Line 326: Line 321:


Note that ZX81 BASIC does not have an explicit computed <code>GOTO</code>; we can, however, actually compute the value of an expression and then <code>GOTO</code> it as a line number.
Note that ZX81 BASIC does not have an explicit computed <code>GOTO</code>; we can, however, actually compute the value of an expression and then <code>GOTO</code> it as a line number.
<syntaxhighlight lang=basic> 10 LET X=RND*46
<syntaxhighlight lang="basic"> 10 LET X=RND*46
20 LET Y=RND*40
20 LET Y=RND*40
30 FOR I=1 TO 5000
30 FOR I=1 TO 5000
Line 346: Line 341:
==={{header|ZX Spectrum Basic}}===
==={{header|ZX Spectrum Basic}}===
The final <code>INK</code> statement sets the foreground colour back to black.
The final <code>INK</code> statement sets the foreground colour back to black.
<syntaxhighlight lang=basic> 10 LET x=RND*200
<syntaxhighlight lang="basic"> 10 LET x=RND*200
20 LET y=RND*173
20 LET y=RND*173
30 FOR i=1 TO 20000
30 FOR i=1 TO 20000
Line 364: Line 359:
170 NEXT i
170 NEXT i
180 INK 0</syntaxhighlight>
180 INK 0</syntaxhighlight>

=={{header|C}}==
=={{header|C}}==


Interactive code which asks the side length of the starting triangle and number of iterations as inputs, a larger number of iterations produces a more accurate approximation of the Sierpinski fractal. Requires the [http://www.cs.colorado.edu/~main/bgi/cs1300/ WinBGIm] library.
Interactive code which asks the side length of the starting triangle and number of iterations as inputs, a larger number of iterations produces a more accurate approximation of the Sierpinski fractal. Requires the [http://www.cs.colorado.edu/~main/bgi/cs1300/ WinBGIm] library.


<syntaxhighlight lang=C>
<syntaxhighlight lang="c">
#include<graphics.h>
#include<graphics.h>
#include<stdlib.h>
#include<stdlib.h>
Line 422: Line 416:
return 0;
return 0;
}</syntaxhighlight>
}</syntaxhighlight>

=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==


<syntaxhighlight lang=csharp>using System.Diagnostics;
<syntaxhighlight lang="csharp">using System.Diagnostics;
using System.Drawing;
using System.Drawing;


Line 456: Line 449:
}
}
}</syntaxhighlight>
}</syntaxhighlight>

=={{header|C++}}==
=={{header|C++}}==
This program will generate the Sierpinski Triangle and save it to your hard drive.
This program will generate the Sierpinski Triangle and save it to your hard drive.
<syntaxhighlight lang=cpp>
<syntaxhighlight lang="cpp">
#include <windows.h>
#include <windows.h>
#include <ctime>
#include <ctime>
Line 609: Line 601:
}
}
</syntaxhighlight>
</syntaxhighlight>

=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
{{libheader|opticl}}
{{libheader|opticl}}
<syntaxhighlight lang=lisp>(defpackage #:chaos
<syntaxhighlight lang="lisp">(defpackage #:chaos
(:use #:cl
(:use #:cl
#:opticl))
#:opticl))
Line 648: Line 639:
{{libheader| Vcl.ExtCtrls}}
{{libheader| Vcl.ExtCtrls}}
{{libheader| System.Generics.Collections}}
{{libheader| System.Generics.Collections}}
<syntaxhighlight lang=Delphi>
<syntaxhighlight lang="delphi">
unit main;
unit main;


Line 773: Line 764:
[https://easylang.online/apps/_chaos-game.html Run it]
[https://easylang.online/apps/_chaos-game.html Run it]


<syntaxhighlight lang=text>color 900
<syntaxhighlight lang="text">color 900
x[] = [ 0 100 50 ]
x[] = [ 0 100 50 ]
y[] = [ 93 93 7 ]
y[] = [ 93 93 7 ]
Line 785: Line 776:
y = (y + y[h]) / 2
y = (y + y[h]) / 2
.</syntaxhighlight>
.</syntaxhighlight>

=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
<syntaxhighlight lang=lisp>; Chaos game
<syntaxhighlight lang="lisp">; Chaos game


(defun make-array (size)
(defun make-array (size)
Line 839: Line 829:


(chaos 400 180 50000) </syntaxhighlight>
(chaos 400 180 50000) </syntaxhighlight>

=={{header|F Sharp|F#}}==
=={{header|F Sharp|F#}}==
<syntaxhighlight lang=fsharp>
<syntaxhighlight lang="fsharp">
open System.Windows.Forms
open System.Windows.Forms
open System.Drawing
open System.Drawing
Line 866: Line 855:
f.Show()
f.Show()
</syntaxhighlight>
</syntaxhighlight>

=={{header|Fortran}}==
This FORTRAN code creates an output file which can be drawn with gnuplot.
<syntaxhighlight lang=Fortran>
PROGRAM CHAOS
IMPLICIT NONE
REAL, DIMENSION(3):: KA, KN ! Koordinates old/new
REAL, DIMENSION(3):: DA, DB, DC ! Triangle
INTEGER:: I, Z
INTEGER, PARAMETER:: UT = 17
! Define corners of triangle
DA = (/ 0., 0., 0. /)
DB = (/ 600., 0., 0. /)
DC = (/ 500., 0., 400. /)
! Define starting point
KA = (/ 500., 0., 100. /)
OPEN (UNIT = UT, FILE = 'aus.csv')
DO I=1, 1000000
Z = ZAHL()
WRITE (UT, '(3(F12.6, ";"))') KA
SELECT CASE (Z)
CASE (1)
CALL MITTELP(KA, DA, KN)
CASE (2)
CALL MITTELP(KA, DB, KN)
CASE (3)
CALL MITTELP(KA, DC, KN)
END SELECT
KA = KN
END DO
CLOSE (UT)
CONTAINS
! Calculates center of two points
SUBROUTINE MITTELP(P1, P2, MP)
REAL, INTENT(IN), DIMENSION(3):: P1, P2
REAL, INTENT(OUT), DIMENSION(3):: MP
MP = (P1 + P2) / 2.
END SUBROUTINE MITTELP
! Returns random number
INTEGER FUNCTION ZAHL()
REAL:: ZZ
CALL RANDOM_NUMBER(ZZ)
ZZ = ZZ * 3.
ZAHL = FLOOR(ZZ) + 1
IF (ZAHL .GT. 3) ZAHL = 3
END FUNCTION ZAHL
END PROGRAM CHAOS
</syntaxhighlight>
Gnuplot Code to draw file:
<syntaxhighlight lang=Gnuplot>
set terminal jpeg enhanced size 1600,960
set output 'chaos.jpg'
set nokey
set style line 1 lc rgb '#0060ad' lt 1 lw 3 pt 7 ps 0.3
plot 'aus.csv' using 1:3 with points ls 1 notitle
</syntaxhighlight>

=={{header|FreeBASIC}}==
{{trans|BASIC256}}
<syntaxhighlight lang=freebasic>
' Chaos game
Const ancho = 320, alto = 240
Dim As Integer x, y, iteracion, vertice
x = Int(Rnd * ancho)
y = Int(Rnd * alto)

Screenres ancho, alto, 8
Cls

For iteracion = 1 To 30000
vertice = Int(Rnd * 3) + 1
Select Case vertice
Case 1
x = x / 2
y = y / 2
vertice = 4 'red
Case 2
x = (ancho/2) + ((ancho/2)-x) / 2
y = alto - (alto-y) / 2
vertice = 2 'green
Case 3
x = ancho - (ancho-x) / 2
y = y / 2
vertice = 1 'blue
End Select
Pset (x,y),vertice
Next iteracion
Sleep
End
</syntaxhighlight>

=={{header|Fōrmulæ}}==

Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.

Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.

In '''[https://formulae.org/?example=Chaos_game this]''' page you can see the program(s) related to this task and their results.

=={{header|Forth}}==
=={{header|Forth}}==
{{works with|gforth|0.7.3}}
{{works with|gforth|0.7.3}}
<br>
<br>
<syntaxhighlight lang=forth>#! /usr/bin/gforth
<syntaxhighlight lang="forth">#! /usr/bin/gforth
\ Chaos Game
\ Chaos Game


Line 1,115: Line 1,005:
<tt>./chaos-game.fs 1000 1000000 chao-game.pbm</tt>:]
<tt>./chaos-game.fs 1000 1000000 chao-game.pbm</tt>:]
[https://commons.wikimedia.org/wiki/File:Chaos_Game.png]<br>
[https://commons.wikimedia.org/wiki/File:Chaos_Game.png]<br>
=={{header|Fortran}}==
This FORTRAN code creates an output file which can be drawn with gnuplot.
<syntaxhighlight lang="fortran">
PROGRAM CHAOS
IMPLICIT NONE
REAL, DIMENSION(3):: KA, KN ! Koordinates old/new
REAL, DIMENSION(3):: DA, DB, DC ! Triangle
INTEGER:: I, Z
INTEGER, PARAMETER:: UT = 17
! Define corners of triangle
DA = (/ 0., 0., 0. /)
DB = (/ 600., 0., 0. /)
DC = (/ 500., 0., 400. /)
! Define starting point
KA = (/ 500., 0., 100. /)
OPEN (UNIT = UT, FILE = 'aus.csv')
DO I=1, 1000000
Z = ZAHL()
WRITE (UT, '(3(F12.6, ";"))') KA
SELECT CASE (Z)
CASE (1)
CALL MITTELP(KA, DA, KN)
CASE (2)
CALL MITTELP(KA, DB, KN)
CASE (3)
CALL MITTELP(KA, DC, KN)
END SELECT
KA = KN
END DO
CLOSE (UT)
CONTAINS
! Calculates center of two points
SUBROUTINE MITTELP(P1, P2, MP)
REAL, INTENT(IN), DIMENSION(3):: P1, P2
REAL, INTENT(OUT), DIMENSION(3):: MP
MP = (P1 + P2) / 2.
END SUBROUTINE MITTELP
! Returns random number
INTEGER FUNCTION ZAHL()
REAL:: ZZ
CALL RANDOM_NUMBER(ZZ)
ZZ = ZZ * 3.
ZAHL = FLOOR(ZZ) + 1
IF (ZAHL .GT. 3) ZAHL = 3
END FUNCTION ZAHL
END PROGRAM CHAOS
</syntaxhighlight>
Gnuplot Code to draw file:
<syntaxhighlight lang="gnuplot">
set terminal jpeg enhanced size 1600,960
set output 'chaos.jpg'
set nokey
set style line 1 lc rgb '#0060ad' lt 1 lw 3 pt 7 ps 0.3
plot 'aus.csv' using 1:3 with points ls 1 notitle
</syntaxhighlight>
=={{header|FreeBASIC}}==
{{trans|BASIC256}}
<syntaxhighlight lang="freebasic">
' Chaos game
Const ancho = 320, alto = 240
Dim As Integer x, y, iteracion, vertice
x = Int(Rnd * ancho)
y = Int(Rnd * alto)


Screenres ancho, alto, 8
Cls

For iteracion = 1 To 30000
vertice = Int(Rnd * 3) + 1
Select Case vertice
Case 1
x = x / 2
y = y / 2
vertice = 4 'red
Case 2
x = (ancho/2) + ((ancho/2)-x) / 2
y = alto - (alto-y) / 2
vertice = 2 'green
Case 3
x = ancho - (ancho-x) / 2
y = y / 2
vertice = 1 'blue
End Select
Pset (x,y),vertice
Next iteracion
Sleep
End
</syntaxhighlight>
=={{header|Fōrmulæ}}==

Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.

Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.

In '''[https://formulae.org/?example=Chaos_game this]''' page you can see the program(s) related to this task and their results.
=={{header|GML}}==
=={{header|GML}}==
Create two new objects and rename them to "Game" and "Point" respectively.
Create two new objects and rename them to "Game" and "Point" respectively.
Line 1,121: Line 1,105:


'''"Game" Object Create Event:'''
'''"Game" Object Create Event:'''
<syntaxhighlight lang=GML>offset = 32; //Distance from triangle vertices to edges of window
<syntaxhighlight lang="gml">offset = 32; //Distance from triangle vertices to edges of window


//triangle vertex coordinates
//triangle vertex coordinates
Line 1,155: Line 1,139:


'''"Game" Object Step Event:'''
'''"Game" Object Step Event:'''
<syntaxhighlight lang=GML>if(step and step_count < max_iterations) //Wait for alarm to finish, or stop completely
<syntaxhighlight lang="gml">if(step and step_count < max_iterations) //Wait for alarm to finish, or stop completely
{ // if the desired number of iterations is hit
{ // if the desired number of iterations is hit
vertex = choose(1, 2, 3);
vertex = choose(1, 2, 3);
Line 1,192: Line 1,176:


'''"Game" Object Draw Event:'''
'''"Game" Object Draw Event:'''
<syntaxhighlight lang=GML>if(step_count < max_iterations)
<syntaxhighlight lang="gml">if(step_count < max_iterations)
{
{
draw_triangle(x1, y1, x2, y2, x3, y3, true);
draw_triangle(x1, y1, x2, y2, x3, y3, true);
Line 1,201: Line 1,185:


'''"Game" Object Alarm 0:'''
'''"Game" Object Alarm 0:'''
<syntaxhighlight lang=GML>step = true;
<syntaxhighlight lang="gml">step = true;
alarm[0] = interval;</syntaxhighlight>
alarm[0] = interval;</syntaxhighlight>




'''"Point" Object Draw Event:'''
'''"Point" Object Draw Event:'''
<syntaxhighlight lang=GML>draw_circle(x, y, 5, false);</syntaxhighlight>
<syntaxhighlight lang="gml">draw_circle(x, y, 5, false);</syntaxhighlight>

=={{header|Gnuplot}}==
=={{header|Gnuplot}}==
{{trans|PARI/GP}}
{{trans|PARI/GP}}
Line 1,213: Line 1,196:
[[File:ChGS3Gnu1.png|right|thumb|Output ChGS3Gnu1.png]]
[[File:ChGS3Gnu1.png|right|thumb|Output ChGS3Gnu1.png]]


<syntaxhighlight lang=gnuplot>
<syntaxhighlight lang="gnuplot">
## Chaos Game (Sierpinski triangle) 2/16/17 aev
## Chaos Game (Sierpinski triangle) 2/16/17 aev
reset
reset
Line 1,246: Line 1,229:
File: ChGS3Gnu1.png
File: ChGS3Gnu1.png
</pre>
</pre>

=={{header|Go}}==
=={{header|Go}}==
This writes a simple GIF animation of the method.
This writes a simple GIF animation of the method.
<syntaxhighlight lang=Go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,354: Line 1,336:
return err
return err
}</syntaxhighlight>
}</syntaxhighlight>

=={{header|Groovy}}==
=={{header|Groovy}}==


{{libheader|JavaFX}}
{{libheader|JavaFX}}


<syntaxhighlight lang=Groovy>import javafx.animation.AnimationTimer
<syntaxhighlight lang="groovy">import javafx.animation.AnimationTimer
import javafx.application.Application
import javafx.application.Application
import javafx.scene.Scene
import javafx.scene.Scene
Line 1,428: Line 1,409:
}
}
}</syntaxhighlight>
}</syntaxhighlight>

=={{header|Haskell}}==
=={{header|Haskell}}==


<syntaxhighlight lang=haskell>import Control.Monad (replicateM)
<syntaxhighlight lang="haskell">import Control.Monad (replicateM)
import Control.Monad.Random (fromList)
import Control.Monad.Random (fromList)


Line 1,444: Line 1,424:
Some transformations:
Some transformations:


<syntaxhighlight lang=haskell>-- the Sierpinsky`s triangle
<syntaxhighlight lang="haskell">-- the Sierpinsky`s triangle
triangle = [ (mid (0, 0), 1)
triangle = [ (mid (0, 0), 1)
, (mid (1, 0), 1)
, (mid (1, 0), 1)
Line 1,463: Line 1,443:


Drawing the result:
Drawing the result:
<syntaxhighlight lang=haskell>import Control.Monad.Random (getRandomR)
<syntaxhighlight lang="haskell">import Control.Monad.Random (getRandomR)
import Graphics.Gloss
import Graphics.Gloss


Line 1,472: Line 1,452:
where window = InWindow "Game of Chaos" (400,400) (0,0)
where window = InWindow "Game of Chaos" (400,400) (0,0)
point (x,y) = translate (100*x) (100*y) $ circle 0.02 </syntaxhighlight>
point (x,y) = translate (100*x) (100*y) $ circle 0.02 </syntaxhighlight>

=={{header|J}}==
=={{header|J}}==
[[File:j_chaos_game.png|300px|thumb|right]]
[[File:j_chaos_game.png|300px|thumb|right]]
<syntaxhighlight lang=j>
<syntaxhighlight lang="j">
Note 'plan, Working in complex plane'
Note 'plan, Working in complex plane'
Make an equilateral triangle.
Make an equilateral triangle.
Line 1,510: Line 1,489:
'marker'plot NEW_POINTS
'marker'plot NEW_POINTS
</syntaxhighlight>
</syntaxhighlight>

=={{header|Java}}==
=={{header|Java}}==
[[File:chaos_game.png|300px|thumb|right]]
[[File:chaos_game.png|300px|thumb|right]]
{{works with|Java|8}}
{{works with|Java|8}}
<syntaxhighlight lang=java>import java.awt.*;
<syntaxhighlight lang="java">import java.awt.*;
import java.awt.event.*;
import java.awt.event.*;
import java.util.*;
import java.util.*;
Line 1,603: Line 1,581:
}
}
}</syntaxhighlight>
}</syntaxhighlight>

=={{header|JavaScript}}==
=={{header|JavaScript}}==
Plots the fractal on an HTML <tt>canvas</tt> element.
Plots the fractal on an HTML <tt>canvas</tt> element.
<syntaxhighlight lang=javascript><html>
<syntaxhighlight lang="javascript"><html>


<head>
<head>
Line 1,659: Line 1,636:


</html></syntaxhighlight>
</html></syntaxhighlight>

=={{header|Julia}}==
=={{header|Julia}}==
Run in REPL.
Run in REPL.
<syntaxhighlight lang=julia>using Luxor
<syntaxhighlight lang="julia">using Luxor


function chaos()
function chaos()
Line 1,693: Line 1,669:
preview()
preview()
</syntaxhighlight>
</syntaxhighlight>

=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Java}}
{{trans|Java}}
<syntaxhighlight lang=scala>//Version 1.1.51
<syntaxhighlight lang="scala">//Version 1.1.51


import java.awt.*
import java.awt.*
Line 1,781: Line 1,756:
Same as Java entry
Same as Java entry
</pre>
</pre>

=={{header|Logo}}==
=={{header|Logo}}==
<syntaxhighlight lang=logo>to chaosgame :sidelength :iterations
<syntaxhighlight lang="logo">to chaosgame :sidelength :iterations
make "width :sidelength
make "width :sidelength
make "height (:sidelength/2 * sqrt 3)
make "height (:sidelength/2 * sqrt 3)
Line 1,812: Line 1,786:
hideturtle
hideturtle
end</syntaxhighlight>
end</syntaxhighlight>

=={{header|Lua}}==
=={{header|Lua}}==
Needs L&Ouml;VE 2d Engine
Needs L&Ouml;VE 2d Engine
<syntaxhighlight lang=Lua>
<syntaxhighlight lang="lua">
math.randomseed( os.time() )
math.randomseed( os.time() )
colors, orig = { { 255, 0, 0 }, { 0, 255, 0 }, { 0, 0, 255 } }, {}
colors, orig = { { 255, 0, 0 }, { 0, 255, 0 }, { 0, 0, 255 } }, {}
Line 1,857: Line 1,830:
end
end
</syntaxhighlight>
</syntaxhighlight>

=={{header|Maple}}==
=={{header|Maple}}==
<syntaxhighlight lang=maple>chaosGame := proc(numPoints)
<syntaxhighlight lang="maple">chaosGame := proc(numPoints)
local points, i;
local points, i;
randomize();
randomize();
Line 1,877: Line 1,849:
end use;
end use;
end proc:</syntaxhighlight>
end proc:</syntaxhighlight>

=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica>points = 5000;
<syntaxhighlight lang="mathematica">points = 5000;
a = {0, 0};
a = {0, 0};
b = {1, 0};
b = {1, 0};
Line 1,889: Line 1,860:
If[t == 1, d = Mean[{b, d}], d = Mean[{c, d}]]]; AppendTo[S, d]]
If[t == 1, d = Mean[{b, d}], d = Mean[{c, d}]]]; AppendTo[S, d]]
Graphics[Point[S]]</syntaxhighlight>
Graphics[Point[S]]</syntaxhighlight>

=={{header|Nim}}==
=={{header|Nim}}==
==={{header|Using a game library}}===
==={{header|Using a game library}}===
{{libheader|rapid}}
{{libheader|rapid}}
The "rapid" library is no longer maintained and this program fails to compile with last available version.
The "rapid" library is no longer maintained and this program fails to compile with last available version.
<syntaxhighlight lang=nim>import random
<syntaxhighlight lang="nim">import random


import rapid/gfx
import rapid/gfx
Line 1,934: Line 1,904:
==={{header|Using SDL}}===
==={{header|Using SDL}}===
{{libheader|SDL2}}
{{libheader|SDL2}}
<syntaxhighlight lang=nim>## needs sdl2 ("nimble install sdl2")
<syntaxhighlight lang="nim">## needs sdl2 ("nimble install sdl2")


import sdl2, random
import sdl2, random
Line 1,995: Line 1,965:
{{libheader|imageman}}
{{libheader|imageman}}


<syntaxhighlight lang=Nim>import math
<syntaxhighlight lang="nim">import math
import random
import random


Line 2,072: Line 2,042:


image.savePNG("chaos_game.png", compression = 9)</syntaxhighlight>
image.savePNG("chaos_game.png", compression = 9)</syntaxhighlight>

=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
Note: Find plotmat() here on RosettaCode Wiki.
Note: Find plotmat() here on RosettaCode Wiki.
{{Works with|PARI/GP|2.9.1 and above}}
{{Works with|PARI/GP|2.9.1 and above}}
[[File:SierpTri1.png|right|thumb|Output SierpTri1.png]]
[[File:SierpTri1.png|right|thumb|Output SierpTri1.png]]
<syntaxhighlight lang=parigp>
<syntaxhighlight lang="parigp">
\\ Chaos Game (Sierpinski triangle) 2/15/17 aev
\\ Chaos Game (Sierpinski triangle) 2/15/17 aev
pChaosGameS3(size,lim)={
pChaosGameS3(size,lim)={
Line 2,100: Line 2,069:
time = 751 ms.
time = 751 ms.
</pre>
</pre>

=={{header|Pascal}}==
=={{header|Pascal}}==
<syntaxhighlight lang=Pascal>
<syntaxhighlight lang="pascal">
program ChaosGame;
program ChaosGame;


Line 2,181: Line 2,149:


</syntaxhighlight>
</syntaxhighlight>

=={{header|Perl}}==
=={{header|Perl}}==
<syntaxhighlight lang=perl>use Imager;
<syntaxhighlight lang="perl">use Imager;


my $width = 1000;
my $width = 1000;
Line 2,221: Line 2,188:


$img->write(file => 'chaos_game_triangle.png');</syntaxhighlight>
$img->write(file => 'chaos_game_triangle.png');</syntaxhighlight>

=={{header|Phix}}==
=={{header|Phix}}==
Implements five of the fractals on the wikipedia page.
Implements five of the fractals on the wikipedia page.
Line 2,227: Line 2,193:
{{libheader|Phix/online}}
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/chaos.htm here]. Press space to cycle through the five fractals.
You can run this online [http://phix.x10.mx/p2js/chaos.htm here]. Press space to cycle through the five fractals.
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Chaos_game.exw
-- demo\rosetta\Chaos_game.exw
Line 2,324: Line 2,290:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
<!--</syntaxhighlight>-->

=={{header|Plain English}}==
=={{header|Plain English}}==
<syntaxhighlight lang=plainenglish>To run:
<syntaxhighlight lang="plainenglish">To run:
Start up.
Start up.
Initialize our reference points.
Initialize our reference points.
Line 2,370: Line 2,335:
{{out}}
{{out}}
[https://commons.wikimedia.org/wiki/File:Chaos-game.png]
[https://commons.wikimedia.org/wiki/File:Chaos-game.png]

=={{header|Processing}}==
=={{header|Processing}}==
<syntaxhighlight lang=java>size(300, 260);
<syntaxhighlight lang="java">size(300, 260);


background(#ffffff); // white
background(#ffffff); // white
Line 2,404: Line 2,368:
==={{header|Processing Python mode}}===
==={{header|Processing Python mode}}===


<syntaxhighlight lang=python>from __future__ import division
<syntaxhighlight lang="python">from __future__ import division


size(300, 260)
size(300, 260)
Line 2,429: Line 2,393:


set(x, height - y, colour)</syntaxhighlight>
set(x, height - y, colour)</syntaxhighlight>

=={{header|Python}}==
=={{header|Python}}==


<syntaxhighlight lang=Python>
<syntaxhighlight lang="python">
import argparse
import argparse
import random
import random
Line 2,550: Line 2,513:


</syntaxhighlight>
</syntaxhighlight>

=={{header|R}}==
=={{header|R}}==
Note: Find plotmat() here on RosettaCode Wiki.
Note: Find plotmat() here on RosettaCode Wiki.
Line 2,556: Line 2,518:
{{Works with|R|3.3.1 and above}}
{{Works with|R|3.3.1 and above}}
[[File:SierpTriR1.png|right|thumb|Output SierpTriR1.png]]
[[File:SierpTriR1.png|right|thumb|Output SierpTriR1.png]]
<syntaxhighlight lang=r>
<syntaxhighlight lang="r">
# Chaos Game (Sierpinski triangle) 2/15/17 aev
# Chaos Game (Sierpinski triangle) 2/15/17 aev
# pChaosGameS3(size, lim, clr, fn, ttl)
# pChaosGameS3(size, lim, clr, fn, ttl)
Line 2,591: Line 2,553:
'''Alternative code:'''
'''Alternative code:'''


<syntaxhighlight lang=r>
<syntaxhighlight lang="r">
pta = c(1,2)
pta = c(1,2)
ptb = c(4,2)
ptb = c(4,2)
Line 2,623: Line 2,585:
}
}
</syntaxhighlight>
</syntaxhighlight>

=={{header|Racket}}==
=={{header|Racket}}==


{{trans|Haskell}}
{{trans|Haskell}}


<syntaxhighlight lang=racket>#lang racket
<syntaxhighlight lang="racket">#lang racket


(require 2htdp/image)
(require 2htdp/image)
Line 2,678: Line 2,639:
(draw-fern)
(draw-fern)
(draw-dragon)</syntaxhighlight>
(draw-dragon)</syntaxhighlight>

=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
{{works with|Rakudo|2018.10}}
{{works with|Rakudo|2018.10}}


<syntaxhighlight lang=raku line>use Image::PNG::Portable;
<syntaxhighlight lang="raku" line>use Image::PNG::Portable;


my ($w, $h) = (640, 640);
my ($w, $h) = (640, 640);
Line 2,701: Line 2,661:


$png.write: 'Chaos-game-perl6.png';</syntaxhighlight>
$png.write: 'Chaos-game-perl6.png';</syntaxhighlight>

=={{header|REXX}}==
=={{header|REXX}}==
<syntaxhighlight lang=rexx>/*REXX pgm draws a Sierpinski triangle by running the chaos game with a million points*/
<syntaxhighlight lang="rexx">/*REXX pgm draws a Sierpinski triangle by running the chaos game with a million points*/
parse value scrsize() with sd sw . /*obtain the depth and width of screen.*/
parse value scrsize() with sd sw . /*obtain the depth and width of screen.*/
sw= sw - 2 /*adjust the screen width down by two. */
sw= sw - 2 /*adjust the screen width down by two. */
Line 2,943: Line 2,902:
</pre>
</pre>
</b>
</b>

=={{header|Ring}}==
=={{header|Ring}}==
<syntaxhighlight lang=ring>
<syntaxhighlight lang="ring">
# Project : Chaos game
# Project : Chaos game


Line 3,007: Line 2,965:
</syntaxhighlight>
</syntaxhighlight>
* [https://lh3.googleusercontent.com/-xqBO5MB8fpc/Wg05SvwaF9I/AAAAAAAABDA/UGI2goKdDoAR6nbbGZF0YcuwGG6tancvACLcBGAs/s1600/CalmoSoftChaos.jpg Chaos Game (image)]
* [https://lh3.googleusercontent.com/-xqBO5MB8fpc/Wg05SvwaF9I/AAAAAAAABDA/UGI2goKdDoAR6nbbGZF0YcuwGG6tancvACLcBGAs/s1600/CalmoSoftChaos.jpg Chaos Game (image)]

=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<syntaxhighlight lang=runbasic>x = int(rnd(0) * 200)
<syntaxhighlight lang="runbasic">x = int(rnd(0) * 200)
y = int(rnd(0) * 173)
y = int(rnd(0) * 173)
graphic #g, 200,200
graphic #g, 200,200
Line 3,030: Line 2,987:
next
next
render #g</syntaxhighlight>
render #g</syntaxhighlight>

=={{header|Rust}}==
=={{header|Rust}}==
Dependencies: image, rand
Dependencies: image, rand
<syntaxhighlight lang=rust>
<syntaxhighlight lang="rust">
extern crate image;
extern crate image;
extern crate rand;
extern crate rand;
Line 3,077: Line 3,033:
}
}
</syntaxhighlight>
</syntaxhighlight>

=={{header|Scala}}==
=={{header|Scala}}==
===Java Swing Interoperability===
===Java Swing Interoperability===
<syntaxhighlight lang=Scala>import javax.swing._
<syntaxhighlight lang="scala">import javax.swing._
import java.awt._
import java.awt._
import java.awt.event.ActionEvent
import java.awt.event.ActionEvent
Line 3,149: Line 3,104:


}</syntaxhighlight>
}</syntaxhighlight>

=={{header|Scilab}}==
=={{header|Scilab}}==
This script uses complex numbers to represent (x,y) coordinates: real part as x position, and imaginary part as y position.
This script uses complex numbers to represent (x,y) coordinates: real part as x position, and imaginary part as y position.
<syntaxhighlight lang=text>//Input
<syntaxhighlight lang="text">//Input
n_sides = 3;
n_sides = 3;
side_length = 1;
side_length = 1;
Line 3,200: Line 3,154:
It outputs a graphic window and prints on the console the time elapsed during iterations.
It outputs a graphic window and prints on the console the time elapsed during iterations.
<pre>Time: 1.0424433s.</pre>
<pre>Time: 1.0424433s.</pre>

=={{header|Sidef}}==
=={{header|Sidef}}==
<syntaxhighlight lang=ruby>require('Imager')
<syntaxhighlight lang="ruby">require('Imager')


var width = 600
var width = 600
Line 3,238: Line 3,191:
img.write(file => 'chaos_game.png')</syntaxhighlight>
img.write(file => 'chaos_game.png')</syntaxhighlight>
Output image: [https://github.com/trizen/rc/blob/master/img/chaos-game-sidef.png Chaos game]
Output image: [https://github.com/trizen/rc/blob/master/img/chaos-game-sidef.png Chaos game]

=={{header|Simula}}==
=={{header|Simula}}==
<syntaxhighlight lang=simula>BEGIN
<syntaxhighlight lang="simula">BEGIN
INTEGER U, COLUMNS, LINES;
INTEGER U, COLUMNS, LINES;
COLUMNS := 40;
COLUMNS := 40;
Line 3,327: Line 3,279:
XXX
XXX
</pre>
</pre>

=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
Line 3,333: Line 3,284:
{{libheader|Wren-dynamic}}
{{libheader|Wren-dynamic}}
{{libheader|Wren-seq}}
{{libheader|Wren-seq}}
<syntaxhighlight lang=ecmascript>import "dome" for Window
<syntaxhighlight lang="ecmascript">import "dome" for Window
import "graphics" for Canvas, Color
import "graphics" for Canvas, Color
import "math" for Point
import "math" for Point
Line 3,395: Line 3,346:


var Game = ChaosGame.new(640, 640)</syntaxhighlight>
var Game = ChaosGame.new(640, 640)</syntaxhighlight>

=={{header|X86 Assembly}}==
=={{header|X86 Assembly}}==
Sixty bytes handles it.
Sixty bytes handles it.
<syntaxhighlight lang=asm> 1 ;Assemble with: tasm, tlink /t
<syntaxhighlight lang="asm"> 1 ;Assemble with: tasm, tlink /t
2 0000 .model tiny
2 0000 .model tiny
3 0000 .code
3 0000 .code
Line 3,436: Line 3,386:
37 0136 0000 01DF 01DF Ty dw 0, 479, 479
37 0136 0000 01DF 01DF Ty dw 0, 479, 479
38 end start</syntaxhighlight>
38 end start</syntaxhighlight>

=={{header|XPL0}}==
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0>int Tx, Ty, X, Y, R;
<syntaxhighlight lang="xpl0">int Tx, Ty, X, Y, R;
[SetVid($12); \640x480x4 graphics
[SetVid($12); \640x480x4 graphics
Tx:= [320, 320-277, 320+277]; \equilateral triangle
Tx:= [320, 320-277, 320+277]; \equilateral triangle
Line 3,451: Line 3,400:
SetVid($03); \restore normal text mode
SetVid($03); \restore normal text mode
]</syntaxhighlight>
]</syntaxhighlight>

=={{header|Yabasic}}==
=={{header|Yabasic}}==
<syntaxhighlight lang=Yabasic>width = 640 : height = 480
<syntaxhighlight lang="yabasic">width = 640 : height = 480
open window width, height
open window width, height
window origin "lb"
window origin "lb"
Line 3,475: Line 3,423:
dot x, y
dot x, y
next</syntaxhighlight>
next</syntaxhighlight>

=={{header|Z80 Assembly}}==
=={{header|Z80 Assembly}}==
{{trans|BASIC}}
{{trans|BASIC}}
Line 3,483: Line 3,430:
space key.
space key.


<syntaxhighlight lang=z80>VREG: equ 99h ; VDP register port
<syntaxhighlight lang="z80">VREG: equ 99h ; VDP register port
VR0: equ 0F3DFh ; Copy of VDP R0 in memory
VR0: equ 0F3DFh ; Copy of VDP R0 in memory
VR1: equ 0F3E0h ; Copy of VDP R1 in memory
VR1: equ 0F3E0h ; Copy of VDP R1 in memory
Line 3,653: Line 3,600:
exx
exx
ret</syntaxhighlight>
ret</syntaxhighlight>


=={{header|zkl}}==
=={{header|zkl}}==
This is a half assed animated process - a bunch of pixels are drawn every couple of seconds and the pixmap written [to the file system]. So, if you open the output file ("chaosGame.jpg") it will [auto] update and show the progression of the image.
This is a half assed animated process - a bunch of pixels are drawn every couple of seconds and the pixmap written [to the file system]. So, if you open the output file ("chaosGame.jpg") it will [auto] update and show the progression of the image.
Line 3,661: Line 3,606:
{{trans|Java}}
{{trans|Java}}
[[File:ChaosGame.zkl.jpg|240px|thumb|right]]
[[File:ChaosGame.zkl.jpg|240px|thumb|right]]
<syntaxhighlight lang=zkl>w,h:=640,640;
<syntaxhighlight lang="zkl">w,h:=640,640;
bitmap:=PPM(w,h,0xFF|FF|FF); // White background
bitmap:=PPM(w,h,0xFF|FF|FF); // White background
colors:=T(0xFF|00|00,0x00|FF|00,0x00|00|FF); // red,green,blue
colors:=T(0xFF|00|00,0x00|FF|00,0x00|00|FF); // red,green,blue