Koch curve: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 7:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:REALMATH.ACT"
 
DEFINE MAXSIZE="20"
Line 125:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Koch_curve.png Screenshot from Atari 8-bit computer]
Line 131:
=={{header|Ada}}==
{{libheader|APDF}}
<langsyntaxhighlight Adalang="ada">with Ada.Command_Line;
with Ada.Numerics.Generic_Elementary_Functions;
with Ada.Text_IO;
Line 191:
Rule => Even_Odd);
Doc.Close;
end Koch_Curve;</langsyntaxhighlight>
 
=={{header|BASIC256}}==
<langsyntaxhighlight BASIC256lang="basic256">global RtoD, DtoR
RtoD = 180 / Pi
DtoR = Pi / 180
Line 242:
 
imgsave "Koch_curve.jpg", "jpg"
end</langsyntaxhighlight>
 
=={{header|Amazing Hopper}}==
Line 250:
or
./src/kock_curve.com > kockcurve.svg
<syntaxhighlight lang="amazing hopper">
<lang Amazing Hopper>
#!/usr/bin/hopper
 
Line 338:
{output}
back
</syntaxhighlight>
</lang>
 
 
Line 344:
{{trans|Go}}
Requires [https://www.autohotkey.com/boards/viewtopic.php?t=6517 Gdip Library]
<langsyntaxhighlight AutoHotkeylang="autohotkey">gdip1()
KochX := 0, KochY := 0
Koch(0, 0, A_ScreenWidth, A_ScreenHeight, 4, Arr:=[])
Line 424:
Gdip_Shutdown(pToken)
ExitApp
Return</langsyntaxhighlight>
 
=={{header|C}}==
Interactive program which takes the width, height (of the Graphics window) and recursion level of the Koch curve as inputs, prints out usage on incorrect invocation. Requires the [http://www.cs.colorado.edu/~main/bgi/cs1300/ WinBGIm] library.
<syntaxhighlight lang="c">
<lang C>
#include<graphics.h>
#include<stdlib.h>
Line 489:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
The output of this program is an SVG file depicting a Koch snowflake.
<langsyntaxhighlight lang="cpp">// See https://en.wikipedia.org/wiki/Koch_snowflake
#include <fstream>
#include <iostream>
Line 560:
koch_curve_svg(out, 600, 5);
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 567:
=={{header|Factor}}==
The approach taken is to generate a [[Thue-Morse]] sequence. Using turtle graphics, move forward for each 0 encountered and rotate 60 degrees for each 1 encountered. Remarkably, this produces a Koch curve.
<langsyntaxhighlight lang="factor">USING: accessors images images.testing images.viewer kernel
literals math math.constants math.functions sequences ;
IN: rosetta-code.koch-curve
Line 607:
] 2with each image-window ;
 
MAIN: koch-curve</langsyntaxhighlight>
{{out}}
[https://i.imgur.com/MVS8QiS.png]
Line 613:
=={{header|Forth}}==
{{works with|4tH v3.64}}
<syntaxhighlight lang="text">include lib/graphics.4th
include lib/math.4th
include lib/enter.4th
Line 648:
450 500 115 300 r> koch
 
s" gkoch.ppm" save_image \ save the image</langsyntaxhighlight>
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
Const Pi = 4 * Atn(1)
Const RtoD = 180 / Pi
Line 696:
Sleep
End
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
{{libheader|Go Graphics}}
{{trans|Ring}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 741:
dc.Stroke()
dc.SavePNG("koch.png")
}</langsyntaxhighlight>
 
{{out}}
Line 752:
Generates SVG for a Koch snowflake. To view, save to a text file with an .svg extension, and open in a browser.
 
<langsyntaxhighlight lang="haskell">import Data.Bifunctor (bimap)
import Text.Printf (printf)
 
Line 853:
)
)
xys</langsyntaxhighlight>
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "Koch.bas"
110 OPTION ANGLE DEGREES
120 SET 22,1:SET 23,0:SET 24,42:SET 25,26
Line 877:
290 PLOT LEFT A;FORWARD D;
300 END IF
310 END DEF</langsyntaxhighlight>
 
=={{header|J}}==
 
<langsyntaxhighlight Jlang="j">seg=: [ + [: +/\ (0,1r3*1,(^j.(,-)_2 o.1r2),1) * -~
koch=: [: ,/ 2 seg/\ ]
 
require'plot'
tri=: ^ j. 4r3 * (_2 o. 0) * i._4
plot koch ^: 5 tri</langsyntaxhighlight>
 
[[j:File:Koch-snowflake.png|example]]
Line 897:
 
{{Trans|Haskell}}{{Trans|Python}}
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,024:
// MAIN ---
return main();
})();</langsyntaxhighlight>
 
