Koch curve: Difference between revisions

m
 
(7 intermediate revisions by 5 users not shown)
Line 193:
end Koch_Curve;</syntaxhighlight>
 
=={{header|BASIC256ALGOL 68}}==
{{libheader|ALGOL 68-l-system}}
<syntaxhighlight lang="basic256">global RtoD, DtoR
Generates an SVG file containing the curve using the L-System. Very similar to the Algol 68 [[Sierpinski square curve]] sample.
RtoD = 180 / Pi
<br/>
DtoR = Pi / 180
Note the Algol 68 L-System library source code is on a separate page on Rosetta Code - follow the above link and then to the Talk page.
<syntaxhighlight lang="algol68">
BEGIN # Koch Curve in SVG #
# uses the RC Algol 68 L-System library for the L-System evaluation & #
# interpretation #
 
PR read "lsystem.incl.a68" PR # include L-System utilities #
global posX, posY, angulo
posX = 170 : posY = 100 : angulo = 0
 
PROC koch curve = ( STRING fname, INT size, length, order, init x, init y )VOID:
global ancho, alto
IF FILE svg file;
ancho = 650 : alto = 650
BOOL open error := IF open( svg file, fname, stand out channel ) = 0
graphsize ancho, alto
THEN
# opened OK - file already exists and #
# will be overwritten #
FALSE
ELSE
# failed to open the file #
# - try creating a new file #
establish( svg file, fname, stand out channel ) /= 0
FI;
open error
THEN # failed to open the file #
print( ( "Unable to open ", fname, newline ) );
stop
ELSE # file opened OK #
 
REAL x := init x;
subroutine kochLado(longitud, fondo)
REAL y := init y;
if fondo = 0 then
INT angle := 0;
dx = cos(angulo*DtoR) * longitud
put( svg file, ( "<svg xmlns='http://www.w3.org/2000/svg' width='"
dy = sin(angulo*DtoR) * longitud
, whole( size, 0 ), "' height='", whole( size, 0 ), "'>"
color rgb(5,100,24)
, newline, "<rect width='100%' height='100%' fill='white'/>"
line (posX, posY, posX+dx, posY+dy)
, newline, "<path stroke-width='1' stroke='black' fill='none' d='"
posX += dx
, newline, "M", whole( x, 0 ), ",", whole( y, 0 ), newline
posY += dy
)
else
);
call kochLado(longitud/3.0, fondo-1)
angulo += 60
call kochLado(longitud/3.0, fondo-1)
angulo -= 120
call kochLado(longitud/3.0, fondo-1)
angulo += 60
call kochLado(longitud/3.0, fondo-1)
end if
end subroutine
 
LSYSTEM ssc = ( "F++F++F"
subroutine CopoNieveKoch(longitud, recursionfondo)
, ( "F" -> "F-F++F-F"
for i = 1 to 6
)
call kochLado(longitud,recursionfondo)
);
angulo -= 300
STRING curve = ssc EVAL order;
next i
curve INTERPRET ( ( CHAR c )VOID:
end subroutine
IF c = "F" THEN
x +:= length * cos( angle * pi / 180 );
y +:= length * sin( angle * pi / 180 );
put( svg file, ( " L", whole( x, 0 ), ",", whole( y, 0 ), newline ) )
ELIF c = "+" THEN
angle +:= 60 MODAB 360
ELIF c = "-" THEN
angle -:= 60 MODAB 360
FI
);
put( svg file, ( "'/>", newline, "</svg>", newline ) );
close( svg file )
FI # sierpinski square # ;
 
koch curve( "koch.svg", 600, 5, 4, 150, 150 )
for n = 0 To 7
clg
fastgraphics
text 3,4, "Copo de nieve de Koch"
text 4,16, "Iteración número: " & n
call CopoNieveKoch(280, n)
pause 0.8
refresh
next n
 
END
imgsave "Koch_curve.jpg", "jpg"
end</syntaxhighlight>
 
=={{header|Amazing Hopper}}==
Line 339 ⟶ 355:
back
</syntaxhighlight>
 
 
=={{header|AutoHotkey}}==
Line 425 ⟶ 440:
ExitApp
Return</syntaxhighlight>
 
=={{header|BASIC256}}==
<syntaxhighlight lang="basic256">global RtoD, DtoR
RtoD = 180 / Pi
DtoR = Pi / 180
 
global posX, posY, angulo
posX = 170 : posY = 100 : angulo = 0
 
global ancho, alto
ancho = 650 : alto = 650
graphsize ancho, alto
 
subroutine kochLado(longitud, fondo)
if fondo = 0 then
dx = cos(angulo*DtoR) * longitud
dy = sin(angulo*DtoR) * longitud
color rgb(5,100,24)
line (posX, posY, posX+dx, posY+dy)
posX += dx
posY += dy
else
call kochLado(longitud/3.0, fondo-1)
angulo += 60
call kochLado(longitud/3.0, fondo-1)
angulo -= 120
call kochLado(longitud/3.0, fondo-1)
angulo += 60
call kochLado(longitud/3.0, fondo-1)
end if
end subroutine
 
