Log In  

The code below should draw 10 squares, there is only one variable that define the size (b.s in _init), but sometimes some squares appear with a width different of the height. I don't know if its a bug, or I'm using rectifll in the wrong way.

blocks={}

function _init()
    for x=1,10 do
        local b={}
        b.x=rnd(100)
        b.y=rnd(100)
        b.s=rnd(15)
        add(blocks,b)
    end
end

function _draw()
    cls()
    for b in all(blocks) do
        rectfill(b.x,b.y,b.x+b.s,b.y+b.s,4)
    end 
end
P#20958 2016-05-20 21:39 ( Edited 2016-05-21 03:11)

The rnd() function returns floating point values (actually 16:16 fixed point) so when it comes time to render, the rectfill () function will round to the closest integer pixel coordinate. That's why you'll occasionally see rectangles with width and height differing by one pixel.

I'd recommend wrapping the assignments to b.x, b.y, and b.s in with flr() to force them to integer values.

P#20959 2016-05-20 21:48 ( Edited 2016-05-21 01:49)

The rounding is still something I'm getting used to too. I had to make a couple methods to help with randomizing and rounding.

b.x=flr(rnd(100))

Forum tip: Try include the nature of your question in the thread subject rather than just "help" - it just makes it easier for people to find things later.

P#20964 2016-05-20 22:25 ( Edited 2016-05-21 02:25)

thanks scgrn, that worked. With a small resolution these things become very noticeable.

morningtoast, you are right, I renamed with a better title.

P#20969 2016-05-20 23:11 ( Edited 2016-05-21 03:11)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 06:40:33 | 0.006s | Q:12