I found by chance that
int a = (h/2)*w+ ( (h+1)/2-h/2 ) * (w+1)/2 ;
is equal to
int b = (w * h + 1) / 2 ;
when w and h are positive integers (assume no overflow).
Can you show me why these 2 are the same?
edit : integer -> positive integer.
Actually this is a math problem: (integer)/2 should be interpreted as floor. So, the problem is:
Proof:
A hint:
floor((2k+1)/2) == k. You can easily show the equivalence.For example, the case 4:
a)
floor(2k+1/2)*(2l+1) + ( floor((2k+2)/2) - floor((2k+1)/2) ) * floor((2l+2)/2) = 2kl+k + (k+1 - k)*(l+1) = 2kl + k + l + 1b)
floor(((2k+1)*(2l+1)+1)/2) = floor((4kl+2k+2l+2)/2) = 2kl + k + l + 1Therefore, the two equations are equivalent.