Barnsley fern: Difference between revisions
m
syntax highlighting fixup automation
(Barnsley fern in BASIC256) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
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|Applesoft BASIC}}==
<
110 XX(2) = .85:XY(2) = .04
120 YX(2) = - .04:YY(2) = .85
Line 308:
330 HPLOT X% * 2 + 1,Y%
340 NEXT
</syntaxhighlight>
=={{header|BASIC256}}==
<
# call the subroutine with the altoght you want
# it's possible to have a window that's large than your display
Line 347:
# remove comment (#) in next line to save window as .png file
# imgsave("Barnsley_fern.png")
end subroutine</
{{out}}
[https://www.dropbox.com/s/8rkgguj3ol57771/Barnsley_fern_BASIC256.png?dl=0 Barnsley fern BASIC256 image]
Line 354:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
X=0 : Y=0
FOR I%=1 TO 100000
Line 367:
PLOT 1000 + X * 130 , Y * 130
NEXT
END</
=={{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 434:
return 0;
}
</syntaxhighlight>
=={{header|C sharp|C#}}==
<
using System.Diagnostics;
using System.Drawing;
Line 482:
}
}
}</
=={{header|C++}}==
[[File:BFCpp.png|200px|thumb|right]]
<
#include <windows.h>
#include <ctime>
Line 617:
fern f; f.draw(); return 0;
}
</syntaxhighlight>
===Cross-Platform Alternative===
Line 623:
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 680:
}
return EXIT_SUCCESS;
}</
{{out}}
Line 687:
=={{header|Common Lisp}}==
This code uses the <code>opticl</code> package for generating an image and saving it as a PNG file.
<
(:use #:cl
#:opticl))
Line 734:
(set-pixel image x y)
(multiple-value-setq (x y) (funcall (choose-transform) x y)))
(write-png-file filespec image)))</
=={{header|D}}==
{{trans|Raku}}
{{libheader|dlib}}
<
/+ dub.sdl:
dependency "dlib" version="~>0.21.0"
Line 795:
}
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 863:
end;
end.</
=={{header|EasyLang}}==
Line 869:
[https://easylang.online/apps/barnsley-fern.html Run it]
<syntaxhighlight lang=text>color 060
for i range 200000
r = randomf
Line 889:
move 50 + x * 15 100 - y * 10
rect 0.3 0.3
.</
=={{header|Emacs Lisp}}==
<
(defun make-array (size)
Line 945:
(barnsley-show arr size)))
(barnsley 400 35 100000)</
=={{header|F sharp|F#}}==
<
open System.Drawing
Line 977:
|> 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 992:
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 1,049:
;
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,121:
;
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,176:
close(1)
end program BarnsleyFern
</syntaxhighlight>
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 1,232:
Windowtitle "hit any key to end program"
Sleep
End</
=={{header|Frink}}==
<
g = new graphics
g.backgroundColor[0,0,0] // black
Line 1,272:
g.show[]
</syntaxhighlight>
=={{header|Fōrmulæ}}==
Line 1,283:
=={{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,306:
)"}
-r 40%,40%,1,1,2
</syntaxhighlight>
=={{header|gnuplot}}==
Line 1,313:
[[File:BarnsleyFernGnu.png|right|thumb|Output BarnsleyFernGnu.png]]
<
## Barnsley fern fractal 2/17/17 aev
reset
Line 1,338:
set output
unset print
</
{{Output}}
<pre>
Line 1,346:
=={{header|Go}}==
<
import (
Line 1,417:
log.Fatal(err)
}
}</
=={{header|Groovy}}==
Line 1,423:
{{libheader|JavaFX}}
<
import javafx.application.Application
import javafx.scene.Group
Line 1,483:
}
}
</syntaxhighlight>
=={{header|Haskell}}==
<
import Diagrams.Backend.Rasterific.CmdLine
import Diagrams.Prelude
Line 1,529:
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,553:
290 LET X=NX:LET Y=NY
300 PLOT X*96+600,Y*96
310 NEXT</
=={{header|J}}==
[[File:jfern.png|200px|thumb|right]]
<
f=: |: 0 ". ];._2 noun define
Line 1,574:
plotFern=: 'dot;grids 0 0;tics 0 0;labels 0 0;color green' plot ;/@|:
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,646:
});
}
}</
=={{header|JavaScript}}==
Line 1,652:
[[File:BarnsleyFernjs.png|right|thumb|Output BarnsleyFernjs.png]]
<
//6/17/16 aev
function pBarnsleyFern(canvasId, lim) {
Line 1,693:
ctx.fillRect(x * 50 + 260, -y * 50 + 540, 1, 1);
} //fend i
}</
'''Executing:'''
<
<html>
<head><script src="BarnsleyFern.js"></script></head>
Line 1,703:
</body>
</html>
</
{{Output}}
<pre>
Line 1,712:
{{works with|Julia|0.6}}
<
funs = (
(x, y) -> (0, 0.16y),
Line 1,729:
end
return rst
end</
=={{header|Kotlin}}==
{{trans|Java}}
<
import java.awt.*
Line 1,798:
f.setVisible(true)
}
}</
=={{header|Lambdatalk}}==
<
{def fern
{lambda {:size :sign}
Line 1,821:
{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,856:
[q]
close #1
</syntaxhighlight>
=={{header|Locomotive Basic}}==
{{trans|ZX Spectrum Basic}}
<
20 scale=38
30 maxpoints=20000: x=0: y=0
Line 1,871:
100 x=nx: y=ny
110 plot scale*x+320,scale*y
120 next</
=={{header|Lua}}==
Needs LÖVE 2D Engine
<
g = love.graphics
wid, hei = g.getWidth(), g.getHeight()
Line 1,909:
g.draw( canvas )
end
</syntaxhighlight>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
BarnsleyFern[{x_, y_}] := Module[{},
i = RandomInteger[{1, 100}];
Line 1,922:
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}}
<
x = 0
y = 0
Line 1,947:
y = 0.26 * xp + 0.24 * y + 0.44
end if
end for</
=={{header|Nim}}==
<
import nimPNG, random
Line 2,020:
discard savePNG24("fern.png",img.toString, width, height)
</syntaxhighlight>
=={{header|Oberon-2}}==
[[File:Barnsleyfern-oberon2.png|250px|thumb|right]]
<
MODULE BarnsleyFern;
(**
Line 2,079:
Init;Draw
END BarnsleyFern.
</syntaxhighlight>
=={{header|PARI/GP}}==
Line 2,086:
[[File:BarnsleyFern.png|right|thumb|Output BarnsleyFern.png]]
<
\\ Barnsley fern fractal
\\ 6/17/16 aev
Line 2,108:
pBarnsleyFern(530,100000); \\ BarnsleyFern.png
}
</
{{Output}}
Line 2,119:
=={{header|Perl}}==
[[File:BarnsleyFernPerl.png|250px|thumb|right]]
<
my $w = 640;
Line 2,141:
$img->flip(dir => 'v');
$img->write(file => 'barnsleyFern.png');</
=={{header|Phix}}==
Line 2,147:
{{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,193:
<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,229:
(prinl "P1")
(prinl 640 " " 640)
(mapc prinl G) ) )</
=={{header|Processing}}==
<
size(640, 640);
background(0, 0, 0);
Line 2,271:
}
noLoop();
}</
==={{header|Processing Python mode}}===
<
background(0)
Line 2,335:
n = height - round(60 * y)
set(m, n, "#00ff00")</
=={{header|PureBasic}}==
<
DisableDebugger
Line 2,379:
Repeat : Until WaitWindowEvent(50)=#PB_Event_CloseWindow
EndIf
End</
=={{header|Python}}==
<
import random
Line 2,426:
fern.fern.show()
</syntaxhighlight>
=={{header|QB64}}==
<
Dim As Integer sw, sh
sw = 400: sh = 600
Line 2,461:
Sleep
System</
Line 2,468:
{{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,502:
## Executing:
pBarnsleyFern("BarnsleyFernR", 100000, "dark green", "Barnsley Fern Fractal", psz=600)
</
{{Output}}
Line 2,514:
==='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
Line 2,553:
#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,589:
bmp
(send bmp save-file "images/racket-barnsley-fern.png" 'png)</
=={{header|Raku}}==
Line 2,596:
{{trans|Perl}}
[[File:Barnsley-fern-perl6.png|250px|thumb|right]]
<syntaxhighlight lang=raku
my ($w, $h) = (640, 640);
Line 2,615:
}
$png.write: 'Barnsley-fern-perl6.png';</
=={{header|REXX}}==
Line 2,622:
<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,645:
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}}==
<
Load "guilib.ring"
Line 2,765:
return
</syntaxhighlight>
=={{header|Ruby}}==
{{libheader|RubyGems}}
{{libheader|JRubyArt}}
<
MAX_ITERATIONS = 200_000
Line 2,813:
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,846:
NEXT n
render #g
#g "flush"</
=={{header|Rust}}==
Line 2,852:
{{libheader|rand}}
<
extern crate raster;
Line 2,896:
raster::save(&image, "fractal.png").unwrap();
}</
=={{header|Scala}}==
===Java Swing Interoperability===
<
import java.awt.image.BufferedImage
Line 2,962:
})
}</
=={{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 3,025:
(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 3,068:
axes.isoview="on";
axes.children.children.mark_foreground=13;
</syntaxhighlight>
=={{header|SequenceL}}==
'''Tail-Recursive SequenceL Code:'''<br>
<
import <Utilities/Random.sl>;
Line 3,103:
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,140:
return 0;
}</
{{out}}
Line 3,146:
=={{header|Sidef}}==
<
var w = 640
Line 3,168:
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,185:
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,248:
XtRealizeWidget shell
)
end ; </
call
demoWindow () ;
Line 3,255:
Output is viewable in a playground.
<
import CoreImage
import PlaygroundSupport
Line 3,298:
}
let uiImage = UIImage(cgImage: context.makeImage()!)</
=={{header|TI-83 BASIC}}==
<
Input "ITERS:",M
[[0,0,1]]→[A]
Line 3,338:
Pxl-On(E,F)
I+1→I
End</
=={{header|Unicon}}==
{{libheader|graphics}}
<
link graphics
Line 3,409:
}
end
</syntaxhighlight>
=={{header|VBA}}==
<
Dim chrt As Chart
Set chrt = ActiveSheet.Shapes.AddChart.Chart
Line 3,445:
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,483:
End Sub 'Paint
End Class 'BarnsleyFern</
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|DOME}}
<
import "dome" for Window
import "random" for Random
Line 3,540:
}
var Game = BarnsleyFern.new(640, 640, 200000)</
=={{header|XPL0}}==
<
real NX, NY, X, Y;
[SetVid($12); \set 640x480x4 VGA graphics (on PC or RPi)
Line 3,557:
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,574:
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,588:
x = nx : y = ny
dot x * wid / 12, y * hei / 12
next</
=={{header|zkl}}==
Line 3,594:
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,609:
}
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,625:
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.
|