Skip to content

Instantly share code, notes, and snippets.

@tk3369
Last active February 21, 2022 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tk3369/529fe2a909c8a649b98d1a49047188a6 to your computer and use it in GitHub Desktop.
Save tk3369/529fe2a909c8a649b98d1a49047188a6 to your computer and use it in GitHub Desktop.
# Modified from @frank's code at Discord
function koch_curve!(X, Y, x1, y1, x2, y2, level)
if level == 0
push!(X, x1)
push!(X, x2)
push!(Y, y1)
push!(Y, y2)
else
Δx = x2 - x1
Δy = y2 - y1
x3 = x1 + Δx / 3
y3 = y1 + Δy / 3
x5 = y1 + 2Δx / 3
y5 = y1 + 2Δy / 3
x4 = ((x1 + x2) / 2) - ((Δy / 2) * sqrt(3))
y4 = ((y1 + y2) / 2) + ((Δx / 2) * sqrt(3))
koch_curve!(X, Y, x1, y1, x3, y3, (level - 1))
koch_curve!(X, Y, x3, y3, x4, y4, (level - 1))
koch_curve!(X, Y, x4, y4, x5, y5, (level - 1))
koch_curve!(X, Y, x5, y5, x2, y2, (level - 1))
end
end
X = []
Y = []
koch_curve!(X, Y, 1.0, 2.0, 0.0, 1.0, 2)
#=
julia> [X Y]
32×2 Matrix{Float64}:
1.0 2.0
0.888889 1.88889
0.888889 1.88889
1.12201 1.54466
1.12201 1.54466
1.77778 1.77778
1.77778 1.77778
0.666667 1.66667
0.666667 1.66667
1.33333 1.33333
1.33333 1.33333
0.888889 1.22222
0.888889 1.22222
0.955342 0.0119661
0.955342 0.0119661
0.444444 1.11111
0.444444 1.11111
0.0 1.0
=#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment