Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why all positions/sizes are in double format?

Basically, I would say that coordinates are "pixel-based". It is easy to understand a pixel at (10,10) or a width of 100 pixels.

But because positions and sizes are in double format, you can have a size of 100.6 and a position at (10.1,50.9). Does the decimal value have an impact on position/size?

In fact, I generate a custom path by calculating points. Some points are nearly equals such as 1.9999999 and 2.000000. At first, I would like to round all points but I am sure of the impact in xaml (1.99 and 2.00, is it the same point ?).

like image 986
Cybermaxs Avatar asked Jan 07 '13 08:01

Cybermaxs


2 Answers

No, WPF positions and size are not pixel based, but "unit" based.

In fact, 1 unit equals 1 pixel when your scren is set to 96 dpi. But if you set up your screen to 120 dpi for example, 1 unit will equals 1,25 pixel.

The direct benefits is that your applications will fit easier than windows forms apps to higher scale resolutions.

Quote from FrameworkElement.Height Property :

This value is interpreted as a device-independent unit (1/96th inch) measurement.

To conclude, I would suggest you to round values. Not for technical reasons, but to make the XAML markup more readable and "natural".

like image 104
Steve B Avatar answered Oct 14 '22 04:10

Steve B


WPF uses vector graphics. See the Vector Graphics section in Visual Rendering Behavior for details. One big advantage of vector graphics is that drawings (as your path for example) can easily be scaled without getting pixelated.

Rounding the points of your Path doesn't make much sense if the calculated point values are directly put into a Path geometry. The coordinates are relative to the origin of the Path object, and the Path (or one of its ancestors, e.g. a Canvas) might itself be located at a fractional pixel position.

If the point values are written to XAML, i agree with Steve that rounding would make the XAML nicer, and you wouldn't realize any difference between 1.99 and 2.

like image 32
Clemens Avatar answered Oct 14 '22 04:10

Clemens