裏 RjpWiki

Julia ときどき R, Python によるコンピュータプログラム,コンピュータ・サイエンス,統計学

Julia で円をモチーフにした格子模様を描く with cherry blossoms(5)

2021年08月18日 | ブログラミング

Julia で円をモチーフにした格子模様を描く(5)

plotter.jl を include
https://blog.goo.ne.jp/r-de-r/e/bd71a52a09801335d56f7c47d879bfe3

include("plotter.jl")

function en5(nx=6, ny=5; r=1, factor=0.985, width=600, height=400)
    function unit(x, y, r)
        deg = 180:0.25:270
        xs = zeros(length(deg), 2)
        ys = similar(xs)
        for (i, θ) = enumerate(deg)
            xs[i, 1] = cosd(θ)factor + r
            xs[i, 2] = cosd(θ-180)factor
            ys[i, 1] = sind(θ)factor + r
            ys[i, 2] = sind(θ-180)factor
        end
        xs = reshape(xs, :, 1)
        ys = reshape(ys, :, 1)
        xs2 = copy(xs)
        ys2 = copy(ys)
        for θ = 0:90:270 # degree
            for i = 1:length(xs)
                xs2[i], ys2[i] = xs[i]cosd(θ) + ys[i]sind(θ), xs[i]sind(θ) - ys[i]cosd(θ)
            end
            plotpolygon(x .+ xs2, y .+ ys2, lwd=0, col=:palegreen4, fcol=:palegreen4)
        end
    end
    function cherryblossom(x, y, r)
        R = 0.6r
        deg = -asind(r/2R):0.5:asind(r/2R)
        n = floor(Int, 0.9length(deg))
        xs = zeros(n);
        ys = similar(xs);
        for i = 1:n
            xs[i] = cosd(deg[i])R - cosd(asind(r/2R))R
            ys[i] = sind(deg[i])R + 0.5r
        end
        xs = vcat(xs, -reverse(xs))
        ys = vcat(ys, reverse(ys))
        xs[n+1] = 0.5(xs[n] + xs[n+1])
        ys[n+1] = 0.85r
        xs2 = copy(xs)
        ys2 = copy(ys)
        randdeg = rand(0:35)
        for θ = 36+randdeg:72:324+randdeg # degree
            sine, cosine = sincosd(θ)
            for i = 1:length(xs)
                xs2[i], ys2[i] = xs[i]cosine + ys[i]sine, xs[i]sine - ys[i]cosine
            end
            plotpolygon(x .+ xs2, y .+ ys2, col=:pink, fcol=:pink)
        end
        plotcircle(x, y, 0.08r, col=:white, fcol=:white)
    end
    x1, y1, x2, y2 = 0, r, 2nx, 2ny+r
    println("(width, height) = ($(x2 - x1), $(y2 - y1))")
    plotbegin(w=width, h=height)
    plotlimit(x1, y1, x2, y2)
    for y = 1:2ny+1
        for x = 0:2nx
            if y % 2 != x %2
                unit(x, y, r)
                cherryblossom(x, y, 0.4r)
            end
        end
    end
    plotend()
end

@time en5(5, 4, width=600, height=480)
savefig("en5.png")

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

PVアクセスランキング にほんブログ村

PVアクセスランキング にほんブログ村