=={{header|jq}}==
Line 1,037:
depending on the location of the included file, and the command-line
options used.
<langsyntaxhighlight lang="jq">include "turtle" {search: "."};
 
def rules:
Line 1,066:
 
koch_curve(5)
| draw(1200)</langsyntaxhighlight>
 
 
=={{header|Julia}}==
Multiple snowflake plots. Copied from https://www.juliabloggers.com/koch-snowflakes-for-the-holidays/.
<langsyntaxhighlight Julialang="julia">using Plots
 
function pointskoch(points, maxk, α = sqrt(3)/2)
Line 1,241:
#large_koch()
koch_julia()
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|Ring}}
This incorporates code from other relevant tasks in order to provide a runnable example. The image produced is saved to disk where it can be viewed with a utility such as EOG.
<langsyntaxhighlight lang="scala">// Version 1.2.41
 
import java.awt.Color
Line 1,327:
ImageIO.write(image, "jpg", kFile)
}
}</langsyntaxhighlight>
 
{{output}}
Line 1,335:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
{def koch
{lambda {:d :n}
Line 1,362:
The output is a "square of Koch" which can be seen in
http://lambdaway.free.fr/lambdawalks/?view=koch
</syntaxhighlight>
</lang>
 
=={{header|Logo}}==
Line 1,394:
 
Make "startup [zzz]
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
Using the Bitmap class [[Bitmap#Lua|here]], with an ASCII pixel representation, then extending with <code>line()</code> as [[Bitmap/Bresenham%27s_line_algorithm#Lua|here]], then extending further..
<langsyntaxhighlight lang="lua">local cos, sin, floor, pi = math.cos, math.sin, math.floor, math.pi
 
function Bitmap:render()
Line 1,439:
bitmap:render()
print()
end</langsyntaxhighlight>
{{out}}
<pre style="font-size:50%">...........
Line 1,596:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight lang="mathematica">Graphics[{GeometricTransformation[KochCurve[5], RotationTransform[Pi, {0.5, 0}]],
GeometricTransformation[KochCurve[5], RotationTransform[-Pi/3, {1, 0}]],
GeometricTransformation[KochCurve[5], RotationTransform[Pi/3, {0, 0}]]}]</langsyntaxhighlight>
 
=={{header|Nim}}==
{{works with|nim|1.4.2}}
{{libheader|nim-libgd}}
<langsyntaxhighlight lang="nim">
from math import sin, cos, PI
import libgd
Line 1,645:
 
main()
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use SVG;
use List::Util qw(max min);
 
Line 1,684:
open $fh, '>', 'koch_curve.svg';
print $fh $svg->xmlify(-namespace=>'svg');
close $fh;</langsyntaxhighlight>
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/koch_curve.svg Koch curve] (offsite image)
 
Line 1,691:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/koch.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Koch_curve.exw
Line 1,769:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Processing}}==
<langsyntaxhighlight lang="java">int l = 300;
 
void setup() {
Line 1,816:
kcurve(0, s);
popMatrix();
}</langsyntaxhighlight>'''The sketch can be run online''' :<BR> [https://www.openprocessing.org/sketch/848209 here.]
 
==={{header|Processing Python mode}}===
<langsyntaxhighlight lang="python">l = 300
 
def setup():
Line 1,861:
rotate(radians(-120))
kcurve(0, s)
popMatrix()</langsyntaxhighlight>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
Produces an SVG file showing a Koch snowflake.
<langsyntaxhighlight lang="prolog">main:-
write_koch_snowflake('koch_snowflake.svg').
 
Line 1,910:
koch_curve(Stream, X2, Y2, X3, Y3, N1),
koch_curve(Stream, X3, Y3, X4, Y4, N1),
koch_curve(Stream, X4, Y4, X1, Y1, N1).</langsyntaxhighlight>
 
{{out}}
Line 1,920:
 
{{Trans|Haskell}}
<langsyntaxhighlight lang="python">'''Koch curve'''
 
from math import cos, pi, sin
Line 2,056:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
 
===String rewriting===
<langsyntaxhighlight lang="python">import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import hsv_to_rgb as hsv
Line 2,090:
#curve('F', {'F': 'G-F-G', 'G': 'F+G+F'}, 60, 7)
#curve('A', {'A': '+BF-AFA-FB+', 'B': '-AF+BFB+FA-'}, 90, 6)
#curve('FX+FX+', {'X': 'X+YF', 'Y': 'FX-Y'}, 90, 12)</langsyntaxhighlight>
 
=={{header|QBasic}}==
<langsyntaxhighlight lang="qbasic"> ' Chaos: start at any point, this program uses the middle of the screen (or universe. One of six
' degrees of freedom (a direction) is chosen at random (by throwing a six-sided die), and a line
' is drawn from the old point to the new point in the direction indicated by the pip on the die.
Line 2,311:
Y= Y + YP(N, R)
LINE -(X, Y) ' depending on the "die", draw the next part of the chaos curve.
GOTO 900 ' now, go and do another point.</langsyntaxhighlight><br><br>
 
=={{header|Quackery}}==
Line 2,317:
Using an L-system.
 
<langsyntaxhighlight Quackerylang="quackery"> [ $ "turtleduck.qky" loadfile ] now!
[ $ "" swap witheach
Line 2,338:
char L = iff
[ -1 6 turn ] done
1 6 turn ]</langsyntaxhighlight>
 
{{output}}
Line 2,345:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
 
(require metapict)
Line 2,371:
(koch a c n)))
 
