<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Cyril Wei&#039;s Blog</title>
	<atom:link href="http://cyrilwei.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://cyrilwei.wordpress.com</link>
	<description>Strive for perfection in everything you do. Take the best that exists and make it better. When it does not exist, design it. -- Sir Henry Royce.</description>
	<lastBuildDate>Tue, 06 Sep 2011 12:28:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='cyrilwei.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Cyril Wei&#039;s Blog</title>
		<link>http://cyrilwei.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://cyrilwei.wordpress.com/osd.xml" title="Cyril Wei&#039;s Blog" />
	<atom:link rel='hub' href='http://cyrilwei.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Move to new blog</title>
		<link>http://cyrilwei.wordpress.com/2011/02/23/move-to-new-blog/</link>
		<comments>http://cyrilwei.wordpress.com/2011/02/23/move-to-new-blog/#comments</comments>
		<pubDate>Wed, 23 Feb 2011 01:57:04 +0000</pubDate>
		<dc:creator>Cyril Wei</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cyrilwei.wordpress.com/?p=77</guid>
		<description><![CDATA[I&#8217;ve moved to new blog http://cyrilwei.me/blog<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cyrilwei.wordpress.com&amp;blog=14689083&amp;post=77&amp;subd=cyrilwei&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve moved to new blog</p>
<p><a href="http://cyrilwei.me/blog">http://cyrilwei.me/blog</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cyrilwei.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cyrilwei.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/cyrilwei.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/cyrilwei.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/cyrilwei.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/cyrilwei.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/cyrilwei.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/cyrilwei.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/cyrilwei.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/cyrilwei.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/cyrilwei.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/cyrilwei.wordpress.com/77/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/cyrilwei.wordpress.com/77/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/cyrilwei.wordpress.com/77/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cyrilwei.wordpress.com&amp;blog=14689083&amp;post=77&amp;subd=cyrilwei&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cyrilwei.wordpress.com/2011/02/23/move-to-new-blog/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8f6e9631156bfddd28bc08cccaada599?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cyrilwei</media:title>
		</media:content>
	</item>
		<item>
		<title>Quartz 2D Programming for iPhone 4 Series: Drawing at High-Resolution</title>
		<link>http://cyrilwei.wordpress.com/2010/09/26/quartz-2d-programming-for-iphone-4-series-drawing-at-high-resolution/</link>
		<comments>http://cyrilwei.wordpress.com/2010/09/26/quartz-2d-programming-for-iphone-4-series-drawing-at-high-resolution/#comments</comments>
		<pubDate>Sun, 26 Sep 2010 14:13:57 +0000</pubDate>
		<dc:creator>Cyril Wei</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[Quartz]]></category>

		<guid isPermaLink="false">http://cyrilwei.wordpress.com/?p=61</guid>
		<description><![CDATA[The iPhone 4 has a really high-resolution screen which’s got 960 by 640 pixels. So the apps now need to be prepared to run on different resolutions. Although the apple said that most of the work was done by the &#8230; <a href="http://cyrilwei.wordpress.com/2010/09/26/quartz-2d-programming-for-iphone-4-series-drawing-at-high-resolution/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cyrilwei.wordpress.com&amp;blog=14689083&amp;post=61&amp;subd=cyrilwei&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The iPhone 4 has a really high-resolution screen which’s got 960 by 640 pixels. So the apps now need to be prepared to run on different resolutions. Although the apple said that most of the work was done by the system framework, your application still needs to do some other works.</p>
<p><span id="more-61"></span>The UIKit will render texts and standard views on the high-resolution correctly, and if the app provides the @2x files, the UIKit will display the high-resolution images too.</p>
<p>Instead of pixels, the Quartz and UIKit use <strong>points</strong> to measure distances. One point does not necessarily correspond to one pixel on the screen. In iOS 4, the UIScreen, UIView, UIImage and CALayer expose a scale factor that tells you the relationship between points and pixels for that particular object. This factor is set to 2.0 on iPhone 4, so the apps which think the screen as 480 by 320 points would work well.</p>
<p>But the coordinate system of iOS may make some troubles.</p>
<p>The origin in Mac OS X is located at the lower-left corner, but the coordinate system of iOS is different. Its origin is located at the upper-left. However, if you want to draw some bitmaps or PDFs on iOS, the origin will be located at the lower-left, just like the Mac OS X coordinate system. So, the only one different is the device coordinate system of iOS.</p>
<p>Actually, the default origin of iOS is lower-left too. If you use <code>CGContextGetCTM(context)</code> to get the default transformation matrix on the <code>drawRect:</code> method of your views, you will find that it’s not an identity matrix, which means the iOS has already done some translation on the coordinate system. Maybe this is for someone to port games from other platforms.</p>
<p>To make the custom drawing code more general, or just because the developers are more familiar with the lower-left corner, some of them would like to ‘reset’ the coordinate system by asking Quartz 2D to apply an inverse matrix to the current transformation. Like this.</p>
<p><pre class="brush: objc;">
CGAffineTransform t0 = CGContextGetCTM(context);
t0 = CGAffineTransformInvert(t0);
CGContextConcatCTM(context,t0);
</pre></p>
<p>Then the transformation will be reset, the context’s transformation matrix will be identity and the origins of everything are located at the lower-left now.</p>
<p>That sounds good. Everything is reset, including the scale transformation.</p>
<p>Before the iPhone 4, the default scale transformation is 1, which is harmless. But things changed. To avoid the custom drawing code in old apps making mistakes on iPhone 4, the iOS sets a scale transform whose value is 2 on the default transform matrix for the <code>drawRect:</code> method of your views. The code will reset it to 1, which means that all the custom drawing will be displayed in the lower-left quarter of the screen.</p>
<p>Like this.</p>
<p><a href="http://cyrilwei.files.wordpress.com/2010/09/quarter.png"><img class="aligncenter size-full wp-image-66" title="Quarter" src="http://cyrilwei.files.wordpress.com/2010/09/quarter.png?w=640" alt=""   /></a>To fix this bug, we can use the scale property of UIScreen which is 2 on iPhone 4 and 1 on other iPhones and maybe something else on some further devices.</p>
<p>Add 2 lines after the reset code.</p>
<p><pre class="brush: objc;">
CGAffineTransform t0 = CGContextGetCTM(context);
t0 = CGAffineTransformInvert(t0);
CGContextConcatCTM(context,t0);

CGFloat scale = [UIScreen mainScreen].scale;
CGContextScaleCTM(context, scale, scale);
</pre></p>
<p>This will reset the context to 480 by 320 points. Then all the custom drawing will be enlarged 2 times to fit the new screen’s resolution, but which means that the custom drawing cannot benefit too much from the high-resolution.</p>
<p><a href="http://cyrilwei.files.wordpress.com/2010/09/full.png"><img class="aligncenter size-full wp-image-65" title="Full" src="http://cyrilwei.files.wordpress.com/2010/09/full.png?w=640" alt=""   /></a></p>
<p>Actually, we want to use the high-resolution screen to get more beautiful pictures. So the custom drawing code should use the 960 by 640 screen sufficiently.</p>
<p>We can use the reset context directly, for it has a 960 by 640 points canvas. If you don’t reset the coordinate system, you can use the following code to set the scale of transformation to 1. Then we have 960 by 640 points too.</p>
<p><pre class="brush: objc;">
CGFloat inverseScale = (CGFloat)1.0/[UIScreen mainScreen].scale;
CGContextScaleCTM(context, inverseScale, inverseScale);
</pre></p>
<p>The next step to do is to modify the custom drawing code to use the more points. We could only multiply the position of drawing codes by 2, but maybe some lines will be too thin on the high-resolution screen.</p>
<p>A better way is to provide 2 different codes for different resolutions. Just like the images, we can provide an ‘@2x’ drawing code. When finding it the iPhone 4, the app will draw with the optimized code for high-resolution screen.</p>
<p>The best solution depends on the needs of the application. We should test some approaches before we choose.</p>
<p>For further information please refer to the iOS Application Programming Guide. There is a chapter discussing supporting high-resolution screens.</p>
<p>* The pictures was taken from <em>Asteroids</em> - a sample game from <em>Beginning iPhone Games Development</em>, which is a wonderful book for iPhone game developer.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cyrilwei.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cyrilwei.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/cyrilwei.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/cyrilwei.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/cyrilwei.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/cyrilwei.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/cyrilwei.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/cyrilwei.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/cyrilwei.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/cyrilwei.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/cyrilwei.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/cyrilwei.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/cyrilwei.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/cyrilwei.wordpress.com/61/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cyrilwei.wordpress.com&amp;blog=14689083&amp;post=61&amp;subd=cyrilwei&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cyrilwei.wordpress.com/2010/09/26/quartz-2d-programming-for-iphone-4-series-drawing-at-high-resolution/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8f6e9631156bfddd28bc08cccaada599?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cyrilwei</media:title>
		</media:content>

		<media:content url="http://cyrilwei.files.wordpress.com/2010/09/quarter.png" medium="image">
			<media:title type="html">Quarter</media:title>
		</media:content>

		<media:content url="http://cyrilwei.files.wordpress.com/2010/09/full.png" medium="image">
			<media:title type="html">Full</media:title>
		</media:content>
	</item>
		<item>
		<title>The BigDecimal is not predictable too. A Trap?</title>
		<link>http://cyrilwei.wordpress.com/2010/08/31/the-bigdecimal-is-not-predictable-too-a-trap/</link>
		<comments>http://cyrilwei.wordpress.com/2010/08/31/the-bigdecimal-is-not-predictable-too-a-trap/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 07:25:59 +0000</pubDate>
		<dc:creator>Cyril Wei</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://cyrilwei.wordpress.com/?p=30</guid>
		<description><![CDATA[Hi, have you used the BigDecimal class in java? And do you know that the BigDecimal is as unpredictable as double? Recently, I failed in a test case which I thought would have never failed because I just returned the &#8230; <a href="http://cyrilwei.wordpress.com/2010/08/31/the-bigdecimal-is-not-predictable-too-a-trap/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cyrilwei.wordpress.com&amp;blog=14689083&amp;post=30&amp;subd=cyrilwei&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hi, have you used the <code>BigDecimal</code> class in java? And do you know that the <code>BigDecimal</code> is as unpredictable as <code>double</code>?</p>
<p>Recently, I failed in a test case which I thought would have never failed because I just returned the parameter in the method&#8230;ok, well, i converted it. So, what do you think the output is</p>
<p><pre class="brush: java;">
BigDecimal a = new BigDecimal(1.16);
BigDecimal b = new BigDecimal(100);
System.out.println(a.multiply(b).longValue());
</pre></p>
<p><span id="more-30"></span>It’s 115! Not the expected 116.</p>
<p>Oh, well, what’s wrong with it? It’s the precise, predictable and trustworthy <code>BigDecimal</code>. Don’t do that <code>double</code> things.</p>
<p>Well, <code>double</code>, yes, what’s the result of <code>double</code>?</p>
<p><pre class="brush: java;">System.out.println((long)(1.16 * 100));</pre></p>
<p>Bingo! 115 too. So, maybe the <code>BigDecimal</code> translates the exact <code>double</code> value, with the unpredictable behaviors of <code>double</code>.</p>
<p><pre class="brush: java;">System.out.println(new BigDecimal(1.16));</pre></p>
<p>The output is 1.1599999999999999200639422269887290894985198974609375. Yes, perhaps it’s the precise binary value of 1.16 in <code>double</code>.</p>
<p>To find the reason, I opened the Java SDK documents and, oops, there are some notes of the <code>BigDecimal(double val)</code> constructor.</p>
<blockquote><p>Notes:</p>
<ol>
<li>The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed <em>in</em> to the constructor is not exactly equal to 0.1, appearances notwithstanding.</li>
<li>The String constructor, on the other hand, is perfectly predictable: writing new BigDecimal(&#8220;0.1&#8243;) creates a BigDecimal which is <em>exactly</em> equal to 0.1, as one would expect. Therefore, it is generally recommended that the <a href="http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html#BigDecimal(java.lang.String)">String constructor</a> be used in preference to this one.</li>
<li>When a double must be used as a source for a BigDecimal, note that this constructor provides an exact conversion; it does not give the same result as converting the double to a String using the <a href="http://download.oracle.com/javase/6/docs/api/java/lang/Double.html#toString(double)">Double.toString(double)</a> method and then using the <a href="http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html#BigDecimal(java.lang.String)">BigDecimal(String)</a> constructor. To get that result, use the static <a href="http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html#valueOf(double)">valueOf(double)</a> method.</li>
</ol>
</blockquote>
<p>Ok, yes, that’s right. Well, then, why haven’t anybody told me this?</p>
<p>So the <code>BigDecimal</code> is trustworthy still, but don’t use the untrustworthy <code>double</code> any more. Use the <code>String</code> constructor, they recommended that.</p>
<p><pre class="brush: java;">
BigDecimal a = new BigDecimal(&quot;1.16&quot;);
BigDecimal b = new BigDecimal(&quot;100&quot;);
System.out.println(a.multiply(b).longValue());
System.out.println(a);
</pre></p>
<p>Very well, 116 and 1.16. But will you use <code>String</code> as number type in you applications? If you can’t, follow the 3rd note. The static method <code>valueOf(double)</code> will first convert the <code>double</code> to <code>String</code> and then use the <code>String</code> constructor.</p>
<p>Like this</p>
<p><pre class="brush: java;">
BigDecimal a = BigDecimal.valueOf(1.16);
BigDecimal b = BigDecimal.valueOf(100);
System.out.println(a.multiply(b).longValue());
System.out.println(a);
</pre></p>
<p>Output 116 and 1.16 too.</p>
<p>Ok, I chose <code>String</code> in my application because my data is money. I won’t use <code>double</code> type to store money data forever.</p>
<p><strong>Questions:</strong><br />
1. Why haven’t mark a <code>'deprecated'</code> on the <code>BigDecimal(double val)</code> constructor?<br />
2. What does the <code>double.toString(double d)</code> do to make the value reasonable?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cyrilwei.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cyrilwei.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/cyrilwei.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/cyrilwei.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/cyrilwei.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/cyrilwei.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/cyrilwei.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/cyrilwei.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/cyrilwei.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/cyrilwei.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/cyrilwei.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/cyrilwei.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/cyrilwei.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/cyrilwei.wordpress.com/30/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cyrilwei.wordpress.com&amp;blog=14689083&amp;post=30&amp;subd=cyrilwei&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cyrilwei.wordpress.com/2010/08/31/the-bigdecimal-is-not-predictable-too-a-trap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8f6e9631156bfddd28bc08cccaada599?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cyrilwei</media:title>
		</media:content>
	</item>
	</channel>
</rss>
