Barnsley fern: Difference between revisions
Added uBasic/4tH version
(add F# version) |
(Added uBasic/4tH version) |
||
(40 intermediate revisions by 17 users not shown) | |||
Line 29:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<
BYTE CH=$02FC,COLOR1=$02C5,COLOR2=$02C6
Line 119:
ValR("30",scale)
Fern(scale)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Barnsley_fern.png Screenshot from Atari 8-bit computer]
Line 125:
=={{header|Ada}}==
{{libheader|SDLAda}}
<
with SDL.Video.Windows.Makers;
Line 218:
Window.Finalize;
SDL.Finalise;
end Barnsley_Fern;</
=={{header|ALGOL 68}}==
Line 224:
This program generates a [https://en.wikipedia.org/wiki/Netpbm_format PBM file].
<
BEGIN
INT iterations = 300000;
Line 281:
leave: SKIP
END
</syntaxhighlight>
=={{header|
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="applesoftbasic"> 100 LET YY(1) = .16
110 XX(2) = .85:XY(2) = .04
120 YX(2) = - .04:YY(2) = .85
Line 308 ⟶ 309:
330 HPLOT X% * 2 + 1,Y%
340 NEXT
</syntaxhighlight>
==={{header|
<syntaxhighlight lang="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
call barnsley(800)
end
subroutine barnsley(alto)
graphsize alto / 2, alto
color rgb(0, 255, 0)
f = alto / 10.6
c = alto / 4 - alto / 40
x = 0 : y = 0
for n = 1 to alto * 50
p = rand * 100
begin case
case p <= 1
nx = 0
ny = 0.16 * y
case p <= 8
nx = 0.2 * x - 0.26 * y
ny = 0.23 * x + 0.22 * y + 1.6
case p <= 15
nx = -0.15 * x + 0.28 * y
ny = 0.26 * x + 0.24 * y + 0.44
else
nx = 0.85 * x + 0.04 * y
ny = -0.04 * x + 0.85 * y + 1.6
end case
x = nx : y = ny
plot(c + x * f, alto - y * f)
next n
# remove comment (#) in next line to save window as .png file
# imgsave("Barnsley_fern.png")
end subroutine</syntaxhighlight>
{{out}}
[https://www.dropbox.com/s/8rkgguj3ol57771/Barnsley_fern_BASIC256.png?dl=0 Barnsley fern BASIC256 image]
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<
X=0 : Y=0
FOR I%=1 TO 100000
Line 325 ⟶ 367:
PLOT 1000 + X * 130 , Y * 130
NEXT
END</
==={{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">
#include<graphics.h>
#include<stdlib.h>
Line 392 ⟶ 621:
return 0;
}
</syntaxhighlight>
=={{header|C sharp|C#}}==
<
using System.Diagnostics;
using System.Drawing;
Line 440 ⟶ 669:
}
}
}</
=={{header|C++}}==
[[File:BFCpp.png|200px|thumb|right]]
<
#include <windows.h>
#include <ctime>
Line 575 ⟶ 804:
fern f; f.draw(); return 0;
}
</syntaxhighlight>
===Cross-Platform Alternative===
Line 581 ⟶ 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.
<
#include <random>
#include <vector>
Line 638 ⟶ 867:
}
return EXIT_SUCCESS;
}</
{{out}}
[[Media:Barnsley fern cpp.png]]
=={{header|Common Lisp}}==
{{libheader|opticl}}
<
(:use #:cl
#:opticl))
Line 692 ⟶ 921:
(set-pixel image x y)
(multiple-value-setq (x y) (funcall (choose-transform) x y)))
(write-png-file filespec image)))</
=={{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}}
<
/+ dub.sdl:
dependency "dlib" version="~>0.21.0"
Line 753 ⟶ 1,027:
}
img.saveImage(`barnsley_dlib.png`);
}</
=={{header|Delphi}}==
{{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.
<
interface
Line 821 ⟶ 1,095:
end;
end.</
=={{header|EasyLang}}==
[https://easylang.
<syntaxhighlight lang="text">
color 060
for i = 1 to 200000
r = randomf
if r < 0.01
Line 845 ⟶ 1,120:
x = nx
y = ny
.
</syntaxhighlight>
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">; Barnsley fern
(defun make-array (size)
"Create an empty array with size*size elements."
(setq m-array (make-vector size nil))
(dotimes (i size)
(setf (aref m-array i) (make-vector size 0)))
m-array)
(defun barnsley-next (p)
"Return the next Barnsley fern coordinates."
(let ((r (random 100))
(x (car p))
(y (cdr p)))
(cond ((< r 2) (setq nx 0) (setq ny (* 0.16 y)))
((< r 9) (setq nx (- (* 0.2 x) (* 0.26 y)))
(setq ny (+ 1.6 (* 0.23 x) (* 0.22 y))))
((< r 16) (setq nx (+ (* -0.15 x) (* 0.28 y)))
(setq ny (+ 0.44 (* 0.26 x) (* 0.24 y))))
(t (setq nx (+ (* 0.85 x) (* 0.04 y)))
(setq ny (+ 1.6 (* -0.04 x) (* 0.85 y)))))
(cons nx ny)))
(defun barnsley-lines (arr size)
"Turn array into a string for XPM conversion."
(setq all "")
(dotimes (y size)
(setq line "")
(dotimes (x size)
(setq line (concat line (if (= (elt (elt arr y) x) 1) "*" "."))))
(setq all (concat all "\"" line "\",\n")))
all)
(defun barnsley-show (arr size)
"Convert size*size array to XPM image and show it."
(insert-image (create-image (concat (format "/* XPM */
static char * barnsley[] = {
\"%i %i 2 1\",
\". c #000000\",
\"* c #00ff00\"," size size)
(barnsley-lines arr size) "};") 'xpm t)))
(defun barnsley (size scale max-iter)
"Plot the Barnsley fern."
(let ((arr (make-array size))
(p (cons 0 0)))
(dotimes (it max-iter)
(setq p (barnsley-next p))
(setq x (round (+ (/ size 2) (* scale (car p)))))
(setq y (round (- size (* scale (cdr p)) 1)))
(setf (elt (elt arr y) x) 1))
(barnsley-show arr size)))
(barnsley 400 35 100000)</syntaxhighlight>
=={{header|F sharp|F#}}==
<
open System.Drawing
Line 879 ⟶ 1,211:
|> Seq.fold (fun (b:Bitmap) (x,y) -> b.SetPixel(x-1,y-1,Color.ForestGreen); b) emptyBitmap // add pixels to bitmap
bitmap.Save("BFFsharp.png")
</syntaxhighlight>
{{Out|Use}}
<
BarnsleyFern.run 720 720
</syntaxhighlight>
=={{header|Forth}}==
Line 894 ⟶ 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.
<
s" SDL2" add-lib
\c #include <SDL2/SDL.h>
Line 951 ⟶ 1,283:
;
fern</
===Floating Point and Multiple Functions solution===
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.
<
s" SDL2" add-lib
\c #include <SDL2/SDL.h>
Line 1,023 ⟶ 1,355:
;
fern</
=={{header|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,078 ⟶ 1,410:
close(1)
end program BarnsleyFern
</syntaxhighlight>
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 1,134 ⟶ 1,466:
Windowtitle "hit any key to end program"
Sleep
End</
=={{header|Frink}}==
<syntaxhighlight lang="frink">
g = new graphics
g.backgroundColor[0,0,0] // black
Line 1,174 ⟶ 1,506:
g.show[]
</syntaxhighlight>
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Barnsley_fern}}
'''Solution'''
[[File:Fōrmulæ - Barnsley fern 01.png]]
'''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,208 ⟶ 1,546:
)"}
-r 40%,40%,1,1,2
</syntaxhighlight>
=={{header|gnuplot}}==
Line 1,215 ⟶ 1,553:
[[File:BarnsleyFernGnu.png|right|thumb|Output BarnsleyFernGnu.png]]
<
## Barnsley fern fractal 2/17/17 aev
reset
Line 1,240 ⟶ 1,578:
set output
unset print
</
{{Output}}
<pre>
Line 1,248 ⟶ 1,586:
=={{header|Go}}==
<
import (
Line 1,319 ⟶ 1,657:
log.Fatal(err)
}
}</
=={{header|Groovy}}==
Line 1,325 ⟶ 1,663:
{{libheader|JavaFX}}
<
import javafx.application.Application
import javafx.scene.Group
Line 1,385 ⟶ 1,723:
}
}
</syntaxhighlight>
=={{header|Haskell}}==
<
import Diagrams.Backend.Rasterific.CmdLine
import Diagrams.Prelude
Line 1,431 ⟶ 1,769:
main = do
rand <- getStdGen
mainWith $ drawFern (randomRs (0, 1) rand)</
=={{header|IS-BASIC}}==
<
110 RANDOMIZE
120 SET VIDEO MODE 1:SET VIDEO COLOR 0:SET VIDEO X 40:SET VIDEO Y 27
Line 1,455 ⟶ 1,793:
290 LET X=NX:LET Y=NY
300 PLOT X*96+600,Y*96
310 NEXT</
=={{header|J}}==
[[File:jfern.png|
<
f=: |: 0 ". 1 2 }. ];._2 noun define
w
f1 0
f2 0.
f4 -0.15 0.26 0.28 0.24 0 0.44 0.07
)
Line 1,474 ⟶ 1,813:
ifs=: (fa@] + fm@] +/ .* [) prob
getPoints=: ifs^:(<200000)
plotFern=: 'dot;
plotFern getPoints 0 0</
=={{header|Java}}==
[[File:barnsley_fern.png|200px|thumb|right]]
{{works with|Java|8}}
<
import java.awt.image.BufferedImage;
import javax.swing.*;
Line 1,548 ⟶ 1,887:
});
}
}</
=={{header|JavaScript}}==
Line 1,554 ⟶ 1,893:
[[File:BarnsleyFernjs.png|right|thumb|Output BarnsleyFernjs.png]]
<
//6/17/16 aev
function pBarnsleyFern(canvasId, lim) {
Line 1,595 ⟶ 1,934:
ctx.fillRect(x * 50 + 260, -y * 50 + 540, 1, 1);
} //fend i
}</
'''Executing:'''
<
<html>
<head><script src="BarnsleyFern.js"></script></head>
Line 1,605 ⟶ 1,944:
</body>
</html>
</
{{Output}}
<pre>
Line 1,612 ⟶ 1,951:
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Images
mutable struct BarnsleyFern
height::Int
color::RGB
x::Float64
y::Float64
function BarnsleyFern(width, height, color = RGB(0.0, 1.0, 0.0), bgcolor = RGB(0.0, 0.0, 0.0))
img = [bgcolor for
end
end
function transform(f::BarnsleyFern)
r = rand(0:99)
f.x, f.y = r < 1 ? (0.0, 0.16 * f.y) :
1 <= r < 86 ? (0.85 * f.x + 0.04 * f.y, -0.04 * f.x + 0.85 * f.y + 1.6) :
86 <= r < 93 ? (0.2 * f.x - 0.26 * f.y, 0.23 * f.x + 0.22 * f.y + 1.6) :
(-0.15 * f.x + 0.28 * f.y, 0.26 * f.x + 0.24 * f.y + 0.44)
cx = Int(floor((f.x + 2.182) * (f.width - 1) / 4.8378) + 1)
cy = Int(floor((9.9983 - f.y) * (f.height - 1) / 9.9983) + 1)
f.fern[cx, cy] = f.color
end
const fern = BarnsleyFern(500, 500)
for _ in 1:1000000
transform(fern)
end
fern.fern'
</syntaxhighlight>
[[File:Jbarnsleyfern.png]]
=={{header|Kotlin}}==
{{trans|Java}}
<
import java.awt.*
Line 1,700 ⟶ 2,055:
f.setVisible(true)
}
}</
=={{header|Lambdatalk}}==
<
{def fern
{lambda {:size :sign}
Line 1,723 ⟶ 2,078:
{def F {fern 25 1}}
</syntaxhighlight>
The output can be seen in http://lambdaway.free.fr/lambdawalks/?view=fern
=={{header|Liberty BASIC}}==
<
WindowWidth=800
WindowHeight=600
Line 1,758 ⟶ 2,113:
[q]
close #1
</syntaxhighlight>
=={{header|Locomotive Basic}}==
{{trans|ZX Spectrum Basic}}
<
20 scale=38
30 maxpoints=20000: x=0: y=0
Line 1,773 ⟶ 2,128:
100 x=nx: y=ny
110 plot scale*x+320,scale*y
120 next</
=={{header|Lua}}==
Needs LÖVE 2D Engine
<syntaxhighlight lang="lua">
g = love.graphics
wid, hei = g.getWidth(), g.getHeight()
Line 1,811 ⟶ 2,166:
g.draw( canvas )
end
</syntaxhighlight>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">
BarnsleyFern[{x_, y_}] := Module[{},
i = RandomInteger[{1, 100}];
Line 1,824 ⟶ 2,179:
points = NestList[BarnsleyFern, {0,0}, 100000];
Show[Graphics[{Hue[.35, 1, .7], PointSize[.001], Point[#] & /@ points}]]
</syntaxhighlight>
=={{header|MiniScript}}==
{{trans|C#}}
{{works with|Mini Micro}}
<syntaxhighlight lang="miniscript">clear
x = 0
y = 0
Line 1,849 ⟶ 2,204:
y = 0.26 * xp + 0.24 * y + 0.44
end if
end for</
=={{header|Nim}}==
<syntaxhighlight lang="nim">
import nimPNG, std/random
randomize()
Line 1,901 ⟶ 2,256:
for i in 1..iterations:
var r =
var nx, ny: float
if r <= 85:
Line 1,922 ⟶ 2,277:
discard savePNG24("fern.png",img.toString, width, height)
</syntaxhighlight>
=={{header|Oberon-2}}==
[[File:Barnsleyfern-oberon2.png|250px|thumb|right]]
<
MODULE BarnsleyFern;
(**
Line 1,981 ⟶ 2,336:
Init;Draw
END BarnsleyFern.
</syntaxhighlight>
=={{header|PARI/GP}}==
Line 1,988 ⟶ 2,343:
[[File:BarnsleyFern.png|right|thumb|Output BarnsleyFern.png]]
<
\\ Barnsley fern fractal
\\ 6/17/16 aev
Line 2,010 ⟶ 2,365:
pBarnsleyFern(530,100000); \\ BarnsleyFern.png
}
</
{{Output}}
Line 2,021 ⟶ 2,376:
=={{header|Perl}}==
[[File:BarnsleyFernPerl.png|250px|thumb|right]]
<
my $w = 640;
Line 2,043 ⟶ 2,398:
$img->flip(dir => 'v');
$img->write(file => 'barnsleyFern.png');</
=={{header|Phix}}==
Line 2,049 ⟶ 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]
<!--<
<span style="color: #000080;font-style:italic;">--
-- pwa\phix\BarnsleyFern.exw
Line 2,095 ⟶ 2,450:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</
=={{header|PicoLisp}}==
<
(seed (in "/dev/urandom" (rd 8)))
(scl 20)
Line 2,131 ⟶ 2,486:
(prinl "P1")
(prinl 640 " " 640)
(mapc prinl G) ) )</
=={{header|Processing}}==
<
size(640, 640);
background(0, 0, 0);
Line 2,173 ⟶ 2,528:
}
noLoop();
}</
==={{header|Processing Python mode}}===
<
background(0)
Line 2,237 ⟶ 2,592:
n = height - round(60 * y)
set(m, n, "#00ff00")</
=={{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}}==
<
DisableDebugger
Line 2,281 ⟶ 2,698:
Repeat : Until WaitWindowEvent(50)=#PB_Event_CloseWindow
EndIf
End</
=={{header|Python}}==
<syntaxhighlight lang="python">
import random
Line 2,327 ⟶ 2,744:
fern.iterate(1000000)
fern.fern.show()
</syntaxhighlight>
[[File:Pyfern.png]]
=={{header|QB64}}==
<
Dim As Integer sw, sh
sw = 400: sh = 600
Line 2,363 ⟶ 2,780:
Sleep
System</
=={{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,370 ⟶ 2,862:
{{trans|PARI/GP}}
[[File:BarnsleyFernR.png|right|thumb|Output BarnsleyFernR.png]]
<
## Where: fn - file name; n - number of dots; clr - color; ttl - plot title;
## psz - picture size.
Line 2,404 ⟶ 2,896:
## Executing:
pBarnsleyFern("BarnsleyFernR", 100000, "dark green", "Barnsley Fern Fractal", psz=600)
</
{{Output}}
Line 2,416 ⟶ 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.
<
{
currentX <- currentY <- newX <- newY <- 0
plot(0, 0, xlim = c(-2, 3), ylim = c(0, 10), xlab = "", ylab = "", pch = 20, col = "darkgreen", cex = 0.1)
f1 <- function()#ran 1% of the time
{
newX <<- 0
newY <<- 0.16 * currentY
}
f2 <- function()#ran 85% of the time
{
newX <<- 0.85 * newX + 0.04 * newY
newY <<- -0.
}
{
newX <<- 0.
newY <<-
}
{
newX <<- -0.
newY <<- 0.
}
for(i in 2:n)#We've already plotted (0,0), so we can skip one run.
{
case <- runif(1)
if(case <= 0.01) f1()
else if(case <= 0.86) f2()
else if(case <= 0.93) f3()
else f4()
points(newX, newY, pch = 20, col = "darkgreen", cex = 0.1)
}
return(invisible())
}
Line 2,455 ⟶ 2,947:
#It will look better if you use a bigger input, but the plot might take a while.
#I find that there's a large delay between RStudio saying that my code is finished running and the plot appearing.
#If your input is truly big, you may want to reduce the two cex parameters (to make the points smaller).</
=={{header|Racket}}==
[[File:racket-barnsley-fern.png]] : file uploading broken :-(
<
(require racket/draw)
Line 2,491 ⟶ 2,983:
bmp
(send bmp save-file "images/racket-barnsley-fern.png" 'png)</
=={{header|Raku}}==
Line 2,498 ⟶ 2,990:
{{trans|Perl}}
[[File:Barnsley-fern-perl6.png|250px|thumb|right]]
<syntaxhighlight lang="raku"
my ($w, $h) = (640, 640);
Line 2,517 ⟶ 3,009:
}
$png.write: 'Barnsley-fern-perl6.png';</
=={{header|REXX}}==
Line 2,524 ⟶ 3,016:
<br>contains the '''X''' and '''Y''' coördinates for a scatter plot that can be
visualized with a plotting program.
<
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,547 ⟶ 3,039:
call lineout FID, x","y
end /*#*/ /* [↓] close the file (safe practice).*/
call lineout FID /*stick a fork in it, we're all done. */</
{{out|output|text= is generated to an output file: BARNSLEY.DAT which contains the '''X''' and '''Y''' coördinates of a scatter plot.}}<br><br>
=={{header|Ring}}==
<syntaxhighlight lang="ring">
Load "guilib.ring"
Line 2,667 ⟶ 3,159:
return
</syntaxhighlight>
=={{header|Ruby}}==
{{libheader|RubyGems}}
{{libheader|JRubyArt}}
<
MAX_ITERATIONS = 200_000
Line 2,715 ⟶ 3,207:
size 500, 500
end
</syntaxhighlight>
=={{header|Run BASIC}}==
<
'http://rosettacode.org/wiki/Barnsley_fern#Run_BASIC
'copy code and run it at http://www.runbasic.com
Line 2,748 ⟶ 3,240:
NEXT n
render #g
#g "flush"</
=={{header|Rust}}==
Line 2,754 ⟶ 3,246:
{{libheader|rand}}
<
extern crate raster;
Line 2,798 ⟶ 3,290:
raster::save(&image, "fractal.png").unwrap();
}</
=={{header|Scala}}==
===Java Swing Interoperability===
<
import java.awt.image.BufferedImage
Line 2,864 ⟶ 3,356:
})
}</
=={{header|Scheme}}==
This version creates a list of points, defining the fern, which are then rescaled and output to an eps file.
<
(scheme cxr)
(scheme file)
Line 2,927 ⟶ 3,419:
(display "\n%%EOF")))))
(output-fern-as-eps "barnsley.eps" (create-fern 0 0 50000))</
=={{header|Scilab}}==
{{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 2,970 ⟶ 3,462:
axes.isoview="on";
axes.children.children.mark_foreground=13;
</syntaxhighlight>
=={{header|SequenceL}}==
'''Tail-Recursive SequenceL Code:'''<br>
<
import <Utilities/Random.sl>;
Line 3,005 ⟶ 3,497:
fern := barnsleyFern(seedRandom(seed), count, [[0.0,0.0]]);
in
scale(fern, width, height);</
'''C++ Driver Code:'''<br>
{{libheader|CImg}}
<
#include "CImg.h"
Line 3,042 ⟶ 3,534:
return 0;
}</
{{out}}
Line 3,048 ⟶ 3,540:
=={{header|Sidef}}==
<
var w = 640
Line 3,070 ⟶ 3,562:
img.flip(dir => 'v')
img.write(file => 'barnsleyFern.png')</
Output image: [https://github.com/trizen/rc/blob/master/img/barnsley-fern-sidef.png Barnsley fern]
=={{header|SPL}}==
<
x,y = 0
>
Line 3,087 ⟶ 3,579:
f2(x,y) <= 0.85*x+0.04*y, -0.04*x+0.85*y+1.6
f3(x,y) <= 0.2*x-0.26*y, 0.23*x+0.22*y+1.6
f4(x,y) <= -0.15*x+0.28*y, 0.26*x+0.24*y+0.44</
=={{header|Standard ML}}==
Works with PolyML. Random generator copy from the [[Random_numbers#Standard_ML]] task. Window slimmed down from [[Animation#Standard_ML]].
<
open Motif ;
Line 3,150 ⟶ 3,642:
XtRealizeWidget shell
)
end ; </
call
demoWindow () ;
Line 3,157 ⟶ 3,649:
Output is viewable in a playground.
<
import CoreImage
import PlaygroundSupport
Line 3,200 ⟶ 3,692:
}
let uiImage = UIImage(cgImage: context.makeImage()!)</
=={{header|TI-83 BASIC}}==
<
Input "ITERS:",M
[[0,0,1]]→[A]
Line 3,240 ⟶ 3,732:
Pxl-On(E,F)
I+1→I
End</
=={{header|Unicon}}==
{{libheader|graphics}}
<
link graphics
Line 3,311 ⟶ 3,803:
}
end
</syntaxhighlight>
=={{header|VBA}}==
<
Dim chrt As Chart
Set chrt = ActiveSheet.Shapes.AddChart.Chart
Line 3,347 ⟶ 3,839:
Next i
plot_coordinate_pairs x, y
End Sub</
/* {{header|Visual Basic .NET}} */ Section added
=={{header|Visual Basic .NET}}==
{{works with|Visual Basic .NET|2011}}
<
Public Class BarnsleyFern
Line 3,385 ⟶ 3,877:
End Sub 'Paint
End Class 'BarnsleyFern</
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|DOME}}
<
import "dome" for Window
import "random" for Random
Line 3,442 ⟶ 3,934:
}
var Game = BarnsleyFern.new(640, 640, 200000)</
{{out}}
[[File:Wren-Barnsley_fern.png|400px]]
=={{header|XPL0}}==
<
real NX, NY, X, Y;
[SetVid($12); \set 640x480x4 VGA graphics (on PC or RPi)
Line 3,459 ⟶ 3,954:
Point(320+fix(X*40.0), 440-fix(Y*40.0), 2\green\);
]
]</
=={{header|Yabasic}}==
{{trans|ZX Spectrum Basic}}
Classic style
<
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,476 ⟶ 3,971:
100 LET x=nx: LET y=ny
110 DOT x*wid/12,y*hei/12
120 NEXT n</
Modern style <
wid = 800 : hei = 600 : open window wid, hei : window origin "cb"
backcolor 0, 0, 0 : color 0, 255, 0 : clear window
Line 3,490 ⟶ 3,985:
x = nx : y = ny
dot x * wid / 12, y * hei / 12
next</
=={{header|zkl}}==
Line 3,496 ⟶ 3,991:
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
{{trans|Java}}
<
w,h:=640,640;
bitmap:=PPM(w+1,h+1,0xFF|FF|FF); // White background
Line 3,511 ⟶ 4,006:
}
bitmap.writeJPGFile("barnsleyFern.jpg");
}();</
=={{header|ZX Spectrum Basic}}==
{{trans|zkl}}
<
20 PAPER 7: BORDER 7: BRIGHT 1: INK 4: CLS
30 LET maxpoints=20000: LET x=0: LET y=0
Line 3,527 ⟶ 4,022:
110 PLOT x*17+127,y*17
120 NEXT n
</syntaxhighlight>
It is recommended to run on an emulator that supports running at full speed.
|