Advertisement
Guest User

Untitled

a guest
May 31st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 4.00 KB | None | 0 0
  1. using StatsBase
  2. using Plots
  3. using ProfileView
  4.  
  5. const Iterations = 100000
  6. const vec_len = 4
  7.  
  8.    
  9. #black_sq = eye(Int64, NumColumns, NumColumns)
  10.  
  11.  
  12. #=
  13. black_sq = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  14.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  15.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  16.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  17.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  18.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  19.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  20.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  21.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  22.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  23.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  24.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  25.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  26.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  27.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  28.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  29.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  30.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  31.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  32.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  33.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]=#
  34.  
  35. black_sq = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  36.             1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1;
  37.             1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1;
  38.             1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1;
  39.             1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1;
  40.             1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1;
  41.             1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1;
  42.             1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1;
  43.             1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1;
  44.             1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1;
  45.             1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1;
  46.             1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1;
  47.             1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1;
  48.             1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1;
  49.             1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1;
  50.             1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1;
  51.             1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1;
  52.             1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1;
  53.             1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1;
  54.             1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1;
  55.             0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  56.  
  57. const NumColumns = size(black_sq)[1] #Int64((stop - start) / step + 1)
  58. const col_max = NumColumns - vec_len
  59.  
  60. # Bresenham's line algorithm
  61. function b_line(x1::Int, y1::Int, x2::Int, y2::Int)
  62.     coords = Array{CartesianIndex{2}}(0)
  63.     @fastmath dx = Int(abs(x2-x1))
  64.     @fastmath dy = Int(abs(y2-y1))
  65.  
  66.     sx::Int = x1<x2 ? 1 : -1
  67.     sy::Int = y1<y2 ? 1 : -1;
  68.  
  69.     err = (dx>dy ? dx : -dy)/2
  70.  
  71.     @fastmath while true
  72.         push!(coords, CartesianIndex(x1, y1))
  73.        
  74.         if (x1==x2 && y1==y2);
  75.             return coords
  76.         end
  77.        
  78.         err2 = err;
  79.         if err2 > -dx
  80.             err -= dy
  81.             x1 += sx
  82.         end
  83.        
  84.         if err2 < dy
  85.             err += dx
  86.             y1 += sy
  87.         end
  88.     end
  89. end
  90.  
  91. function gen_heatmap(black_map::Array{Int}, col::Int, iter::Int)
  92.     counters = zeros(Int, col, col)
  93.  
  94.     for i in 1:iter
  95.         #rand_line = b_line(rand(1:col), rand(1:col), rand(1:col), rand(1:col))
  96.         @fastmath x1, y1 = rand(vec_len:col_max), rand(vec_len:col_max)
  97.         @fastmath rand_line = b_line(x1, y1, rand((x1-vec_len+1):(x1+vec_len)), rand((y1-vec_len+1):(y1+vec_len)))
  98.         @fastmath if countnz(black_map[rand_line]) == 0
  99.             @inbounds counters[rand_line] += 1
  100.         end
  101.     end
  102.     return counters
  103. end
  104.  
  105. gen_heatmap(black_sq, NumColumns, Iterations)
  106. Profile.clear()
  107. @profile @time  gen_heatmap(black_sq, NumColumns, Iterations)
  108. ProfileView.view()
  109.  
  110. #heatmap(gen_heatmap(black_sq, NumColumns, Iterations))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement