Retrogamecoding(.org) > EGSL

int()

(1/1)

Peter:
Hi Cyber,

There is a bug with INTstatement. There is a pixel difference of 16 pixeln!
Try the small demo in order to see what I  mean.

If you click on the red wall, you will put another wall on it.
This happens with a difference of 16 pixel!
Look also at  the (x/y position) and (mousex/mousey position). Move the mouse slow.

--- Code: ---function Sprite(id,x,y,w,h,fx)
   drawimage(w*fx,0,w,h,x,y,id)
end

function SetText(x,y,txt,r,g,b)
   color(r,g,b)
   drawtext(x,y,txt)
end

screen(320,240,0,"Ohh")
setframetimer(60)
backcolor(255,255,255)

x,y,z,b,s = 0,0,0,0,0
b=loadimage("png/brick1.png")
s=loadimage("png/xsteel.png")

repeat
key=getkey()
cls()
x= int(mousex()/32)
y= int(mousey()/32)
for z=0,10 do
   Sprite(b,z*32,32,32,32,0)
end
if mouseb()==1 and y==1 then
   Sprite(s,x*32,y*32,32,32,1)
end
SetText(0, 0,"x= "..x.."  mousex "..mousex(),0,0,0)
SetText(0,12,"y= "..y.."  mousey "..mousey(),0,0,0)
sync()
until key==27
closewindow()

--- End code ---

Cybermonkey:
Hm, seems you're right. Since I am using the round function of Pascal, there seems to be the fault.
Anyway - as a workaround - you can use the lua built-in functions:

--- Code: ---x= math.floor(mousex()/32)
y= math.floor(mousey()/32)

--- End code ---
Then it seems to work correctly.

Or you change the calculation, because int() rounds mathematically correct which means 2.6 = 3 and 2.4 = 2 etc. I could change it to trunc() which means 2.1 ... 2.9 = 2 but is this correct? How does other int() functions work in other BASICs?
See this code for clarification:

--- Code: ---screen (800,600,0,"Integer-Test")
drawtext (0,0,int (2.6))
drawtext (0,10,math.floor (2.6))
drawtext (0,20,math.ceil (2.6))

drawtext (0,40,int (2.4))
drawtext (0,50,math.floor (2.4))
drawtext (0,60,math.ceil (2.4))

drawtext (0,80,int (2.5))
drawtext (0,90,math.floor (2.5))
drawtext (0,100,math.ceil (2.5))

sync()
inkey()
closewindow()
--- End code ---
Result:

--- Quote ---3
2
3

2
2
3

2
2
3

--- End quote ---
For an example you can also have a look at the multix.lua in the examples download.

Tomaaz:
I think mostly int(x) return the largest integer smaller or equal to x (like math.floor() in Lua), while round(x) returns the nearest integer.

Examples:


--- Code: ---int(5.2) => 5
round(5.2) => 5

int(5.8) => 5
round(5.8) => 6

int(-5.2) => -6
round(-5.2) => -5
--- End code ---

round(x) wouldn't be the same as math.ceil() in Lua, as depends on situation, it can return both smaller or larger integer.

Tomaaz:
But it is possible to make int() return an integer part of the number,

--- Code: ---int(5.2) => 5
int(-5.2) => -5 (not -6)

--- End code ---

so there would be four slightly different functions: int(), floor(), ceil() and round(). Not a bad idea IMHO.

Peter:
math.floor is good,  in my case here.
You can try  a  game,  section examples.

Navigation

[0] Message Index

Go to full version