subroutine CopoNieveKoch(longitud, recursionfondo)
for i = 1 to 6
call kochLado(longitud,recursionfondo)
angulo -= 300
next i
end subroutine
 
for n = 0 To 7
clg
fastgraphics
text 3,4, "Copo de nieve de Koch"
text 4,16, "Iteración número: " & n
call CopoNieveKoch(280, n)
pause 0.8
refresh
next n
 
imgsave "Koch_curve.jpg", "jpg"
end</syntaxhighlight>
 
=={{header|C}}==
Line 852 ⟶ 918:
=={{header|EasyLang}}==
 
[https://easylang.dev/show/#cod=ZZLdUoMwEIXv8xTn0tohhp/WK3wXB6llrKQDjCZv754lFWOvIOfbs3uycJ18hw/fnRFKxBKhQqwwLP0EC2sAhBotHoQ+osJeCnZ4Qk0SlcQbiRsJTfKwnnjzNMnD+oyEgxAZthdjg0Jed8I7P+PoKEaKUcV5GEXUdjRJjiIzrfyfae1E03BaL/gCPVLhsWhRpvPfhdTsz4KMqSqRJcAdU1XiyPA7puq2ZLL+Mvep5jKM/To3EzgsEzghE9g2EziBgjXWUPke3pYznK2NtJeLHkzks3bm03/dZp78hNfxXXSHeemvKCuHxaNqdFHStIV+1GeXFirV+hlI4i/h/hO5+7U0pmYIGlBjSFZrfgA= Run it]
[https://easylang.dev/show/#cod=bZHNboMwEITvfoo5No1wzU/SE32XiJIGleIIUGu/fXcWUuQmJ/B8s7vj9XX0DT59c0HIEXOEArFAN7cjLKwBEErUeBL6jAJ7MezwgpIkKok3EjcSqrWGfuKtplpr6E9IOAiRYXsprJDJ70544yccHcVIMao4dYOI2o5FkiNLihb+r2jpxKLuvFzwDXqkwmNWI1/Pzanvk62UHELXvUGRhJcojw2KJJ1keWxQtC2ehraf2tXYd0O7xEgEjk0EjkkEtk0ETqBgjTVUfrr3+QJnSyPt5fIHE/ktnfny37eZZz/iNHyI7jDN7RV54TB7FJUuT5rW0Id+deuSxa1PQxL/CN9kJXfb1atrVg0SNKVmkcDW/AI= Run it]
 
<syntaxhighlight lang="easylang">
Line 864 ⟶ 930:
if iter > 0
iter -= 1
call koch x1 y1 x3 y3 iter
call koch x3 y3 x5 y5 iter
call koch x5 y5 x4 y4 iter
call koch x4 y4 x2 y2 iter
else
line x1 y1
Line 883 ⟶ 949:
x2 = x1 + 70 * cos ang
y2 = y1 + 70 * sin ang
call koch x1 y1 x2 y2 4
x1 = x2
y1 = y2
Line 1,021 ⟶ 1,087:
End
</syntaxhighlight>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/L-system}}
 
'''Solution'''
 
It can be done using an [[wp:L-system|L-system]]. There are generic functions written in Fōrmulæ to compute an L-system in the page [[L-system#Fōrmulæ | L-system]].
 
The program that creates a Koch curve is:
 
[[File:Fōrmulæ - L-system - Koch's snowflake 01.png]]
 
[[File:Fōrmulæ - L-system - Koch's snowflake 02.png]]
 
=={{header|Go}}==
Line 2,007 ⟶ 2,087:
GeometricTransformation[KochCurve[5], RotationTransform[-Pi/3, {1, 0}]],
GeometricTransformation[KochCurve[5], RotationTransform[Pi/3, {0, 0}]]}]</syntaxhighlight>
 
=={{header|Maxima}}==
Using [https://riotorto.users.sourceforge.net/Maxima/gnuplot/turtle/turtle.mac turtle.mac] package for turtle graphics in Maxima.
<syntaxhighlight lang="maxima">
set_draw_defaults(
terminal = svg,
dimensions = [350,350],
proportional_axes = xy) $
 
wxdraw2d(
turtle(
to(koch_snowflake, [n, len],
ifelse(n = 0,
[forward(len)],
[koch_snowflake(n - 1, len),
right(60),
koch_snowflake(n - 1, len),
left(120),
koch_snowflake(n - 1, len),
right(60),
koch_snowflake(n - 1, len)]
)
),
repeat(6,
koch_snowflake(5, 300),
right(60)
)
)
);
</syntaxhighlight>
[[File:KochSnowflake.png|thumb|center]]
 
=={{header|Nim}}==
Line 3,239 ⟶ 3,350:
{{trans|Go}}
{{libheader|DOME}}
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, Color, Point
import "dome" for Window
import "math" for M
3,026

edits