Koch curve: Difference between revisions
m
→{{header|ALGOL 68}}: comments
m (→{{header|ALGOL 68}}: comments) |
|||
(7 intermediate revisions by 5 users not shown) | |||
Line 193:
end Koch_Curve;</syntaxhighlight>
=={{header|
{{libheader|ALGOL 68-l-system}}
Generates an SVG file containing the curve using the L-System. Very similar to the Algol 68 [[Sierpinski square curve]] sample.
<br/>
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 #
PROC koch curve = ( STRING fname, INT size, length, order, init x, init y )VOID:
IF FILE svg file;
BOOL open error := IF open( svg file, fname, stand out channel ) = 0
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;
REAL y := init y;
INT angle := 0;
put( svg file, ( "<svg xmlns='http://www.w3.org/2000/svg' width='"
, whole( size, 0 ), "' height='", whole( size, 0 ), "'>"
, newline, "<rect width='100%' height='100%' fill='white'/>"
, newline, "<path stroke-width='1' stroke='black' fill='none' d='"
, newline, "M", whole( x, 0 ), ",", whole( y, 0 ), newline
)
);
LSYSTEM ssc = ( "F++F++F"
, ( "F" -> "F-F++F-F"
)
);
STRING curve = ssc EVAL order;
curve INTERPRET ( ( CHAR c )VOID:
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 )
END
=={{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]
<syntaxhighlight lang="easylang">
Line 864 ⟶ 930:
if iter > 0
iter -= 1
else
line x1 y1
Line 883 ⟶ 949:
x2 = x1 + 70 * cos ang
y2 = y1 + 70 * sin ang
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="
import "dome" for Window
import "math" for M
|