Koch curve: Difference between revisions

m
 
(15 intermediate revisions by 7 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 849 ⟶ 915:
<pre>
</pre>
 
=={{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">
proc koch x1 y1 x2 y2 iter . .
x3 = (x1 * 2 + x2) / 3
y3 = (y1 * 2 + y2) / 3
x4 = (x1 + x2 * 2) / 3
y4 = (y1 + y2 * 2) / 3
x5 = x3 + (x4 - x3) * cos 60 + (y4 - y3) * sin 60
y5 = y3 - (x4 - x3) * sin 60 + (y4 - y3) * cos 60
if iter > 0
iter -= 1
koch x1 y1 x3 y3 iter
koch x3 y3 x5 y5 iter
koch x5 y5 x4 y4 iter
koch x4 y4 x2 y2 iter
else
line x1 y1
line x3 y3
line x5 y5
line x4 y4
line x2 y2
.
.
linewidth 0.3
x1 = 15
y1 = 30
move x1 y1
for ang = 0 step 120 to 240
x2 = x1 + 70 * cos ang
y2 = y1 + 70 * sin ang
koch x1 y1 x2 y2 4
x1 = x2
y1 = y2
.
</syntaxhighlight>
 
=={{header|Factor}}==
Line 982 ⟶ 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 1,173 ⟶ 1,292:
plot koch ^: 5 tri</syntaxhighlight>
 
[[j:File:J-Koch-snowflake.png|example]]
 
The idea is to continually expand the segments between a list of points in the complex plane. Given consecutive x and y in the list, v = (y-x)/3 is a vector representing 1/3 of the trip from x to y. An iteration building the Koch curve starts at x, advances by v, advances by v rotated by 60 degrees, advances by v rotated -60 degrees, and finally advances by another v, reaching y. x seg y produces this expansion. koch takes a list of points and expands segments between consecutive ones, producing another list. koch ^: 5 does this 5 times, and plot shows the snowflake in a window.
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
<syntaxhighlight>
 
import java.awt.Point;
Line 1,198 ⟶ 1,316:
String text = kochCurveText(points, IMAGE_SIZE);
Files.write(Paths.get("C:/Users/psnow/Desktop/koch.svg"), text.getBytes());
}
private static List<Point> initialEquilateralTriangle() {
final int boxSizemargin = IMAGE_SIZE - MARGIN50;
final int boxSize = IMAGE_SIZE - margin;
final int sideLength = Math.round(boxSize * SIN_60_DEGREES);
final int x = ( boxSize + MARGINmargin - sideLength ) / 2;
final int y = Math.round(( boxSize + MARGINmargin ) / 2 - sideLength * SIN_60_DEGREES / 3);
List<Point> points = Arrays.asList(
Line 1,221 ⟶ 1,340:
for ( int i = 0; i < aPoints.size() - 1; i++ ) {
final int x0 = aPoints.get(i).x;
final int y0 = aPoints.get(i).y;
final int x1 = aPoints.get(i + 1).x;
final int y1 = aPoints.get(i + 1).y;
final int dy = y1 - y0;
final int dx = x1 - x0;
result.add(aPoints.get(i));
Line 1,246 ⟶ 1,365:
text.append("<polygon points='");
for ( int i = 0; i < aPoints.size(); i++ ) {
text.append(aPoints.get(i).x + ", " + aPoints.get(i).y + " ");
}
text.append("' style='fill:pink;stroke:black;stroke-width:2'/>\n</svg>\n");
Line 1,253 ⟶ 1,372:
}
private static final int IMAGE_SIZE = 700;
private static final int MARGIN = 50;
private static final float SIN_60_DEGREES = (float) Math.sin(Math.PI / 3.0);
Line 1,969 ⟶ 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,201 ⟶ 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