Concerns about this recursive algorithm

good morning, afternoon or evening.
i was working with a for loop today and something seems to have gone very wrong as this is supposed to create a much larger file than it has

nx = 200
ny = 100
touch("image.PPM")
open("image.PPM", "w") do image  
            write(image,"P3\n" , string(nx) , " " , string(ny) , "\n" , string(255) , "\n")
            for i in ny
                        for j in nx
                                    r = i / ny
                                    g = j / nx
                                    b = 0.2
                                    ir = string(Integer(round(255.99*r)))
                                    write(image, ir , " ")
                                    ig = string(Integer(round(255.99*g)))
                                    write(image,ig , " ")
                                    ib = string(Integer(round(255.99*b)))
                                    write(image,ib , " ")
                                    write(image, "\n")
                                    
                        end
            end
end

this should output a ppm file of what is at the start of ray tracing in a weekend but i have not found any results other than a yellow pixel.

nx is a number; for j in nx iterates once over that number, since numbers in Julia are iterable. You’re looking for for j in 1:nx instead.

See also

More people looping over integers instead of ranges:

1 Like