Rounding functions:
We will start off by calculating the distance to the pixel up left. ((2,2) in this case)
To to that we need to 'remove' the decimals from our X and Y, and we'll get (2,2) instead of (2.6,2.8)
There is no built in function in Visual Basic that does that so we'll have to make our own.
First we have to correct Visual Basic's Round function that will not always return the correct value,
so here's Round2
VB Code:
Private Function Round2(Number As Double, Optional NumDigitsAfterDecimal As Long = 0) As Double If Number - Round(Number, NumDigitsAfterDecimal) >= 0.5 / (10 ^ NumDigitsAfterDecimal) Then Round2 = Round(Number, NumDigitsAfterDecimal) + 0.1 / (10 ^ NumDigitsAfterDecimal) If Number - Round(Number, NumDigitsAfterDecimal) < 0.5 / (10 ^ NumDigitsAfterDecimal) Then Round2 = Round(Number, NumDigitsAfterDecimal) End Function
Now what this function do is it takes your number and checks if the decimal value is equal,
higher or lower than 0.5 and then rounds it off in the correct direction.
Now we head on to the type of round function we were after - A function that 'removes' the decimals
so we can get the upper left pixel.
VB Code:
Private Function RoundClose(Number As Double) As Integer If Number = Round2(Number) Then RoundClose = Number Else If Number - Round2(Number) > 0 Then RoundClose = Round2(Number) Else RoundClose = Round2(Number) - 1 End If End If End Function
This function uses the Round2 function we made earlier to round off the decimals and if the number
gets rounded up to the next integer we simply subtract one from it.




Reply With Quote