# julia
using Plots
## задаємо робочу зону
width, height = 2, 1
r = 0.5
c = height/2
## функції для графічного зображення фігур
function circle(h, k, r)
θ = LinRange(0, 2π, 500)
h .+ r .* sin.(θ), k .+ r .* cos.(θ)
end
function square(h, k, a)
ha = a/2
[h-ha, h+ha, h+ha, h-ha, h-ha], [k+ha, k+ha, k-ha, k-ha, k+ha]
end
## ітерації
iters = 1000
points = [Float64[], Float64[]]
inc, ins = 0, 0
pitrials = Float64[]
for _ in 1:iters
push!(pitrials, inc/max(1, ins))
x, y = width*rand(), height*rand()
push!(points[1], x)
push!(points[2], y)
if width-1.5r <= x && x <= width-0.5r && c-0.5r <= y && y <= c+0.5r
ins += 1
elseif (x-r)^2 + (y-c)^2 <= r^2
inc += 1
end
end
## намалювати середовище
table = scatter(points..., legend=false)
plot!(table, circle(r, c, r), legend=false, lw=2, color="black")
plot!(table, square(width-r, c, r), lw=2, color="black")
## намалювати графік наближення
piplot = plot(pitrials, label="наближення")
hline!(piplot,[π], label="π")