The algorithm for generating a figure using the example of a Christmas tree
Introduction
This example presents an algorithm for generating a fractal structure using stochastic point distribution and visualization using the example of a Christmas tree.
We will attach the necessary libraries. We use the CairoMakie library as a graphical framework.
In [ ]:
# import Pkg
# Pkg.add(["Random" ,"LinearAlgebra", "Colors", "FreeTypeAbstraction", "CairoMakie"])
using Random, LinearAlgebra, Colors, FreeTypeAbstraction
import CairoMakie as CM
Let's define the shape parameters.
In [ ]:
CM.set_theme!()
Random.seed!(123)
number of dots = 6^5
number of embellishments = 50
types of embellishments = rand(1:6, number of embellishments)
color indexes = randperm(number of dots)[1:number of embellishments]
random_number() = rand(number of dots)
angles = random_number() .* 2π
heights = random_number()
radii = 0.4 .* (1 .- heights)
density = random_number()
coordinates x = radii .* cos.(corners)
coordinates y = radii .* sin.(corners)
color_adorning = [
CM.RGBf(1, 0, 0),
CM.RGBf(1, 1, 0),
CM.RGBf(1, 0, 1),
CM.RGBf(0, 0, 1),
CM.RGBf(0.5, 0.5, 0.5),
CM.RGBf(0, 1, 1)
]
embellishment_ markers = [:circle, :star5, :diamond, :utriangle, :hexagon, :cross]
Let's display a figure in the form of a Christmas tree.
In [ ]:
Christmas tree = CM.Figure(backgroundcolor=CM.RGBf(0, 0, 0), size=(500, 500))
ось = CM.Axis3(ёлка[1, 1], aspect=:data, viewmode=:stretch, limits=(-0.6, 0.6, -0.6, 0.6, -0.15, 1.1), azimuth=0.0, elevation=0.0, title="")
CM.hidedecorations!(axis)
CM.hidespines!(axis)
white_colors = [CM.RGBAf(0, density[i]*radii[i]^0.45, 0, 0.97) for i in 1:number of dots]
CM.scatter!(axis, coords_x .* density, coordinates of y .* density, heights,
color=color_references,
markersize=5,
marker=:circle)
for i in 1:6
indexes = indexes of embellishments[types of embellishments .== i]
if !isempty(indexes)
CM.scatter!(axis, coordinates x[indexes], coordinates y[indexes], heights[indexes], color=coloring color[i], markersize=15, marker=coloring markers[i], strokewidth=2, strokecolor=:white)
end
end
CM.scatter!(axis, [0], [0], [1.05], color=CM.RGBf(1, 0, 0), markersize=35, marker=:star5, strokewidth=3, strokecolor=CM.RGBf(1, 1, 0.5))
Number of points of the barrel = 350
barrel coords_x = Float64[]
barrel coordinates y = Float64[]
barrel coords_z = Float64[]
barrel radius_ = 0.025
for i in 1:the number of points of the barrel
z = -0.1 + rand() * 0.12
θ = rand() * 2π
r = barrel radius_ (0.7 + 0.6 * rand())
push!(barrel coordinates x, r * cos(θ))
push!(barrel coordinates, r * sin(θ))
push!(barrel coords_z, z)
end
CM.scatter!(axis, barrel co-ordinates x, barrel co-ordinates Y, barrel co-ordinates z,
color=CM.RGBf(0.4, 0.25, 0.1),
markersize=10,
marker=:circle)
Number of snowdrops = 250
coordinates of the snowdrops_x = Float64[]
coordinates of the snowdrops y = Float64[]
coordinates of the snowdrops_z = Float64[]
Snowflake size_ = Float64[]
for i in 1:the number of snowdrops
push!(coordinates of the snowdrops, -1.0 + rand() * 2.0)
push!(coordinates of the snowdrops, -1.0 + rand() * 2.0)
push!(snowdrop coordinates z, -0.2 + rand() * 1.4)
push!(the size of the snowdrops, 3 + rand()* 8)
end
snowdrop color = [CM.RGBAf(1, 1, 1, 0.8) for _ in 1:number of snowdrops]
CM.scatter!(axis, snowdrop co-ordinates x, snowdrop co-ordinates Y, snowdrop co-ordinates z,
color=the color of the snowdrops,
markersize=size of snowdrops,
marker=:circle)
CM.display(Christmas tree)
Out[0]:
Conclusion
As a result of the script execution, an image of a Christmas tree with decorations surrounded by snow flakes appears. In this way, you can make sure that graphic design can be done not only using specialized software, but also by creating scripts in Engee.
