Silverlight Tip of the Day #103 – Use Integers for Layout Calculations
When calculating the positions of objects such as Images make certain to use integer values not floating point values.
For example, take a look at this code that centers a map around a given position:
private void CenterMap(double windowWidth, double windowHeight)
{ if (null != _parentCanvas)
{ int leftPos = (int) ((windowWidth / 2) - _currentX);
int topPos = (int) ((windowHeight / 2) - _currentY);
_parentCanvas.SetValue(Canvas.LeftProperty, (double) leftPos);
_parentCanvas.SetValue(Canvas.TopProperty, (double)topPos);
}
}
Notice I converted the map layout position left and top to be integers. The following screen shot shows an example where double values were used instead of integers. The result is the images are slightly blurred and lines (or seams) appear between the image tiles.
Compare the screenshot above that used doubles with the shot below that used integers. In the screen shot below the images are crisp and the terrain tiles have no seams or lines.
Thank you,
--Mike Snow
Subscribe in a reader