<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rounding &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/rounding/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Thu, 16 Feb 2017 15:16:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Fractional Scale Factors</title>
		<link>https://blog.xojo.com/2017/02/15/fractional-scale-factors/</link>
		
		<dc:creator><![CDATA[William Yu]]></dc:creator>
		<pubDate>Wed, 15 Feb 2017 06:00:27 +0000</pubDate>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Anti-Alias]]></category>
		<category><![CDATA[Fractional]]></category>
		<category><![CDATA[Rounding]]></category>
		<category><![CDATA[Scale]]></category>
		<guid isPermaLink="false">http://blog.xojo.com/?p=2296</guid>

					<description><![CDATA[With all the Retina/HiDPI work done in the past few years, we&#8217;ve had to add some new features along with it. One of these newer&#8230;]]></description>
										<content:encoded><![CDATA[<p>With all the Retina/HiDPI work done in the past few years, we&#8217;ve had to add some new features along with it. One of these newer features is the Graphics ScaleX and ScaleY properties.  For the purposes of Retina/HiDPI, the scale factor is used when converting user space coordinates to backing store coordinates.  While mostly integral on MacOS (unless originating from some code that&#8217;s probably not ours), it can vary on Windows, and perhaps arguably mostly fractional.  This is because Windows allows you to set DPI scales at 125%, 150%, etc. So when dealing with fractional scales there are a few things to watch out for:</p>
<ol>
<li>Rounding issues</li>
<li>Anti-alias effect</li>
</ol>
<p>While the framework takes care of rounding issues, for the most part, the secondary issue of anti-aliasing is up to you.</p>
<p><span id="more-2296"></span></p>
<p>Here&#8217;s an example to demonstrate the issue:</p>
<pre><span style="color: red">// In this simple example the user wanted to tile their UI for some reason</span>
<span style="color: blue">Sub</span> Window1.Paint(g <span style="color: blue">As</span> Graphics, areas() <span style="color: blue">As</span> REALbasic.Rect)
  <span style="color: red">// For simplicity the tile will just be black, 15px tall</span>
  <span style="color: blue">Dim</span> oneTile <span style="color: blue">As New</span> Picture(Self.Width, 15, 32)
  oneTile.Graphics.FillRect(0, 0, oneTile.Width, oneTile.Height)

  <span style="color: red">// For this demo we'll create a new picture
  // whose Scale factor is set at 1.5x</span>
  <span style="color: blue">Dim</span> resultPic <span style="color: blue">As New</span> Picture(Self.Width, Self.Height, 32)
  <span style="color: blue">Dim</span> resultGfx <span style="color: blue">As</span> Graphics = resultPic.Graphics
  resultGfx.ScaleX = 1.5
  resultGfx.ScaleY = 1.5

  <span style="color: red">// Now tile the picture so the result should
  // be completely black</span>
  <span style="color: blue">For</span> y <span style="color: blue">As Integer</span> = 0 <span style="color: blue">To</span> resultPic.Height <span style="color: blue">Step</span> oneTile.Height
    resultGfx.DrawPicture(oneTile, 0, y)
  <span style="color: blue">Next </span>

  g.DrawPicture(resultPic, 0, 0)
<span style="color: blue">End Sub</span></pre>
<p>Now here&#8217;s the result, from the above code, with Anti-aliasing enabled (that&#8217;s the default for our Graphics object):</p>
<p><img fetchpriority="high" decoding="async" class="size-full wp-image-2306 aligncenter" src="https://blog.xojo.com/wp-content/uploads/2017/02/TiledWithAntiAlias.png" alt="" width="600" height="422" /></p>
<p>Probably not what the user wanted. This is because with Anti-alias enabled the drawing functions smooth the edges and makes fractional scale that much more obvious. For example, drawing the tile at y = 15 means the pixel location (after adjusting for scale) at 15 * 1.5 = 22.5. While there doesn&#8217;t exist a half pixel, the drawing function instead aliases that portion. If this is not what you wanted, and in this case it&#8217;s not, you must manually disable Anti-aliasing.</p>
<p>Let&#8217;s run that example again, but this time with Anti-alias disabled:</p>
<pre> <span style="color: blue">Dim</span> resultGfx <span style="color: blue">As</span> Graphics = resultPic.Graphics
 resultGfx.AntiAlias = <span style="color: blue">False</span>
 resultGfx.ScaleX = 1.5
 resultGfx.ScaleY = 1.5</pre>
<p>Now we get output more in line with our expectations:<img decoding="async" class="wp-image-2307 aligncenter" src="https://blog.xojo.com/wp-content/uploads/2017/02/TiledWithoutAntiAlias.png" alt="" width="584" height="438" /><br />
Alternatively, we could have sized our tile to be 10px high and thus avoid any rounding issues (in this particular case anyway). While there could be many different ways to solve a particular problem, recognizing the issue will help you find that answer even quicker. Now there&#8217;s no excuse for not making your app <strong><em>fractionally</em> </strong>great!</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
