Barnsley fern: Difference between revisions

Added uBasic/4tH version
(Added uBasic/4tH version)
 
(22 intermediate revisions by 13 users not shown)
Line 29:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<syntaxhighlight lang=Action"action!">INCLUDE "H6:REALMATH.ACT"
 
BYTE CH=$02FC,COLOR1=$02C5,COLOR2=$02C6
Line 125:
=={{header|Ada}}==
{{libheader|SDLAda}}
<syntaxhighlight lang=Ada"ada">with Ada.Numerics.Discrete_Random;
 
with SDL.Video.Windows.Makers;
Line 224:
 
This program generates a [https://en.wikipedia.org/wiki/Netpbm_format PBM file].
<syntaxhighlight lang="algol68">
BEGIN
INT iterations = 300000;
Line 283:
</syntaxhighlight>
 
=={{header|Applesoft BASIC}}==
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang=ApplesoftBasic> 100 LET YY(1) = .16
<syntaxhighlight lang="applesoftbasic"> 100 LET YY(1) = .16
110 XX(2) = .85:XY(2) = .04
120 YX(2) = - .04:YY(2) = .85
Line 310 ⟶ 311:
</syntaxhighlight>
 
==={{header|BASIC256}}===
<syntaxhighlight lang=BASIC256"basic256"># adjustable window altoght
# call the subroutine with the altoght you want
# it's possible to have a window that's large than your display
Line 351 ⟶ 352:
[https://www.dropbox.com/s/8rkgguj3ol57771/Barnsley_fern_BASIC256.png?dl=0 Barnsley fern BASIC256 image]
 
==={{header|BBC BASIC}}===
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> GCOL 2 : REM Green Graphics Color
X=0 : Y=0
FOR I%=1 TO 100000
Line 368:
NEXT
END</syntaxhighlight>
==={{header|uBasic/4tH}}===
uBasic/4tH does not feature graphics or floating point, so it requires some extra code to achieve this. This version uses binary scaling.
<syntaxhighlight lang="qbasic">Dim @o(5) ' 0 = SVG file, 1 = color, 2 = fillcolor, 3 = pixel, 4 = text
 
' === Begin Program ===
 
If Info("wordsize") < 64 Then Print "This program requires a 64-bit uBasic" : End
 
Proc _SVGopen ("svgfern.svg")
Proc _Canvas (500, 768) ' light gray background
Proc _Background (FUNC(_RGBtoColor (0, 0, 0)))
Proc _SetMode ("dot") ' we want dots, not pixels
 
For i = 1 To 25000
Let r = Rnd (100)
 
If r = 1 Then
Let x = 0
Let y = FUNC (_Fmul(FUNC(_Fdiv(16, 100)) , y))
Else
 
If r < 9 Then
Let x = FUNC(_Fmul(FUNC(_Fdiv(2, 10)), x)) - FUNC(_Fmul(FUNC(_Fdiv(26, 100)), y))
Let y = FUNC(_Fmul(FUNC(_Fdiv(-23, 100)), x)) + FUNC(_Fmul(FUNC(_Fdiv(22, 100)), y)) + FUNC(_Fdiv(16, 10))
Else
 
If r < 16 then
Let x = FUNC(_Fmul(FUNC(_Fdiv(-15, 100)), x)) + FUNC(_Fmul(FUNC(_Fdiv(28, 100)), y))
Let y = FUNC(_Fmul(FUNC(_Fdiv(26, 100)), x)) + FUNC(_Fmul(FUNC(_Fdiv(24, 100)), y)) + FUNC(_Fdiv(44, 100))
Else
 
Let x = FUNC(_Fmul(FUNC(_Fdiv(85, 100)), x)) + FUNC(_Fmul(FUNC(_Fdiv(4, 100)), y))
Let y = FUNC(_Fmul(FUNC(_Fdiv(-4, 100)), x)) + FUNC(_Fmul(FUNC(_Fdiv(85, 100)), y)) + FUNC(_Fdiv(16, 10))
EndIf
EndIf
EndIf
 
Let q = FUNC(_Fround(FUNC(_Fmul(x + FUNC(_Ntof(3)), FUNC(_Ntof(70))))))
Let p = FUNC(_Fround(FUNC(_Ntof(700)) - FUNC(_Fmul(y, FUNC(_Ntof(70))))))
 
Proc _SetColor (FUNC(_RGBtoColor (0, 128 + Rnd(128), 0)))
Proc _SetPixel (p+20, q)
Next
 
Proc _SVGclose
End
 
' === End Program ===
 
_Ntof Param (1) : Return (a@*16384)
_Ftoi Param (1) : Return ((10000*a@)/16384)
_Fmul Param (2) : Return ((a@*b@)/16384)
_Fdiv Param (2) : Return ((a@*16384)/b@)
_Fround Param (1) : Return ((a@+8192)/16384)
 
_RGBtoColor Param (3) : Return (a@ * 65536 + b@ * 256 + c@)
_SetColor Param (1) : @o(1) = a@ : Return
_GetColor Return (@o(1))
_SetFill Param (1) : @o(2) = a@ : Return
_GetFill Return (@o(2))
_SetPixel Param(2) : Proc @o(3)(a@, b@) : Return
_SVGclose Write @o(0), "</svg>" : Close @o(0) : Return
_color_ Param (1) : Proc _PrintRGB (a@) : Write @o(0), "\q />" : Return
 
_PrintRGB
Param (1)
Radix 16
 
If a@ < 0 Then
Write @o(0), "none";
Else
Write @o(0), Show(Str ("#!######", a@));
EndIf
 
Radix 10
Return
 
_Background
Param (1)
 
Write @o(0), "<rect width=\q100%\q height=\q100%\q fill=\q";
Proc _color_ (a@)
Return
 
_pixel_
Param (2)
 
Write @o(0), "<rect x=\q";b@;"\q y=\q";a@;
Write @o(0), "\q width=\q1px\q height=\q1px\q fill=\q";
Proc _color_ (@o(1))
Return
 
_dot_
Param (2)
 
Write @o(0), "<circle cx=\q";b@;"\q cy=\q";a@;
Write @o(0), "\q r=\q0.5px\q fill=\q";
Proc _color_ (@o(1))
Return
 
_SetMode
Param (1)
 
If Comp(a@, "pixel") = 0 Then
@o(3) = _pixel_
Else If Comp(a@, "dot") = 0 Then
@o(3) = _dot_
Else Print "Bad mode" : Raise 1
Endif : Endif
Return
 
_Canvas
Param (2)
 
Write @o(0), "<svg width=\q";a@;"\q height=\q";b@;"\q viewBox=\q0 0 ";a@;" ";b@;
Write @o(0), "\q xmlns=\qhttp://www.w3.org/2000/svg\q ";
Write @o(0), "xmlns:xlink=\qhttp://www.w3.org/1999/xlink\q>"
Return
 
_SVGopen
Param (1)
 
If Set (@o(0), Open (a@, "w")) < 0 Then
Print "Cannot open \q";Show (a@);"\q" : Raise 1
Else
Write @o(0), "<?xml version=\q1.0\q encoding=\qUTF-8\q standalone=\qno\q?>"
Write @o(0), "<!DOCTYPE svg PUBLIC \q-//W3C//DTD SVG 1.1//EN\q ";
Write @o(0), "\qhttp://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\q>"
EndIf
Return</syntaxhighlight>
This version uses decimal fixed-point numbers. It is not only faster, but also provides a better rendition of the Barnsley fern. It uses the very same SVG routines as the version above, so these are not included.
{{Trans|Forth}}
<syntaxhighlight lang="qbasic">Dim @o(5) ' 0 = SVG file, 1 = color, 2 = fillcolor, 3 = pixel, 4 = text
Dim @c(20) ' coefficients
 
w = 400 : h = 600 : s = 17
 
Proc _coefficients
Proc _SVGopen ("svgfern.svg")
Proc _Canvas (w, h) ' light gray background
Proc _Background (FUNC(_RGBtoColor (0, 0, 0)))
Proc _SetMode ("dot") ' we want dots, not pixels
 
For i = 0 to 50000
Proc _transformation (FUNC (_randomchoice))
 
Proc _SetColor (FUNC(_RGBtoColor (0, 128 + Rnd(128), 0)))
p = h - y/s
q = w/2 + x/s
 
Proc _SetPixel (p, q)
Next
 
Proc _SVGclose
End
 
_coefficients
Local (1)
 
Push 0 , 0 , 0 , 160 , 0 ' 1% of the time - f1
Push 200 , -260 , 230 , 220 , 1600 ' 7% of the time - f3
Push -150 , 280 , 260 , 240 , 440 ' 7% of the time - f4
Push 850 , 40 , -40 , 850 , 1600 ' 85% of the time - f2
 
For a@ = 19 To 0 Step -1 : @c(a@) = Pop() : Next
Return
 
_randomchoice
Local (1)
 
Push Rnd (100)
a@ = (Tos() > 0)
a@ = a@ + (Tos () > 7)
Return ((a@ + (Pop () > 14)) * 5)
 
_transformation
Param (1)
Local (2)
 
b@ = @c(a@) * x
b@ = (b@ + @c(a@+1) * y) / 1000
 
c@ = @c(a@+2) * x
c@ = (c@ + @c(a@+3) * y) / 1000
 
x = b@ : y = @c(a@+4) + c@
Return</syntaxhighlight>
 
=={{header|C}}==
This implementation requires the [http://www.cs.colorado.edu/~main/bgi/cs1300/ WinBGIm] library. Iteration starts from (0,0) as required by the task however before plotting the point is translated and scaled as negative co-ordinates are not supported by the graphics window, scaling is necessary as otherwise the fern is tiny even for large iterations ( > 1000000).
<syntaxhighlight lang=C"c">
#include<graphics.h>
#include<stdlib.h>
Line 438 ⟶ 625:
=={{header|C sharp|C#}}==
 
<syntaxhighlight lang="csharp">using System;
using System.Diagnostics;
using System.Drawing;
Line 486 ⟶ 673:
=={{header|C++}}==
[[File:BFCpp.png|200px|thumb|right]]
<syntaxhighlight lang="cpp">
#include <windows.h>
#include <ctime>
Line 623 ⟶ 810:
This version uses the QImage class from the Qt toolkit as an easy way to save an image in PNG format.
It also uses the C++ 11 random number library. Built and tested on macOS 10.15.4 with Qt 5.12.5.
<syntaxhighlight lang="cpp">#include <iostream>
#include <random>
#include <vector>
Line 683 ⟶ 870:
 
{{out}}
[[Media:Barnsley fern cpp.png]]
See: [https://slack-files.com/T0CNUL56D-F017EKXBC0Y-ca68fe222b barnsley_fern.png] (offsite PNG image)
 
=={{header|Common Lisp}}==
{{libheader|opticl}}
This code uses the <code>opticl</code> package for generating an image and saving it as a PNG file.
<syntaxhighlight lang="lisp">(defpackage #:barnsley-fern
(:use #:cl
#:opticl))
Line 735 ⟶ 922:
(multiple-value-setq (x y) (funcall (choose-transform) x y)))
(write-png-file filespec image)))</syntaxhighlight>
 
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">define x1 = 0, y1 = 0
 
bgcolor 0, 0, 0
cls graphics
 
for i = 1 to 10000
 
let r = rnd
 
if r > 0 and r < .01 then
 
let x = .0
let y = .16 * y
 
endif
 
if r > .01 and r < .08 then
 
let x = .22 * x - .26 * y
let y = -.23 * x + .22 * y + 1.6
 
endif
 
if r > .075 and r < .15 then
 
let x = .15 * x + .28 * y
let y = -.29 * x + .24 * y + .44
 
endif
 
let x = .85 * x + .04 * y
let y = -.04 * x + .85 * y + 1.6
 
let x1 = (x + 3) * 70
let y1 = 700 - y * 70
 
fgcolor 0, int(rnd * 255), 0
 
dot x1, y1
 
wait
 
next i</syntaxhighlight>
 
=={{header|D}}==
{{trans|Raku}}
{{libheader|dlib}}
<syntaxhighlight lang="d">#!/usr/bin/env dub
/+ dub.sdl:
dependency "dlib" version="~>0.21.0"
Line 800 ⟶ 1,032:
{{trans|Java}}
Hint: After putting a TPaintBox on the main form align it to alClient. Client width / height of the main form should be no less than 640 x 480.
<syntaxhighlight lang="delphi">unit Unit1;
 
interface
Line 867 ⟶ 1,099:
=={{header|EasyLang}}==
 
[https://easylang.onlinedev/apps/barnsley-fern.html Run it]
 
<syntaxhighlight lang="text">color 060
color 060
for i range 200000
for i = 1 to 200000
r = randomf
if r < 0.01
Line 887 ⟶ 1,120:
x = nx
y = ny
move 50 + x * 15 100 - y * 10
rect 0.3 0.3
.
.</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">; Barnsley fern
 
(defun make-array (size)
Line 949 ⟶ 1,183:
=={{header|F sharp|F#}}==
 
<syntaxhighlight lang="fsharp">
open System.Drawing
 
Line 980 ⟶ 1,214:
 
{{Out|Use}}
<syntaxhighlight lang="fsharp">
BarnsleyFern.run 720 720
</syntaxhighlight>
Line 992 ⟶ 1,226:
Traditionaly, Forth use Fixed-Point Arithmetic (here with a 1000 scale). For transformation function choice, a formula is used to pick coefficients in a matrix.
 
<syntaxhighlight lang="forth">
s" SDL2" add-lib
\c #include <SDL2/SDL.h>
Line 1,054 ⟶ 1,288:
Forth may use a dedicated Floating Point Stack. For transformation, a pointer to one of the 4 functions is used to be be called at the end of the loop.
 
<syntaxhighlight lang="forth">
s" SDL2" add-lib
\c #include <SDL2/SDL.h>
Line 1,126 ⟶ 1,360:
 
=={{header|Fortran}}==
<syntaxhighlight lang="fortran">
!Generates an output file "plot.dat" that contains the x and y coordinates
!for a scatter plot that can be visualized with say, GNUPlot
Line 1,179 ⟶ 1,413:
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' version 10-10-2016
' compile with: fbc -s console
 
Line 1,235 ⟶ 1,469:
 
=={{header|Frink}}==
<syntaxhighlight lang=Frink"frink">
g = new graphics
g.backgroundColor[0,0,0] // black
Line 1,276 ⟶ 1,510:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Barnsley_fern}}
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.
 
'''Solution'''
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.
 
[[File:Fōrmulæ - Barnsley fern 01.png]]
In '''[https://formulae.org/?example=Barnsley_fern this]''' page you can see the program(s) related to this task and their results.
 
'''Test case'''
 
[[File:Fōrmulæ - Barnsley fern 02.png]]
 
[[File:Fōrmulæ - Barnsley fern 03.png]]
 
=={{header|G'MIC}}==
<syntaxhighlight lang="c">
# Put this into a new file 'fern.gmic' and invoke it from the command line, like this:
# $ gmic fern.gmic -barnsley_fern
Line 1,313 ⟶ 1,553:
[[File:BarnsleyFernGnu.png|right|thumb|Output BarnsleyFernGnu.png]]
 
<syntaxhighlight lang="gnuplot">
## Barnsley fern fractal 2/17/17 aev
reset
Line 1,346 ⟶ 1,586:
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import (
Line 1,423 ⟶ 1,663:
{{libheader|JavaFX}}
 
<syntaxhighlight lang=Groovy"groovy">import javafx.animation.AnimationTimer
import javafx.application.Application
import javafx.scene.Group
Line 1,486 ⟶ 1,726:
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Data.List (scanl')
import Diagrams.Backend.Rasterific.CmdLine
import Diagrams.Prelude
Line 1,532 ⟶ 1,772:
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang=IS"is-BASICbasic">100 PROGRAM "Fern.bas"
110 RANDOMIZE
120 SET VIDEO MODE 1:SET VIDEO COLOR 0:SET VIDEO X 40:SET VIDEO Y 27
Line 1,557 ⟶ 1,797:
=={{header|J}}==
[[File:jfern.png|140px|thumb|right]]
<syntaxhighlight lang="j">require 'plot'
f=: |: 0 ". 1 2 }. ];._2 noun define
Line 1,580 ⟶ 1,820:
[[File:barnsley_fern.png|200px|thumb|right]]
{{works with|Java|8}}
<syntaxhighlight lang="java">import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
Line 1,653 ⟶ 1,893:
[[File:BarnsleyFernjs.png|right|thumb|Output BarnsleyFernjs.png]]
 
<syntaxhighlight lang="javascript">// Barnsley fern fractal
//6/17/16 aev
function pBarnsleyFern(canvasId, lim) {
Line 1,696 ⟶ 1,936:
}</syntaxhighlight>
'''Executing:'''
<syntaxhighlight lang="html">
<html>
<head><script src="BarnsleyFern.js"></script></head>
Line 1,711 ⟶ 1,951:
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Images
 
mutable struct BarnsleyFern
Line 1,720 ⟶ 1,960:
y::Float64
fern::Matrix{RGB}
function BarnsleyFern(width, height, color = RGB(0.0, 1.0, 0.0), bgcolor = RGB(0.0, 0.0, 0.0))
fimg = new(width,[bgcolor height,for RGB(0.0,x in 1.0:width, 0.0),y 0.0,in 0.0,1:height]
cx = [RGBInt(0floor(2.0,182 0.0,* 0.0)(width for x in- 1:width,) y/ 4.8378) in+ 1:height])
cxcy = Int(floor(29.1829983 * (f.widthheight - 1) / 49.83789983) + 1)
img[cx, cy] = color
cy = Int(floor(9.9983 * (f.height - 1) / 9.9983) + 1)
f.fern[cxreturn new(width, cy]height, =color, f0.color0, 0.0, img)
return f
end
end
Line 1,751 ⟶ 1,990:
=={{header|Kotlin}}==
{{trans|Java}}
<syntaxhighlight lang="scala">// version 1.1.0
 
import java.awt.*
Line 1,819 ⟶ 2,058:
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def fern
{lambda {:size :sign}
Line 1,844 ⟶ 2,083:
=={{header|Liberty BASIC}}==
 
<syntaxhighlight lang="lb">nomainwin
WindowWidth=800
WindowHeight=600
Line 1,878 ⟶ 2,117:
=={{header|Locomotive Basic}}==
{{trans|ZX Spectrum Basic}}
<syntaxhighlight lang="locobasic">10 mode 2:ink 0,0:ink 1,18:randomize time
20 scale=38
30 maxpoints=20000: x=0: y=0
Line 1,893 ⟶ 2,132:
=={{header|Lua}}==
Needs L&Ouml;VE 2D Engine
<syntaxhighlight lang=Lua"lua">
g = love.graphics
wid, hei = g.getWidth(), g.getHeight()
Line 1,930 ⟶ 2,169:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">
BarnsleyFern[{x_, y_}] := Module[{},
i = RandomInteger[{1, 100}];
Line 1,945 ⟶ 2,184:
{{trans|C#}}
{{works with|Mini Micro}}
<syntaxhighlight lang=MiniScript"miniscript">clear
x = 0
y = 0
Line 1,968 ⟶ 2,207:
 
=={{header|Nim}}==
<syntaxhighlight lang=Nim"nim">
import nimPNG, std/random
 
randomize()
Line 2,017 ⟶ 2,256:
 
for i in 1..iterations:
var r = randomrand(101)
var nx, ny: float
if r <= 85:
Line 2,043 ⟶ 2,282:
[[File:Barnsleyfern-oberon2.png|250px|thumb|right]]
 
<syntaxhighlight lang="oberon2">
MODULE BarnsleyFern;
(**
Line 2,104 ⟶ 2,343:
[[File:BarnsleyFern.png|right|thumb|Output BarnsleyFern.png]]
 
<syntaxhighlight lang="parigp">
\\ Barnsley fern fractal
\\ 6/17/16 aev
Line 2,137 ⟶ 2,376:
=={{header|Perl}}==
[[File:BarnsleyFernPerl.png|250px|thumb|right]]
<syntaxhighlight lang="perl">use Imager;
 
my $w = 640;
Line 2,165 ⟶ 2,404:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/BarnsleyFern.htm here], or see the output [https://imgur.com/a/04ZZZt9 on imgur]
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- pwa\phix\BarnsleyFern.exw
Line 2,214 ⟶ 2,453:
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">`(== 64 64)
(seed (in "/dev/urandom" (rd 8)))
(scl 20)
Line 2,250 ⟶ 2,489:
 
=={{header|Processing}}==
<syntaxhighlight lang="java">void setup() {
size(640, 640);
background(0, 0, 0);
Line 2,293 ⟶ 2,532:
==={{header|Processing Python mode}}===
 
<syntaxhighlight lang="python">size(640, 640)
background(0)
 
Line 2,354 ⟶ 2,593:
 
set(m, n, "#00ff00")</syntaxhighlight>
 
=={{header|Prolog}}==
<syntaxhighlight lang="prolog>
% a straight forward adaption from the Ada example
% these imports are needed for Ciao Prolog but needed
% modules will vary with your Prolog system
:- use_module(library(streams)).
:- use_module(library(stream_utils)).
:- use_module(library(lists)).
:- use_module(library(llists)).
:- use_module(library(hiordlib)).
:- use_module(library(random)).
:- use_module(library(format)).
 
replicate(Term, Times, L) :-
length(L, Times),
maplist(=(Term), L).
 
replace(0, [_|T], E, [E|T]).
replace(X, [H|T0], E, [H|T]) :-
X0 is X -1,
replace(X0, T0, E, T).
replace_2d(X, 0, [H|T], E, [R|T]) :-
replace(X, H, E, R).
replace_2d(X, Y, [H|T0], E, [H|T]) :-
Y0 is Y -1,
replace_2d(X, Y0, T0, E, T).
 
fern_iteration(10000, _X, _Y, Final, Final).
fern_iteration(N, X, Y, I, Final) :-
random(R),
( R =< 0.01
-> ( X1 is 0.0,
Y1 is 0.16*Y )
; ( R =< 0.86
-> ( X1 is 0.85*X + 0.04*Y,
Y1 is -0.04*X + 0.85*Y + 1.6 )
; ( R =< 0.93
-> ( X1 is 0.20*X - 0.26*Y,
Y1 is 0.23*X + 0.22*Y + 1.60 )
; ( X1 is -0.15*X + 0.28*Y,
Y1 is 0.26*X + 0.24*Y + 0.44 )
) ) ),
PointX is 250 + floor(70.0*X1),
PointY is 750 - floor(70.0*Y1),
replace_2d(PointX, PointY, I, [0, 255, 0], I1), !,
N1 is N + 1,
fern_iteration(N1, X1, Y1, I1, Final).
 
draw_fern :-
replicate([0, 0, 0], 500, Row),
replicate(Row, 750, F),
fern_iteration(0, 0, 0, F, Fern),
% the following lines are written for ciao prolog and
% write to a ppm6 file for viewing
% adapting to SWI or Scryer should be straighforward
open('fern.ppm', write, File),
flatten(Fern, FP),
format(File, "P6\n~d ~d\n255\n", [500, 750]),
write_bytes(File, FP),
close(File).
</syntaxhighlight>
 
=={{header|PureBasic}}==
<syntaxhighlight lang=PureBasic"purebasic">EnableExplicit
DisableDebugger
 
Line 2,400 ⟶ 2,701:
 
=={{header|Python}}==
<syntaxhighlight lang=Python"python">
 
import random
Line 2,447 ⟶ 2,748:
 
=={{header|QB64}}==
<syntaxhighlight lang="qb64">_Title "Barnsley Fern"
Dim As Integer sw, sh
sw = 400: sh = 600
Line 2,481 ⟶ 2,782:
System</syntaxhighlight>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ $ "turtleduck.qky" loadfile ] now!
 
[ ' [ 79 121 66 ] fill
[ 3 2 circle ] ] is dot ( --> )
 
[ 1 fly
-1 4 turn
1 fly
1 4 turn ] is toxy ( n n --> )
 
[ 100 1 v* /
dip [ 100 1 v* / ]
2dup toxy
dot
1 2 turn
toxy
1 2 turn ] is plot ( n n --> )
 
 
[ 2swap 2drop 0 1
2swap 16 100 v* ] is f1 ( n/d n/d --> n/d n/d )
 
[ 2over -4 100 v*
2over 85 100 v*
16 10 v+ v+
join dip
[ 4 100 v*
2swap 85 100 v*
v+ ]
do ] is f2 ( n/d n/d --> n/d n/d )
 
[ 2over 23 100 v*
2over 22 100 v*
16 10 v+ v+
join dip
[ -26 100 v*
2swap 20 100 v*
v+ ]
do ] is f3 ( n/d n/d --> n/d n/d )
 
[ 2over 26 100 v*
2over 24 100 v*
44 100 v+ v+
join dip
[ 28 100 v*
2swap -15 100 v*
v+ ]
do ] is f4 ( n/d n/d --> n/d n/d )
 
[ 100 random
[ dup 0 = iff
[ drop f1 ] done
dup 86 < iff
[ drop f2 ] done
93 < iff f3 done
f4 ]
2swap 1000000000 round
2swap 1000000000 round
2over 2over plot ] is nextpoint ( n/d n/d --> n/d n/d )
 
turtle
' [ 79 121 66 ] colour
-500 1 fly
0 1 0 1
0 frames
20000 times nextpoint
1 frames
4 times drop
</syntaxhighlight>
 
{{out}}
 
[[File:Quackery Barnsley fern.png|thumb|center]]
 
=={{header|R}}==
Line 2,486 ⟶ 2,862:
{{trans|PARI/GP}}
[[File:BarnsleyFernR.png|right|thumb|Output BarnsleyFernR.png]]
<syntaxhighlight lang="rsplus">## pBarnsleyFern(fn, n, clr, ttl, psz=600): Plot Barnsley fern fractal.
## Where: fn - file name; n - number of dots; clr - color; ttl - plot title;
## psz - picture size.
Line 2,532 ⟶ 2,908:
==='Obvious' solution===
The matrix solution above is a clever approach, but the following solution is more readable if you're unfamiliar with linear algebra. This is very much a blind "just do what the task says" solution. It's so simple that it probably runs unadapted in S. I suspect that there is room for an interesting use of R's ifelse function somewhere, but I couldn't find a clean way.
<syntaxhighlight lang="rsplus">fernOfNPoints <- function(n)
{
currentX <- currentY <- newX <- newY <- 0
Line 2,575 ⟶ 2,951:
=={{header|Racket}}==
[[File:racket-barnsley-fern.png]] : file uploading broken :-(
<syntaxhighlight lang="racket">#lang racket
 
(require racket/draw)
Line 2,614 ⟶ 2,990:
{{trans|Perl}}
[[File:Barnsley-fern-perl6.png|250px|thumb|right]]
<syntaxhighlight lang="raku" linesline>use Image::PNG::Portable;
 
my ($w, $h) = (640, 640);
Line 2,640 ⟶ 3,016:
<br>contains the &nbsp; '''X''' &nbsp; and &nbsp; '''Y''' &nbsp; coördinates for a scatter plot that can be
visualized with a plotting program.
<syntaxhighlight lang="rexx">/*REXX pgm gens X & Y coördinates for a scatter plot to be used to show a Barnsley fern.*/
parse arg N FID seed . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N= 100000 /*Not specified? Then use the default*/
Line 2,667 ⟶ 3,043:
 
=={{header|Ring}}==
<syntaxhighlight lang=Ring"ring">
 
Load "guilib.ring"
Line 2,788 ⟶ 3,164:
{{libheader|RubyGems}}
{{libheader|JRubyArt}}
<syntaxhighlight lang="ruby">
MAX_ITERATIONS = 200_000
 
Line 2,834 ⟶ 3,210:
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">'Barnsley Fern - Run BASIC
'http://rosettacode.org/wiki/Barnsley_fern#Run_BASIC
'copy code and run it at http://www.runbasic.com
Line 2,870 ⟶ 3,246:
{{libheader|rand}}
 
<syntaxhighlight lang="rust">extern crate rand;
extern crate raster;
 
Line 2,918 ⟶ 3,294:
=={{header|Scala}}==
===Java Swing Interoperability===
<syntaxhighlight lang=Scala"scala">import java.awt._
import java.awt.image.BufferedImage
 
Line 2,984 ⟶ 3,360:
=={{header|Scheme}}==
This version creates a list of points, defining the fern, which are then rescaled and output to an eps file.
<syntaxhighlight lang="scheme">(import (scheme base)
(scheme cxr)
(scheme file)
Line 3,048 ⟶ 3,424:
{{Works with|Scilab|5.4.0 and above}}
This version creates a list of points, defining the fern, and shows them on a graphic window which can then be saved to a file via the GUI or the console by the user.
<syntaxhighlight lang="text">
iteractions=1.0d6;
 
Line 3,090 ⟶ 3,466:
=={{header|SequenceL}}==
'''Tail-Recursive SequenceL Code:'''<br>
<syntaxhighlight lang="sequencel">import <Utilities/Math.sl>;
import <Utilities/Random.sl>;
 
Line 3,125 ⟶ 3,501:
'''C++ Driver Code:'''<br>
{{libheader|CImg}}
<syntaxhighlight lang="c">#include "SL_Generated.h"
#include "CImg.h"
 
Line 3,164 ⟶ 3,540:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">require('Imager')
 
var w = 640
Line 3,190 ⟶ 3,566:
 
=={{header|SPL}}==
<syntaxhighlight lang="spl">w,h = #.scrsize()
x,y = 0
>
Line 3,207 ⟶ 3,583:
=={{header|Standard ML}}==
Works with PolyML. Random generator copy from the [[Random_numbers#Standard_ML]] task. Window slimmed down from [[Animation#Standard_ML]].
<syntaxhighlight lang=Standard"standard MLml">open XWindows ;
open Motif ;
 
Line 3,273 ⟶ 3,649:
Output is viewable in a playground.
 
<syntaxhighlight lang="swift">import UIKit
import CoreImage
import PlaygroundSupport
Line 3,319 ⟶ 3,695:
 
=={{header|TI-83 BASIC}}==
<syntaxhighlight lang="ti83b">ClrDraw
Input "ITERS:",M
[[0,0,1]]→[A]
Line 3,360 ⟶ 3,736:
=={{header|Unicon}}==
{{libheader|graphics}}
<syntaxhighlight lang="unicon">
link graphics
 
Line 3,430 ⟶ 3,806:
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">Private Sub plot_coordinate_pairs(x As Variant, y As Variant)
Dim chrt As Chart
Set chrt = ActiveSheet.Shapes.AddChart.Chart
Line 3,468 ⟶ 3,844:
=={{header|Visual Basic .NET}}==
{{works with|Visual Basic .NET|2011}}
<syntaxhighlight lang="vbnet">' Barnsley Fern - 11/11/2019
Public Class BarnsleyFern
 
Line 3,506 ⟶ 3,882:
{{trans|Kotlin}}
{{libheader|DOME}}
<syntaxhighlight lang=ecmascript"wren">import "graphics" for Canvas, Color
import "dome" for Window
import "random" for Random
Line 3,559 ⟶ 3,935:
 
var Game = BarnsleyFern.new(640, 640, 200000)</syntaxhighlight>
 
{{out}}
[[File:Wren-Barnsley_fern.png|400px]]
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"xpl0">int N, R;
real NX, NY, X, Y;
[SetVid($12); \set 640x480x4 VGA graphics (on PC or RPi)
Line 3,580 ⟶ 3,959:
{{trans|ZX Spectrum Basic}}
Classic style
<syntaxhighlight lang=Yabasic"yabasic">10 REM Fractal Fern
20 LET wid = 800 : LET hei = 600 : open window wid, hei : window origin "cb"
25 backcolor 0, 0, 0 : color 0, 255, 0 : clear window
Line 3,593 ⟶ 3,972:
110 DOT x*wid/12,y*hei/12
120 NEXT n</syntaxhighlight>
Modern style <syntaxhighlight lang=Yabasic"yabasic">REM Fractal Fern
wid = 800 : hei = 600 : open window wid, hei : window origin "cb"
backcolor 0, 0, 0 : color 0, 255, 0 : clear window
Line 3,612 ⟶ 3,991:
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
{{trans|Java}}
<syntaxhighlight lang="zkl">fcn barnsleyFern(){
w,h:=640,640;
bitmap:=PPM(w+1,h+1,0xFF|FF|FF); // White background
Line 3,631 ⟶ 4,010:
=={{header|ZX Spectrum Basic}}==
{{trans|zkl}}
<syntaxhighlight lang="zxbasic">10 REM Fractal Fern
20 PAPER 7: BORDER 7: BRIGHT 1: INK 4: CLS
30 LET maxpoints=20000: LET x=0: LET y=0
1

edit