Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using StatsBase
- using Plots
- using ProfileView
- const Iterations = 100000
- const vec_len = 4
- #black_sq = eye(Int64, NumColumns, NumColumns)
- #=
- black_sq = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
- 0 0 0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
- const col_max = NumColumns - vec_len
- # Bresenham's line algorithm
- function b_line(x1::Int, y1::Int, x2::Int, y2::Int)
- coords = Array{CartesianIndex{2}}(0)
- @fastmath dx = Int(abs(x2-x1))
- @fastmath dy = Int(abs(y2-y1))
- sx::Int = x1<x2 ? 1 : -1
- sy::Int = y1<y2 ? 1 : -1;
- err = (dx>dy ? dx : -dy)/2
- @fastmath while true
- push!(coords, CartesianIndex(x1, y1))
- if (x1==x2 && y1==y2);
- return coords
- end
- err2 = err;
- if err2 > -dx
- err -= dy
- x1 += sx
- end
- if err2 < dy
- err += dx
- y1 += sy
- end
- end
- end
- function gen_heatmap(black_map::Array{Int}, col::Int, iter::Int)
- counters = zeros(Int, col, col)
- for i in 1:iter
- #rand_line = b_line(rand(1:col), rand(1:col), rand(1:col), rand(1:col))
- @fastmath x1, y1 = rand(vec_len:col_max), rand(vec_len:col_max)
- @fastmath rand_line = b_line(x1, y1, rand((x1-vec_len+1):(x1+vec_len)), rand((y1-vec_len+1):(y1+vec_len)))
- @fastmath if countnz(black_map[rand_line]) == 0
- @inbounds counters[rand_line] += 1
- end
- end
- return counters
- end
- gen_heatmap(black_sq, NumColumns, Iterations)
- Profile.clear()
- @profile @time gen_heatmap(black_sq, NumColumns, Iterations)
- ProfileView.view()
- #heatmap(gen_heatmap(black_sq, NumColumns, Iterations))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement