<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: knuth code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Tue, 07 Oct 2008 10:24:15 GMT</pubDate>
    <description>DZone Snippets: knuth code</description>
    <item>
      <title>Patchwork quilt pattern in Ruby (from Knuth TAoCP 4 - 7.1.3)</title>
      <link>http://snippets.dzone.com/posts/show/5181</link>
      <description>In Donald Knuth's TAoCP Pre-Fascicle 1a: Bitwise Tricks and Techniques, he shows a patchwork quilt defined by f(x, y) = ((x ^ y) &amp; ((y - 350) &gt;&gt; 3)) ** 2, designed by D. Sleator in 1976. I wanted to recreate this quilt myself using Ruby. The following program generates a PNG file of the "quilt."&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'rubygems'&lt;br /&gt;require 'png'&lt;br /&gt;&lt;br /&gt;def f(x, y)&lt;br /&gt;  ( (x ^ y) &amp; ((y - 350) &gt;&gt; 3) ) ** 2&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;canvas = PNG::Canvas.new(500, 500)&lt;br /&gt;&lt;br /&gt;0.upto(499) do |y|&lt;br /&gt;  0.upto(499) do |x|&lt;br /&gt;    canvas[x, 499 - y] = ((f(x, y) &gt;&gt; 12) &amp; 1) == 1 ? PNG::Color::Black : PNG::Color::White&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;png = PNG.new(canvas)&lt;br /&gt;png.save 'pattern.png'&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 27 Feb 2008 10:17:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5181</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
  </channel>
</rss>
