Chaos game: Difference between revisions

4,608 bytes added ,  2 months ago
m (syntax highlighting fixup automation)
(17 intermediate revisions by 11 users not shown)
Line 15:
* [http://www.geoastro.de/ChaosSpiel/ChaosEnglish.html The Game of Chaos]
<br><br>
 
=={{header|8086 Assembly}}==
{{trans|BASIC}}
Line 21 ⟶ 20:
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
bits 16
vmode: equ 0Fh ; Get current video mode
Line 143 ⟶ 142:
xchg cx,di
ret</syntaxhighlight>
 
=={{header|Action!}}==
<syntaxhighlight lang=Action"action!">PROC Main()
INT x,w=[220],h=[190]
BYTE y,i,CH=$02FC,COLOR1=$02C5,COLOR2=$02C6
Line 175 ⟶ 173:
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Chaos_game.png Screenshot from Atari 8-bit computer]
 
=={{header|Amazing Hopper}}==
{{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>
<syntaxhighlight lang=Amazing"amazing Hopperhopper">
/* Chaos game - JAMBO hopper */
 
Line 196 ⟶ 193:
mid ancho=0, Let( mid ancho:= Div(ancho,2))
 
Cls
i=LIMITE
Void(pixeles)
 
Loop
Ceil(Rand(3)), Move toOn gosub(vertice EQ1, EQ2, EQ3 )
 
If( Eq(vertice,1) )
Set( LetInt(xy), := DivInt(x), 2color),Apndrow(pixeles)
--i
Let(y := Div(y, 2))
color=9
Else If( Eq(vertice,2))
Let(x := Add( mid ancho, Div(Sub(mid ancho, x), 2) ) )
Let(y := Sub( alto, Div( Sub(alto, y), 2 )))
color=10
Else
Let(x := Sub(ancho, Div( Sub(ancho, x), 2)))
Let(y := Div(y, 2))
color=4
End If
Set( Int(y), Int(x), color),Apndrow(pixeles)
--i
Back if (i) is not zero
 
Set video graph(2)
Canvas-term
Cls
i=1
Iterator(++i, Leq(i,LIMITE), Colorfore([i,3]Get(pixeles)), \
Locate( [i,1]Get(pixeles), [i,2]Get(pixeles) ), Print(c) ); Prnl
Pause
 
Set video text
End
Subrutines
</syntaxhighlight>
EQ1:
Let(x := Div(x, 2))
Let(y := Div(y, 2))
Let(color:=9), Return
 
EQ2:
Let(x := Add( mid ancho, Div(Sub(mid ancho, x), 2) ) )
Let(y := Sub( alto, Div( Sub(alto, y), 2 )))
Let(color:=10), Return
EQ3:
Let(x := Sub(ancho, Div( Sub(ancho, x), 2)))
Let(y := Div(y, 2))
Let(color:=4), Return
 
</syntaxhighlight>
{{out}}
[[File:Chaos Game - salida_juego.png]]
 
=={{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.
<syntaxhighlight lang="basic">10 SCREEN 1
20 X = INT(RND(0) * 200)
30 Y = INT(RND(0) * 173)
Line 247 ⟶ 249:
150 PSET X,Y,V
160 NEXT I</syntaxhighlight>
 
==={{header|Applesoft BASIC}}===
Adapted from the code given above.
<syntaxhighlight lang="basic">10 HGR2
20 X = INT(RND(1) * 200)
30 Y = INT(RND(1) * 173)
Line 268 ⟶ 271:
 
==={{header|BASIC256}}===
<syntaxhighlight lang=BASIC256"basic256">#Chaos game
#Chaos game
 
ancho = 500 : alto = 300
Line 300 ⟶ 302:
Refresh
ImgSave "chaos_game.jpg", "jpg"
End</syntaxhighlight>
End
 
</syntaxhighlight>
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
<syntaxhighlight lang="qbasic">100 REM Chaos game
110 CLS
120 SCREEN 7 '320x200 EGA Color
130 X = INT(RND(1) * 200)
140 Y = INT(RND(1) * 173)
150 FOR I=1 TO 20000
160 V = INT(RND(1) * 3) + 1
170 ON V GOTO 180,210,240
180 X = X/2
190 Y = Y/2
200 GOTO 260
210 X = 100 + (100-X)/2
220 Y = 173 - (173-Y)/2
230 GOTO 260
240 X = 200 - (200-X)/2
250 Y = Y/2
260 PSET(X,Y),V
270 NEXT I
280 END</syntaxhighlight>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "ChaosGam.bas"
110 RANDOMIZE
120 GRAPHICS HIRES 4
130 LET X=RND(800):LET Y=RND(600)
140 FOR I=1 TO 20000
150 LET VERTEX=RND(3)
160 SELECT CASE VERTEX
170 CASE 0
180 LET X=X/2
190 LET Y=Y/2
200 CASE 1
210 LET X=400+(400-X)/2
220 LET Y=600-(600-Y)/2
230 CASE 2
240 LET X=800-(800-X)/2
250 LET Y=Y/2
260 END SELECT
270 SET INK VERTEX+1
280 PLOT X,Y
290 NEXT</syntaxhighlight>
 
==={{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.
<syntaxhighlight lang="locobasic">10 mode 1:randomize time:defint a-z
20 x = 640 * rnd
30 y = 400 * rnd
Line 321 ⟶ 367:
150 plot x,y,v
160 next i</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
<syntaxhighlight lang="qbasic">100 REM Chaos game
110 CLS
120 SCREEN 2
130 X = INT(RND(1) * 256)
140 Y = INT(RND(1) * 192)
150 FOR I=1 TO 20000
160 V = INT(RND(1) * 3) + 1
170 ON V GOTO 180,220,260
180 X = X/2
190 Y = Y/2
200 V = 8 'red
210 GOTO 290
220 X = 128 + (128-X)/2
230 Y = 192 - (192-Y)/2
240 V = 3 'green
250 GOTO 290
260 X = 256 - (256-X)/2
270 Y = Y/2
280 V = 7 'blue
290 PSET(X,Y),V
300 NEXT I
310 END</syntaxhighlight>
 
==={{header|Sinclair ZX81 BASIC}}===
Line 326 ⟶ 397:
 
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
20 LET Y=RND*40
30 FOR I=1 TO 5000
Line 346 ⟶ 417:
==={{header|ZX Spectrum Basic}}===
The final <code>INK</code> statement sets the foreground colour back to black.
<syntaxhighlight lang="basic"> 10 LET x=RND*200
20 LET y=RND*173
30 FOR i=1 TO 20000
Line 364 ⟶ 435:
170 NEXT i
180 INK 0</syntaxhighlight>
 
=={{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.
 
<syntaxhighlight lang=C"c">
#include<graphics.h>
#include<stdlib.h>
Line 422 ⟶ 492:
return 0;
}</syntaxhighlight>
 
=={{header|C sharp|C#}}==
 
<syntaxhighlight lang="csharp">using System.Diagnostics;
using System.Drawing;
 
Line 456 ⟶ 525:
}
}</syntaxhighlight>
 
=={{header|C++}}==
This program will generate the Sierpinski Triangle and save it to your hard drive.
<syntaxhighlight lang="cpp">
#include <windows.h>
#include <ctime>
Line 609 ⟶ 677:
}
</syntaxhighlight>
 
=={{header|Common Lisp}}==
{{libheader|opticl}}
<syntaxhighlight lang="lisp">(defpackage #:chaos
(:use #:cl
#:opticl))
Line 648 ⟶ 715:
{{libheader| Vcl.ExtCtrls}}
{{libheader| System.Generics.Collections}}
<syntaxhighlight lang=Delphi"delphi">
unit main;
 
Line 771 ⟶ 838:
=={{header|EasyLang}}==
 
[https://easylang.dev/show/#cod=Zc1BCoMwEIXhfU7xL20FO1aKuMhJQhbFKgrVQAiS3L5MkW66GJj5eMwbwztEBhGTncfiEFoRHoI35aSenqHDm4wlPvdX2GaumjPlT+YQWbG0pKAgIgbYwjGRKbrHaUxI0+novZxP1j3xBe2pMjXZLf7CjbuqdlWFmvLTxnwA Run it]
[https://easylang.online/apps/_chaos-game.html Run it]
 
<syntaxhighlight lang=text>color 900
color 900
x[] = [ 0 100 50 ]
y[] = [ 93 937 7 93 ]
x = randomf * 100
y = randomf * 100
for i range= 1 to 100000
move x y
rect 0.3 0.3
h = randomrandint 3
x = (x + x[h]) / 2
y = (y + y[h]) / 2
.
.</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">; Chaos game
 
(defun make-array (size)
Line 839 ⟶ 908:
 
(chaos 400 180 50000) </syntaxhighlight>
 
=={{header|F Sharp|F#}}==
<syntaxhighlight lang="fsharp">
open System.Windows.Forms
open System.Drawing
Line 866 ⟶ 934:
f.Show()
</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}}==
{{works with|gforth|0.7.3}}
<br>
<syntaxhighlight lang="forth">#! /usr/bin/gforth
\ Chaos Game
 
Line 1,115 ⟶ 1,084:
<tt>./chaos-game.fs 1000 1000000 chao-game.pbm</tt>:]
[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æ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Chaos_game}}
 
'''Solution'''
 
When using n points equally spaced over a circle we get a [https://en.wikipedia.org/wiki/N-flake Sierpiński n-gon]. However, the proportion (1/2 for a triangle) is different according to the following formula:
 
[[File:Fōrmulæ - Chaos game 01.png]]
 
[[File:Fōrmulæ - Chaos game 02.png]]
 
[[File:Fōrmulæ - Chaos game 03.png]]
 
The program is:
 
[[File:Fōrmulæ - Chaos game 04.png]]
 
'''Test cases'''
 
[[File:Fōrmulæ - Chaos game 05.png]]
 
[[File:Fōrmulæ - Chaos game 06.png]]
 
Notice that the Sierpiński's 4-gon is NOT the [https://en.wikipedia.org/wiki/Sierpi%C5%84ski_carpet Sierpiński carpet], it is a solid square
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
void local fn DoIt
long w = 460, h = 400, i, x = rnd(w), y = rnd(h)
for i = 1 to 50000
select ( rnd(3)-1 )
case 1
x = w/2+(w/2-x)/2
y = h-(h-y)/2
pen ,fn ColorRed
case 2
x = w-(w-x)/2
y = y/2
pen ,fn ColorGreen
case else
x = x/2
y = y/2
pen ,fn ColorBlue
end select
line x-0.5,y-0.5,x+0.5,y+0.5
next
end fn
 
window 1, @"Chaos Game", (0,0,460,400)
WindowSetBackgroundColor( 1, fn ColorWhite )
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
[[file:Chaos game FB.png]]
 
=={{header|GML}}==
Line 1,121 ⟶ 1,235:
 
'''"Game" Object Create Event:'''
<syntaxhighlight lang=GML"gml">offset = 32; //Distance from triangle vertices to edges of window
 
//triangle vertex coordinates
Line 1,155 ⟶ 1,269:
 
'''"Game" Object Step Event:'''
<syntaxhighlight lang=GML"gml">if(step and step_count < max_iterations) //Wait for alarm to finish, or stop completely
{ // if the desired number of iterations is hit
vertex = choose(1, 2, 3);
Line 1,192 ⟶ 1,306:
 
'''"Game" Object Draw Event:'''
<syntaxhighlight lang=GML"gml">if(step_count < max_iterations)
{
draw_triangle(x1, y1, x2, y2, x3, y3, true);
Line 1,201 ⟶ 1,315:
 
'''"Game" Object Alarm 0:'''
<syntaxhighlight lang=GML"gml">step = true;
alarm[0] = interval;</syntaxhighlight>
 
 
'''"Point" Object Draw Event:'''
<syntaxhighlight lang=GML"gml">draw_circle(x, y, 5, false);</syntaxhighlight>
 
=={{header|Gnuplot}}==
{{trans|PARI/GP}}
Line 1,213 ⟶ 1,326:
[[File:ChGS3Gnu1.png|right|thumb|Output ChGS3Gnu1.png]]
 
<syntaxhighlight lang="gnuplot">
## Chaos Game (Sierpinski triangle) 2/16/17 aev
reset
Line 1,246 ⟶ 1,359:
File: ChGS3Gnu1.png
</pre>
 
=={{header|Go}}==
This writes a simple GIF animation of the method.
<syntaxhighlight lang=Go"go">package main
 
import (
Line 1,354 ⟶ 1,466:
return err
}</syntaxhighlight>
 
=={{header|Groovy}}==
 
{{libheader|JavaFX}}
 
<syntaxhighlight lang=Groovy"groovy">import javafx.animation.AnimationTimer
import javafx.application.Application
import javafx.scene.Scene
Line 1,428 ⟶ 1,539:
}
}</syntaxhighlight>
 
=={{header|Haskell}}==
 
<syntaxhighlight lang="haskell">import Control.Monad (replicateM)
import Control.Monad.Random (fromList)
 
Line 1,444 ⟶ 1,554:
Some transformations:
 
<syntaxhighlight lang="haskell">-- the Sierpinsky`s triangle
triangle = [ (mid (0, 0), 1)
, (mid (1, 0), 1)
Line 1,463 ⟶ 1,573:
 
Drawing the result:
<syntaxhighlight lang="haskell">import Control.Monad.Random (getRandomR)
import Graphics.Gloss
 
Line 1,472 ⟶ 1,582:
where window = InWindow "Game of Chaos" (400,400) (0,0)
point (x,y) = translate (100*x) (100*y) $ circle 0.02 </syntaxhighlight>
 
=={{header|J}}==
[[File:j_chaos_game.png|300px|thumb|right]]
<syntaxhighlight lang="j">
Note 'plan, Working in complex plane'
Make an equilateral triangle.
Line 1,510 ⟶ 1,619:
'marker'plot NEW_POINTS
</syntaxhighlight>
 
=={{header|Java}}==
[[File:chaos_game.png|300px|thumb|right]]
{{works with|Java|8}}
<syntaxhighlight lang="java">import java.awt.*;
import java.awt.event.*;
import java.util.*;
Line 1,603 ⟶ 1,711:
}
}</syntaxhighlight>
 
=={{header|JavaScript}}==
Plots the fractal on an HTML <tt>canvas</tt> element.
<syntaxhighlight lang="javascript"><html>
 
<head>
Line 1,659 ⟶ 1,766:
 
</html></syntaxhighlight>
 
=={{header|Julia}}==
Run in REPL.
<syntaxhighlight lang="julia">using Luxor
 
function chaos()
Line 1,693 ⟶ 1,799:
preview()
</syntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|Java}}
<syntaxhighlight lang="scala">//Version 1.1.51
 
import java.awt.*
Line 1,781 ⟶ 1,886:
Same as Java entry
</pre>
 
=={{header|Logo}}==
<syntaxhighlight lang="logo">to chaosgame :sidelength :iterations
make "width :sidelength
make "height (:sidelength/2 * sqrt 3)
Line 1,812 ⟶ 1,916:
hideturtle
end</syntaxhighlight>
 
=={{header|Lua}}==
Needs L&Ouml;VE 2d Engine
<syntaxhighlight lang=Lua"lua">
math.randomseed( os.time() )
colors, orig = { { 255, 0, 0 }, { 0, 255, 0 }, { 0, 0, 255 } }, {}
Line 1,857 ⟶ 1,960:
end
</syntaxhighlight>
 
=={{header|M2000 Interpreter}}==
{{trans|Locomotive Basic}}
Old style programming (GOTO, ON GOTO) inside Module. Export bitmap to Clipboard.
 
 
<syntaxhighlight lang="m2000 interpreter">
Module Chaos {
01 Read Ox as integer, Oy as Integer
02 def Screen$
05 cls 0,0 // black background, row for split screen
10 def integer x,y,i,v
20 x = 640 * rnd
30 y = 400 * rnd
40 for i=1 to 20000
50 v = rnd * 2 + 1
60 on v goto 70,100,130
70 x = x/2
80 y = y/2
90 goto 150
100 x = 320 + (320-x)/2
110 y = 400 - (400-y)/2
120 goto 150
130 x = 640 - (640-x)/2
140 y = y/2
150 pset v*2, x*twipsX+Ox,y*twipsY+Oy
160 next i
170 Move Ox, Oy
180 Copy 640*twipsX, 400*twipsY to Screen$
190 Clipboard Screen$
}
Call Chaos 3000, 3000
</syntaxhighlight>
{{output}}
[[File:Chaosgame.png|thumb]]
 
 
=={{header|Maple}}==
<syntaxhighlight lang="maple">chaosGame := proc(numPoints)
local points, i;
randomize();
Line 1,877 ⟶ 2,016:
end use;
end proc:</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">points = 5000;
a = {0, 0};
b = {1, 0};
Line 1,889 ⟶ 2,027:
If[t == 1, d = Mean[{b, d}], d = Mean[{c, d}]]]; AppendTo[S, d]]
Graphics[Point[S]]</syntaxhighlight>
 
=={{header|Nim}}==
==={{header|Using a game library}}===
{{libheader|rapid}}
The "rapid" library is no longer maintained and this program fails to compile with last available version.
<syntaxhighlight lang="nim">import random
 
import rapid/gfx
Line 1,934 ⟶ 2,071:
==={{header|Using SDL}}===
{{libheader|SDL2}}
<syntaxhighlight lang="nim">## needs sdl2 ("nimble install sdl2")
 
import sdl2, random
Line 1,995 ⟶ 2,132:
{{libheader|imageman}}
 
<syntaxhighlight lang=Nim"nim">import math
import random
 
Line 2,072 ⟶ 2,209:
 
image.savePNG("chaos_game.png", compression = 9)</syntaxhighlight>
 
=={{header|PARI/GP}}==
Note: Find plotmat() here on RosettaCode Wiki.
{{Works with|PARI/GP|2.9.1 and above}}
[[File:SierpTri1.png|right|thumb|Output SierpTri1.png]]
<syntaxhighlight lang="parigp">
\\ Chaos Game (Sierpinski triangle) 2/15/17 aev
pChaosGameS3(size,lim)={
Line 2,100 ⟶ 2,236:
time = 751 ms.
</pre>
 
=={{header|Pascal}}==
<syntaxhighlight lang=Pascal"pascal">
program ChaosGame;
 
Line 2,181 ⟶ 2,316:
 
</syntaxhighlight>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">use Imager;
 
my $width = 1000;
Line 2,221 ⟶ 2,355:
 
$img->write(file => 'chaos_game_triangle.png');</syntaxhighlight>
 
=={{header|Phix}}==
Implements five of the fractals on the wikipedia page.
Line 2,227 ⟶ 2,360:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/chaos.htm here]. Press space to cycle through the five fractals.
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Chaos_game.exw
Line 2,324 ⟶ 2,457:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
 
=={{header|Plain English}}==
<syntaxhighlight lang="plainenglish">To run:
Start up.
Initialize our reference points.
Line 2,370 ⟶ 2,502:
{{out}}
[https://commons.wikimedia.org/wiki/File:Chaos-game.png]
 
=={{header|Processing}}==
<syntaxhighlight lang="java">size(300, 260);
 
background(#ffffff); // white
Line 2,404 ⟶ 2,535:
==={{header|Processing Python mode}}===
 
<syntaxhighlight lang="python">from __future__ import division
 
size(300, 260)
Line 2,429 ⟶ 2,560:
 
set(x, height - y, colour)</syntaxhighlight>
 
=={{header|Python}}==
 
<syntaxhighlight lang=Python"python">
import argparse
import random
Line 2,550 ⟶ 2,680:
 
</syntaxhighlight>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ $ "turtleduck.qky" loadfile ] now!
 
[ 2 3 circle ] is dot ( --> )
 
[ 1 fly
-1 4 turn
1 fly
1 4 turn ] is toxy ( n n --> )
 
[ swap 2dup toxy
dot
1 2 turn
toxy
1 2 turn ] is plot ( n n --> )
 
[ 200 random
346 random
over 346 *
over 200 * < if
[ dip [ 400 + ]
692 swap - ] ] is intriangle ( --> n n )
 
[ 3 random
[ table
[ 2 /
dip [ 2 / ] ]
[ 2 /
dip [ 800 + 2 / ] ]
[ 692 + 2 /
dip [ 400 + 2 / ] ] ]
do ] is tovertex ( n n --> )
 
turtle
0 frames
-400 1 fly
1 4 turn
340 1 fly
-1 4 turn
intriangle
10000 times
[ i^ 100 mod 0= if frame
2dup plot
tovertex ]
2drop
1 frames</syntaxhighlight>
 
{{out}}
 
[[File:Quackery chaos game.png|thumb|center]]
 
=={{header|R}}==
Line 2,556 ⟶ 2,738:
{{Works with|R|3.3.1 and above}}
[[File:SierpTriR1.png|right|thumb|Output SierpTriR1.png]]
<syntaxhighlight lang="r">
# Chaos Game (Sierpinski triangle) 2/15/17 aev
# pChaosGameS3(size, lim, clr, fn, ttl)
Line 2,591 ⟶ 2,773:
'''Alternative code:'''
 
<syntaxhighlight lang="r">
pta = c(1,2)
ptb = c(4,2)
Line 2,623 ⟶ 2,805:
}
</syntaxhighlight>
 
=={{header|Racket}}==
 
{{trans|Haskell}}
 
<syntaxhighlight lang="racket">#lang racket
 
(require 2htdp/image)
Line 2,678 ⟶ 2,859:
(draw-fern)
(draw-dragon)</syntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.10}}
 
<syntaxhighlight lang="raku" line>use Image::PNG::Portable;
 
my ($w, $h) = (640, 640);
Line 2,701 ⟶ 2,881:
 
$png.write: 'Chaos-game-perl6.png';</syntaxhighlight>
 
=={{header|REXX}}==
<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.*/
sw= sw - 2 /*adjust the screen width down by two. */
Line 2,943 ⟶ 3,122:
</pre>
</b>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project : Chaos game
 
Line 3,007 ⟶ 3,185:
</syntaxhighlight>
* [https://lh3.googleusercontent.com/-xqBO5MB8fpc/Wg05SvwaF9I/AAAAAAAABDA/UGI2goKdDoAR6nbbGZF0YcuwGG6tancvACLcBGAs/s1600/CalmoSoftChaos.jpg Chaos Game (image)]
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">x = int(rnd(0) * 200)
y = int(rnd(0) * 173)
graphic #g, 200,200
Line 3,030 ⟶ 3,207:
next
render #g</syntaxhighlight>
 
=={{header|Rust}}==
Dependencies: image, rand
<syntaxhighlight lang="rust">
extern crate image;
extern crate rand;
Line 3,077 ⟶ 3,253:
}
</syntaxhighlight>
 
=={{header|Scala}}==
===Java Swing Interoperability===
<syntaxhighlight lang=Scala"scala">import javax.swing._
import java.awt._
import java.awt.event.ActionEvent
Line 3,149 ⟶ 3,324:
 
}</syntaxhighlight>
 
=={{header|Scilab}}==
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
n_sides = 3;
side_length = 1;
Line 3,200 ⟶ 3,374:
It outputs a graphic window and prints on the console the time elapsed during iterations.
<pre>Time: 1.0424433s.</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">require('Imager')
 
var width = 600
Line 3,238 ⟶ 3,411:
img.write(file => 'chaos_game.png')</syntaxhighlight>
Output image: [https://github.com/trizen/rc/blob/master/img/chaos-game-sidef.png Chaos game]
 
=={{header|Simula}}==
<syntaxhighlight lang="simula">BEGIN
INTEGER U, COLUMNS, LINES;
COLUMNS := 40;
Line 3,327 ⟶ 3,499:
XXX
</pre>
=={{header|Uiua}}==
<syntaxhighlight lang="Uiua">
Dim ← 500
Points ← [[50 ⌊÷2Dim] [-50Dim 50] [-50Dim -50Dim]]
# Uncomment to try different square or pentagonal references.
# Points ← [[50 50] [-50Dim 50] [50 -50Dim][-50Dim -50Dim]]
# Points ← [[50 ⌊÷2Dim] [⌊×0.4Dim 50] [⌊×0.4Dim -50Dim] [-50Dim 120] [-50Dim -120Dim]]
Colours ← [[1 0 0] [0 1 0] [0 0 1] [1 1 0] [1 0 1] [0 1 1]]
Rand ← ⌊×⚂
 
↯Dim_Dim_3 0
∧(⍜(⊡|[1 1 1]◌))Points
⊟Rand Dim Rand Dim
⍥(
Rand⧻Points
# Set next point and colour based on target.
⊃(⌊÷2+⊡:Points|⊙◌⊡:Colours)
⟜⍜⊡◌⊙:
)10000
# Uncomment to save image.
# &fwa "UiuaChaosGameSerpinski.png" &ime "png"
 
</syntaxhighlight>
{{out}}
[[File:UiuaChaosGameSerpinski.png|thumb|center|Render using colours associated with each target point.]]
 
=={{header|Wren}}==
Line 3,333 ⟶ 3,531:
{{libheader|Wren-dynamic}}
{{libheader|Wren-seq}}
<syntaxhighlight lang=ecmascript"wren">import "dome" for Window
import "graphics" for Canvas, Color
import "math" for Point
Line 3,398 ⟶ 3,596:
=={{header|X86 Assembly}}==
Sixty bytes handles it.
<syntaxhighlight lang="asm"> 1 ;Assemble with: tasm, tlink /t
2 0000 .model tiny
3 0000 .code
Line 3,436 ⟶ 3,634:
37 0136 0000 01DF 01DF Ty dw 0, 479, 479
38 end start</syntaxhighlight>
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"xpl0">int Tx, Ty, X, Y, R;
[SetVid($12); \640x480x4 graphics
Tx:= [320, 320-277, 320+277]; \equilateral triangle
Line 3,451 ⟶ 3,648:
SetVid($03); \restore normal text mode
]</syntaxhighlight>
 
=={{header|Yabasic}}==
<syntaxhighlight lang=Yabasic"yabasic">width = 640 : height = 480
open window width, height
window origin "lb"
Line 3,475 ⟶ 3,671:
dot x, y
next</syntaxhighlight>
 
=={{header|Z80 Assembly}}==
{{trans|BASIC}}
Line 3,483 ⟶ 3,678:
space key.
 
<syntaxhighlight lang="z80">VREG: equ 99h ; VDP register port
VR0: equ 0F3DFh ; Copy of VDP R0 in memory
VR1: equ 0F3E0h ; Copy of VDP R1 in memory
Line 3,653 ⟶ 3,848:
exx
ret</syntaxhighlight>
 
 
=={{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.
Line 3,661 ⟶ 3,854:
{{trans|Java}}
[[File:ChaosGame.zkl.jpg|240px|thumb|right]]
<syntaxhighlight lang="zkl">w,h:=640,640;
bitmap:=PPM(w,h,0xFF|FF|FF); // White background
colors:=T(0xFF|00|00,0x00|FF|00,0x00|00|FF); // red,green,blue
2,120

edits