Chaos game: Difference between revisions

m
syntax highlighting fixup automation
(implementation in Forth)
m (syntax highlighting fixup automation)
Line 21:
This program will run on a PC with CGA-compatible graphics. It will keep running until a key is pressed.
 
<langsyntaxhighlight lang=asm> cpu 8086
bits 16
vmode: equ 0Fh ; Get current video mode
Line 142:
xchg bx,bp ; Restore the registers
xchg cx,di
ret</langsyntaxhighlight>
 
=={{header|Action!}}==
<langsyntaxhighlight lang=Action!>PROC Main()
INT x,w=[220],h=[190]
BYTE y,i,CH=$02FC,COLOR1=$02C5,COLOR2=$02C6
Line 172:
OD
CH=$FF
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Chaos_game.png Screenshot from Atari 8-bit computer]
Line 179:
{{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>
<langsyntaxhighlight lang=Amazing Hopper>
/* Chaos game - JAMBO hopper */
 
Line 226:
Set video text
End
</syntaxhighlight>
</lang>
 
 
=={{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.
<langsyntaxhighlight lang=basic>10 SCREEN 1
20 X = INT(RND(0) * 200)
30 Y = INT(RND(0) * 173)
Line 246:
140 Y = Y/2
150 PSET X,Y,V
160 NEXT I</langsyntaxhighlight>
==={{header|Applesoft BASIC}}===
Adapted from the code given above.
<langsyntaxhighlight lang=basic>10 HGR2
20 X = INT(RND(1) * 200)
30 Y = INT(RND(1) * 173)
Line 265:
150 HCOLOR=V+4
160 HPLOT X,Y
170 NEXT I</langsyntaxhighlight>
 
==={{header|BASIC256}}===
<langsyntaxhighlight lang=BASIC256>
#Chaos game
 
Line 301:
ImgSave "chaos_game.jpg", "jpg"
End
</syntaxhighlight>
</lang>
 
==={{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.
<langsyntaxhighlight lang=locobasic>10 mode 1:randomize time:defint a-z
20 x = 640 * rnd
30 y = 400 * rnd
Line 320:
140 y = y/2
150 plot x,y,v
160 next i</langsyntaxhighlight>
 
==={{header|Sinclair ZX81 BASIC}}===
Line 326:
 
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.
<langsyntaxhighlight lang=basic> 10 LET X=RND*46
20 LET Y=RND*40
30 FOR I=1 TO 5000
Line 340:
130 LET Y=Y/2
140 PLOT X,42-Y
150 NEXT I</langsyntaxhighlight>
{{out}}
Screenshot [http://www.edmundgriffiths.com/zx81chaosgame.jpg here]. As with most ZX81 graphics, you can obtain the very best results by making it quite small and looking at it from a long way away.
Line 346:
==={{header|ZX Spectrum Basic}}===
The final <code>INK</code> statement sets the foreground colour back to black.
<langsyntaxhighlight lang=basic> 10 LET x=RND*200
20 LET y=RND*173
30 FOR i=1 TO 20000
Line 363:
160 PLOT x,y
170 NEXT i
180 INK 0</langsyntaxhighlight>
 
=={{header|C}}==
Line 369:
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>
<lang C>
#include<graphics.h>
#include<stdlib.h>
Line 421:
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
 
<langsyntaxhighlight lang=csharp>using System.Diagnostics;
using System.Drawing;
 
Line 455:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
This program will generate the Sierpinski Triangle and save it to your hard drive.
<langsyntaxhighlight lang=cpp>
#include <windows.h>
#include <ctime>
Line 608:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
{{libheader|opticl}}
<langsyntaxhighlight lang=lisp>(defpackage #:chaos
(:use #:cl
#:opticl))
Line 640:
(setf (pixel image (first point) (second point))
(values 255 0 0)))
(write-png-file "chaos.png" image)))</langsyntaxhighlight>
=={{header|Delphi}}==
{{libheader| Winapi.Windows}}
Line 648:
{{libheader| Vcl.ExtCtrls}}
{{libheader| System.Generics.Collections}}
<langsyntaxhighlight lang=Delphi>
unit main;
 
Line 768:
end;
end;
end.</langsyntaxhighlight>
=={{header|EasyLang}}==
 
[https://easylang.online/apps/_chaos-game.html Run it]
 
<syntaxhighlight lang=text>color 900
x[] = [ 0 100 50 ]
y[] = [ 93 93 7 ]
Line 784:
x = (x + x[h]) / 2
y = (y + y[h]) / 2
.</langsyntaxhighlight>
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight lang=lisp>; Chaos game
 
(defun make-array (size)
Line 838:
(chaos-show arr size)))
 
(chaos 400 180 50000) </langsyntaxhighlight>
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang=fsharp>
open System.Windows.Forms
open System.Drawing
Line 865:
f.Paint.Add (fun args -> args.Graphics.DrawImage(bmp, Point(0, 0)))
f.Show()
</syntaxhighlight>
</lang>
 
=={{header|Fortran}}==
This FORTRAN code creates an output file which can be drawn with gnuplot.
<langsyntaxhighlight lang=Fortran>
PROGRAM CHAOS
IMPLICIT NONE
Line 913:
END FUNCTION ZAHL
END PROGRAM CHAOS
</syntaxhighlight>
</lang>
Gnuplot Code to draw file:
<langsyntaxhighlight lang=Gnuplot>
set terminal jpeg enhanced size 1600,960
set output 'chaos.jpg'
Line 921:
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>
</lang>
 
=={{header|FreeBASIC}}==
{{trans|BASIC256}}
<langsyntaxhighlight lang=freebasic>
' Chaos game
Const ancho = 320, alto = 240
Line 955:
Sleep
End
</syntaxhighlight>
</lang>
 
=={{header|Fōrmulæ}}==
Line 968:
{{works with|gforth|0.7.3}}
<br>
<langsyntaxhighlight lang=forth>#! /usr/bin/gforth
\ Chaos Game
 
Line 1,109:
STEPS game OUT-FILE to-pbm
 
bye</langsyntaxhighlight>
 
{{out}}
Line 1,121:
 
'''"Game" Object Create Event:'''
<langsyntaxhighlight lang=GML>offset = 32; //Distance from triangle vertices to edges of window
 
//triangle vertex coordinates
Line 1,151:
step_count = 0;
interval = 1; //Number of frames between each step. 1 = no delay
alarm[0] = interval;</langsyntaxhighlight>
 
 
'''"Game" Object Step Event:'''
<langsyntaxhighlight 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
vertex = choose(1, 2, 3);
Line 1,188:
step_count++;
}</langsyntaxhighlight>
 
 
'''"Game" Object Draw Event:'''
<langsyntaxhighlight lang=GML>if(step_count < max_iterations)
{
draw_triangle(x1, y1, x2, y2, x3, y3, true);
draw_circle(px, py, 1, false);
draw_line(px, py, vx, vy);
}</langsyntaxhighlight>
 
 
'''"Game" Object Alarm 0:'''
<langsyntaxhighlight lang=GML>step = true;
alarm[0] = interval;</langsyntaxhighlight>
 
 
'''"Point" Object Draw Event:'''
<langsyntaxhighlight lang=GML>draw_circle(x, y, 5, false);</langsyntaxhighlight>
 
=={{header|Gnuplot}}==
Line 1,213:
[[File:ChGS3Gnu1.png|right|thumb|Output ChGS3Gnu1.png]]
 
<langsyntaxhighlight lang=gnuplot>
## Chaos Game (Sierpinski triangle) 2/16/17 aev
reset
Line 1,241:
set output
unset print
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 1,249:
=={{header|Go}}==
This writes a simple GIF animation of the method.
<langsyntaxhighlight lang=Go>package main
 
import (
Line 1,353:
}
return err
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
Line 1,359:
{{libheader|JavaFX}}
 
<langsyntaxhighlight lang=Groovy>import javafx.animation.AnimationTimer
import javafx.application.Application
import javafx.scene.Scene
Line 1,427:
launch(ChaosGame)
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
 
<langsyntaxhighlight lang=haskell>import Control.Monad (replicateM)
import Control.Monad.Random (fromList)
 
Line 1,440:
gameOfChaos :: MonadRandom m => Int -> Transformations -> Point -> m [Point]
gameOfChaos n transformations x = iterateA (fromList transformations) x
where iterateA f x = scanr ($) x <$> replicateM n f</langsyntaxhighlight>
 
Some transformations:
 
<langsyntaxhighlight lang=haskell>-- the Sierpinsky`s triangle
triangle = [ (mid (0, 0), 1)
, (mid (1, 0), 1)
Line 1,460:
dragon = [(f1, 1), (f2, 1)]
where f1 (x,y) = (0.5*x - 0.5*y, 0.5*x + 0.5*y)
f2 (x,y) = (-0.5*x + 0.5*y+1, -0.5*x - 0.5*y)</langsyntaxhighlight>
 
Drawing the result:
<langsyntaxhighlight lang=haskell>import Control.Monad.Random (getRandomR)
import Graphics.Gloss
 
Line 1,471:
display window white $ foldMap point pts
where window = InWindow "Game of Chaos" (400,400) (0,0)
point (x,y) = translate (100*x) (100*y) $ circle 0.02 </langsyntaxhighlight>
 
=={{header|J}}==
[[File:j_chaos_game.png|300px|thumb|right]]
<syntaxhighlight lang=j>
<lang j>
Note 'plan, Working in complex plane'
Make an equilateral triangle.
Line 1,509:
 
'marker'plot NEW_POINTS
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
[[File:chaos_game.png|300px|thumb|right]]
{{works with|Java|8}}
<langsyntaxhighlight lang=java>import java.awt.*;
import java.awt.event.*;
import java.util.*;
Line 1,602:
});
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Plots the fractal on an HTML <tt>canvas</tt> element.
<langsyntaxhighlight lang=javascript><html>
 
<head>
Line 1,658:
</body>
 
</html></langsyntaxhighlight>
 
=={{header|Julia}}==
Run in REPL.
<langsyntaxhighlight lang=julia>using Luxor
 
function chaos()
Line 1,692:
finish()
preview()
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang=scala>//Version 1.1.51
 
import java.awt.*
Line 1,775:
}
}
}</langsyntaxhighlight>
 
{{output}}
Line 1,783:
 
=={{header|Logo}}==
<langsyntaxhighlight lang=logo>to chaosgame :sidelength :iterations
make "width :sidelength
make "height (:sidelength/2 * sqrt 3)
Line 1,811:
]
hideturtle
end</langsyntaxhighlight>
 
=={{header|Lua}}==
Needs L&Ouml;VE 2d Engine
<langsyntaxhighlight lang=Lua>
math.randomseed( os.time() )
colors, orig = { { 255, 0, 0 }, { 0, 255, 0 }, { 0, 0, 255 } }, {}
Line 1,856:
love.graphics.draw( canvas )
end
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
<langsyntaxhighlight lang=maple>chaosGame := proc(numPoints)
local points, i;
randomize();
Line 1,876:
plots:-display( seq([plots:-display([seq(point(points[i]), i = 1..j)])], j = 1..numelems(points) ), insequence=true);
end use;
end proc:</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight lang=Mathematica>points = 5000;
a = {0, 0};
b = {1, 0};
Line 1,888:
If[t == 0, d = Mean[{a, d}],
If[t == 1, d = Mean[{b, d}], d = Mean[{c, d}]]]; AppendTo[S, d]]
Graphics[Point[S]]</langsyntaxhighlight>
 
=={{header|Nim}}==
Line 1,894:
{{libheader|rapid}}
The "rapid" library is no longer maintained and this program fails to compile with last available version.
<langsyntaxhighlight lang=nim>import random
 
import rapid/gfx
Line 1,930:
ctx.noTexture()
update step:
discard</langsyntaxhighlight>
 
==={{header|Using SDL}}===
{{libheader|SDL2}}
<langsyntaxhighlight lang=nim>## needs sdl2 ("nimble install sdl2")
 
import sdl2, random
Line 1,990:
 
destroy render
destroy window</langsyntaxhighlight>
 
==={{header|Writing result into an image}}===
{{libheader|imageman}}
 
<langsyntaxhighlight lang=Nim>import math
import random
 
Line 2,071:
p = ((p.x + T[idx].x) / 2, (p.y + T[idx].y) / 2)
 
image.savePNG("chaos_game.png", compression = 9)</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
Line 2,077:
{{Works with|PARI/GP|2.9.1 and above}}
[[File:SierpTri1.png|right|thumb|Output SierpTri1.png]]
<langsyntaxhighlight lang=parigp>
\\ Chaos Game (Sierpinski triangle) 2/15/17 aev
pChaosGameS3(size,lim)={
Line 2,093:
\\ Test:
pChaosGameS3(600,30000); \\ SierpTri1.png
</langsyntaxhighlight>
{{Output}}
<pre>
Line 2,102:
 
=={{header|Pascal}}==
<langsyntaxhighlight lang=Pascal>
program ChaosGame;
 
Line 2,180:
end.
 
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
<langsyntaxhighlight lang=perl>use Imager;
 
my $width = 1000;
Line 2,220:
}
 
$img->write(file => 'chaos_game_triangle.png');</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 2,227:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/chaos.htm here]. Press space to cycle through the five fractals.
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Chaos_game.exw
Line 2,323:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Plain English}}==
<langsyntaxhighlight lang=plainenglish>To run:
Start up.
Initialize our reference points.
Line 2,367:
If the number is 1, put the top spot into the reference spot.
If the number is 2, put the right spot into the reference spot.
If the number is 3, put the left spot into the reference spot.</langsyntaxhighlight>
{{out}}
[https://commons.wikimedia.org/wiki/File:Chaos-game.png]
 
=={{header|Processing}}==
<langsyntaxhighlight lang=java>size(300, 260);
 
background(#ffffff); // white
Line 2,400:
}
set(x, height-y, colour);
}</langsyntaxhighlight>
 
==={{header|Processing Python mode}}===
 
<langsyntaxhighlight lang=python>from __future__ import division
 
size(300, 260)
Line 2,428:
colour = color(0, 0, 255) # blue
 
set(x, height - y, colour)</langsyntaxhighlight>
 
=={{header|Python}}==
 
<langsyntaxhighlight lang=Python>
import argparse
import random
Line 2,549:
main(arg_parser.parse_args())
 
</syntaxhighlight>
</lang>
 
=={{header|R}}==
Line 2,556:
{{Works with|R|3.3.1 and above}}
[[File:SierpTriR1.png|right|thumb|Output SierpTriR1.png]]
<syntaxhighlight lang=r>
<lang r>
# Chaos Game (Sierpinski triangle) 2/15/17 aev
# pChaosGameS3(size, lim, clr, fn, ttl)
Line 2,580:
}
pChaosGameS3(600, 30000, "red", "SierpTriR1", "Sierpinski triangle")
</langsyntaxhighlight>
{{Output}}
<pre>
Line 2,591:
'''Alternative code:'''
 
<syntaxhighlight lang=r>
<lang r>
pta = c(1,2)
ptb = c(4,2)
Line 2,622:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Line 2,628:
{{trans|Haskell}}
 
<langsyntaxhighlight lang=racket>#lang racket
 
(require 2htdp/image)
Line 2,677:
(draw-triangle)
(draw-fern)
(draw-dragon)</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 2,683:
{{works with|Rakudo|2018.10}}
 
<syntaxhighlight lang=raku perl6line>use Image::PNG::Portable;
 
my ($w, $h) = (640, 640);
Line 2,700:
}
 
$png.write: 'Chaos-game-perl6.png';</langsyntaxhighlight>
 
=={{header|REXX}}==
<langsyntaxhighlight 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,728:
/* [↑] strip trailing blanks (output).*/
say strip(_, 'T') /*display one row (line) of the image. */
end /*row*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
 
This REXX program makes use of &nbsp; '''SCRSIZE''' &nbsp; REXX program (or
Line 2,945:
 
=={{header|Ring}}==
<langsyntaxhighlight lang=ring>
# Project : Chaos game
 
Line 3,005:
}
label1 {setpicture(p1) show()}
</syntaxhighlight>
</lang>
* [https://lh3.googleusercontent.com/-xqBO5MB8fpc/Wg05SvwaF9I/AAAAAAAABDA/UGI2goKdDoAR6nbbGZF0YcuwGG6tancvACLcBGAs/s1600/CalmoSoftChaos.jpg Chaos Game (image)]
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang=runbasic>x = int(rnd(0) * 200)
y = int(rnd(0) * 173)
graphic #g, 200,200
Line 3,029:
#g set(x,y)
next
render #g</langsyntaxhighlight>
 
=={{header|Rust}}==
Dependencies: image, rand
<langsyntaxhighlight lang=rust>
extern crate image;
extern crate rand;
Line 3,076:
imgbuf.save("fractal.png").unwrap();
}
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
===Java Swing Interoperability===
<langsyntaxhighlight lang=Scala>import javax.swing._
import java.awt._
import java.awt.event.ActionEvent
Line 3,148:
)
 
}</langsyntaxhighlight>
 
=={{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,196:
plot2d(real(points),imag(points),0)
plot2d(real(vertices),imag(vertices),-3);
set(gca(),'isoview','on');</langsyntaxhighlight>
{{out}}
It outputs a graphic window and prints on the console the time elapsed during iterations.
Line 3,202:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang=ruby>require('Imager')
 
var width = 600
Line 3,236:
}
 
img.write(file => 'chaos_game.png')</langsyntaxhighlight>
Output image: [https://github.com/trizen/rc/blob/master/img/chaos-game-sidef.png Chaos game]
 
=={{header|Simula}}==
<langsyntaxhighlight lang=simula>BEGIN
INTEGER U, COLUMNS, LINES;
COLUMNS := 40;
Line 3,279:
END;
END
</syntaxhighlight>
</lang>
{{in}}
<pre>
Line 3,333:
{{libheader|Wren-dynamic}}
{{libheader|Wren-seq}}
<langsyntaxhighlight lang=ecmascript>import "dome" for Window
import "graphics" for Canvas, Color
import "math" for Point
Line 3,394:
}
 
var Game = ChaosGame.new(640, 640)</langsyntaxhighlight>
 
=={{header|X86 Assembly}}==
Sixty bytes handles it.
<langsyntaxhighlight lang=asm> 1 ;Assemble with: tasm, tlink /t
2 0000 .model tiny
3 0000 .code
Line 3,435:
36 0130 0140 002B 0255 Tx dw 320, 320-277, 320+277 ;equilateral triangle
37 0136 0000 01DF 01DF Ty dw 0, 479, 479
38 end start</langsyntaxhighlight>
 
=={{header|XPL0}}==
<langsyntaxhighlight lang=XPL0>int Tx, Ty, X, Y, R;
[SetVid($12); \640x480x4 graphics
Tx:= [320, 320-277, 320+277]; \equilateral triangle
Line 3,450:
until KeyHit;
SetVid($03); \restore normal text mode
]</langsyntaxhighlight>
 
=={{header|Yabasic}}==
<langsyntaxhighlight lang=Yabasic>width = 640 : height = 480
open window width, height
window origin "lb"
Line 3,474:
color 255 * (vertex = 0), 255 * (vertex = 1), 255 * (vertex = 2)
dot x, y
next</langsyntaxhighlight>
 
=={{header|Z80 Assembly}}==
Line 3,483:
space key.
 
<langsyntaxhighlight 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,652:
ld e,a ; -> C
exx
ret</langsyntaxhighlight>
 
 
Line 3,661:
{{trans|Java}}
[[File:ChaosGame.zkl.jpg|240px|thumb|right]]
<langsyntaxhighlight 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
Line 3,688:
 
done.wait(); // don't exit until thread is done
println("Done");</langsyntaxhighlight>
10,333

edits