nbody_display.jl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. function get_planet_pixels(X ::Int64, Y ::Int64)
  2. pix ::Array{Tuple{Int64,Int64}} = []
  3. for i = X-1:X+1
  4. for j = Y-3:Y+3
  5. push!(pix,(i,j))
  6. end
  7. end
  8. for i = Y-2:Y+2
  9. push!(pix,(X-2,i))
  10. push!(pix,(X+2,i))
  11. end
  12. for i = Y-1:Y+1
  13. push!(pix,(X-3,i))
  14. push!(pix,(X+3,i))
  15. end
  16. return pix
  17. end
  18. function is_inside(t ::Tuple{Int64,Int64}, width ::Int64, height ::Int64)
  19. if (t[1] > 0 && t[1] <= width && t[2] > 0 && t[2] <= height)
  20. return true
  21. else
  22. return false
  23. end
  24. end
  25. function graph_planets(pixels ::Array{Array{Int64}}, positions ::Matrix{Float64}, min_value ::Float64, max_value ::Float64, width ::Int64, height ::Int64, file_name ::String)
  26. n = size(positions)[2]
  27. for i = 1:n
  28. X ::Int64= round( ( (positions[1, i] - min_value) / (max_value - min_value) ) * (width - 1) ) + 1
  29. Y ::Int64= round( (1 - ( (positions[2, i] - min_value) / (max_value - min_value) ) ) * (height - 1) ) + 1
  30. pix ::Array{Tuple{Int64,Int64}} = get_planet_pixels(X,Y)
  31. for pixel in pix
  32. if is_inside(pixel, width, height)
  33. pixels[(pixel[2] - 1)*width + pixel[1]] = [125, round(255*i/n)]
  34. end
  35. end
  36. end
  37. open(file_name,"w") do f
  38. write(f, "P3\n$width $height\n255\n")
  39. for he = 1:height
  40. for wi = 1:width
  41. write(f, "$(pixels[(he - 1)*width + wi][1]) 0 $(pixels[(he - 1)*width + wi][2]) ")
  42. end
  43. write(f,"\n")
  44. end
  45. end
  46. end
  47. # function graph_planets(pixels ::Array{Array{Int64}}, positions ::Vector{Vector{Float64}}, min_value ::Float64, max_value ::Float64, width ::Int64, height ::Int64, file_name ::String)
  48. # n = length(positions)
  49. # for i = 1:n
  50. # X ::Int64= round( ( (positions[i][1] - min_value) / (max_value - min_value) ) * (width - 1) ) + 1
  51. # Y ::Int64= round( (1 - ( (positions[i][2] - min_value) / (max_value - min_value) ) ) * (height - 1) ) + 1
  52. # pix ::Array{Tuple{Int64,Int64}} = get_planet_pixels(X,Y)
  53. # for pixel in pix
  54. # if is_inside(pixel, width, height)
  55. # pixels[(pixel[2] - 1)*width + pixel[1]] = [125, round(255*i/n)]
  56. # end
  57. # end
  58. # end
  59. # open(file_name,"w") do f
  60. # write(f, "P3\n$width $height\n255\n")
  61. # for he = 1:height
  62. # for wi = 1:width
  63. # write(f, "$(pixels[(he - 1)*width + wi][1]) 0 $(pixels[(he - 1)*width + wi][2]) ")
  64. # end
  65. # write(f,"\n")
  66. # end
  67. # end
  68. # end