(scale 4 (snow 2))</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 2,377:
{{works with|Rakudo|2018.03}}
Koch curve, actually a full Koch snowflake.
<syntaxhighlight lang="raku" perl6line>use SVG;
 
role Lindenmayer {
Line 2,407:
:polyline[ points => @points.join(','), :fill<white> ],
],
);</langsyntaxhighlight>
 
See: [https://github.com/thundergnat/rc/blob/master/img/koch1.svg Koch snowflake]
 
Variation using 90° angles:
<syntaxhighlight lang="raku" perl6line>use SVG;
 
role Lindenmayer {
Line 2,441:
:polyline[ points => @points.join(','), :fill<white> ],
],
);</langsyntaxhighlight>
See: [https://github.com/thundergnat/rc/blob/master/img/koch2.svg Koch curve variant with 90° angles]
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Koch curve
 
Line 2,513:
paint.drawline(x4, y4, x2, y2)
ok
</syntaxhighlight>
</lang>
Output image:
 
Line 2,522:
{{libheader|JRubyArt}}
Using a Lindenmayer System to produce a KochSnowflake or simple Koch Fractal
<langsyntaxhighlight lang="ruby">
attr_reader :koch
def settings
Line 2,618:
end
 
</syntaxhighlight>
</lang>
 
=={{header|Rust}}==
Output is a file in SVG format depicting a Koch snowflake.
<langsyntaxhighlight lang="rust">// [dependencies]
// svg = "0.8.0"
 
Line 2,686:
fn main() {
write_koch_snowflake("koch_snowflake.svg", 600, 5).unwrap();
}</langsyntaxhighlight>
 
{{out}}
Line 2,693:
=={{header|Sidef}}==
Using the LSystem class defined at [https://rosettacode.org/wiki/Hilbert_curve#Sidef Hilbert curve].
<langsyntaxhighlight lang="ruby">var rules = Hash(
F => 'F+F--F+F',
)
Line 2,709:
)
 
lsys.execute('F--F--F', 4, "koch_snowflake.png", rules)</langsyntaxhighlight>
 
Output image: [https://github.com/trizen/rc/blob/master/img/koch-snowflake-sidef.png Koch snowflake]
Line 2,717:
A Turtle graphics library makes defining the curve easy.
 
<syntaxhighlight lang="vb">
<lang vb>
 
option explicit
Line 2,824:
set x=nothing 'show image in browser
 
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|DOME}}
<langsyntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color, Point
import "dome" for Window
import "math" for M
Line 2,868:
 
static draw(dt) {}
}</langsyntaxhighlight>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">real PosX, PosY, Angle;
 
proc DrawSide(Depth, Dist); \Draw side as 4 segments
Line 2,897:
Angle:= Angle + 120.;
];
]</langsyntaxhighlight>
 
{{out}}
Line 2,908:
Uses Image Magick and
the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
<langsyntaxhighlight lang="zkl">var width=512, height=512, img=PPM(width,height,0xFfffFF); // white canvas
var angle=(60.0).toRad();
const green=0x00FF00;
Line 2,935:
 
koch(100.0,100.0, 400.0,400.0, 4);
img.writeJPGFile("koch.zkl.jpg");</langsyntaxhighlight>
Image at [http://www.zenkinetic.com/Images/RosettaCode/koch.zkl.jpg koch curve]
 
Line 2,941:
Using a Lindenmayer system and turtle graphics to draw a Koch snowflake:
 
<langsyntaxhighlight lang="zkl">lsystem("F--F--F", Dictionary("F","F+F--F+F"), "+-", 4) // snowflake
//lsystem("F", Dictionary("F","F+F--F+F"), "+-", 3) // curve
: turtle(_);
Line 2,971:
}
img.writeJPGFile("kochSnowFlake.zkl.jpg");
}</langsyntaxhighlight>
Image at [http://www.zenkinetic.com/Images/RosettaCode/kochSnowFlake.zkl.jpg Koch snow flake]
10,333

edits