Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Peter Cooperx http://www.petercooper.co.uk/

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Patchwork quilt pattern in Ruby (from Knuth TAoCP 4 - 7.1.3)

In Donald Knuth's TAoCP Pre-Fascicle 1a: Bitwise Tricks and Techniques, he shows a patchwork quilt defined by f(x, y) = ((x ^ y) & ((y - 350) >> 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."

   1  
   2  require 'rubygems'
   3  require 'png'
   4  
   5  def f(x, y)
   6    ( (x ^ y) & ((y - 350) >> 3) ) ** 2
   7  end
   8  
   9  canvas = PNG::Canvas.new(500, 500)
  10  
  11  0.upto(499) do |y|
  12    0.upto(499) do |x|
  13      canvas[x, 499 - y] = ((f(x, y) >> 12) & 1) == 1 ? PNG::Color::Black : PNG::Color::White
  14    end
  15  end
  16  
  17  png = PNG.new(canvas)
  18  png.save 'pattern.png'

My default layout in Rails projects

Writing a new layout from scratch gets boring, so I have a generic / default layout to use and extend from.

   1  
   2  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/ DTD/xhtml1-transitional.dtd">
   3  <html xmlns="http://www.w3.org/1999/xhtml">
   4  <head>
   5  	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   6  	<title><%= @page_title %></title>
   7  	<%= stylesheet_link_tag "main" %>
   8  	<%= javascript_include_tag "prototype" %>
   9  	<%= javascript_include_tag "scriptaculous" %>
  10  	<%= javascript_include_tag "general" %>
  11  	<!--[if lt IE 7.]>
  12  	<script defer type="text/javascript" src="/javascripts/pngfix.js"></script>
  13  	<![endif]-->
  14  </head>
  15  
  16  <body class="<%= @body_class %>">
  17  	<div id="container">
  18  			
  19  	<%= @content_for_layout %>
  20  
  21  	</div>
  22  </body>
  23  </html>


pngfix.js is available here.

Change OS X's default screenshot image format

At the prompt, type this:

   1  defaults write com.apple.screencapture type image_format


Replace "image_format" with a file format name, like pdf, png, tiff, etc. Then to take screenshots, Cmd+Shift+4, then drag around the area you want to capture.
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS