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

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 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."

require 'rubygems'
require 'png'

def f(x, y)
  ( (x ^ y) & ((y - 350) >> 3) ) ** 2
end

canvas = PNG::Canvas.new(500, 500)

0.upto(499) do |y|
  0.upto(499) do |x|
    canvas[x, 499 - y] = ((f(x, y) >> 12) & 1) == 1 ? PNG::Color::Black : PNG::Color::White
  end
end

png = PNG.new(canvas)
png.save 'pattern.png'

Java - JFreeChart Example

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

public class jfcExample extends JFrame
{
	private static final long serialVersionUID = 1L;
	
	private DefaultPieDataset dataset;
	private JFreeChart jfc;

	public jfcExample()
	{
		dataset = new DefaultPieDataset();
	}
	
	public void setValue(String title, Double numDouble)
	{
		dataset.setValue(title, numDouble);
	}
	
	public void setChar(String title)
	{
		jfc = ChartFactory.createPieChart(title, dataset, true, true, false);
		
		PiePlot pp = (PiePlot) jfc.getPlot();
		pp.setSectionOutlinesVisible(false);
		pp.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
		pp.setNoDataMessage("Nessun Dato Inserito");
		pp.setCircular(false);
		pp.setLabelGap(0.02);
	}
	
	private JPanel createPanel()
	{
		return new ChartPanel(jfc);
	}
	
	public void Show()
	{
		setContentPane(createPanel());
		setVisible(true);
	}
	
	public static void main(String[] args)
	{
		jfcExample j = new jfcExample();
		j.setTitle("Example Chart...");
		j.setSize(640, 430);
		
		j.setValue("UNO", new Double(20.0));
		j.setValue("DUE", new Double(10.0));
		j.setValue("TRE", new Double(20.0));
		j.setValue("QUATTRO", new Double(30.0));
		j.setValue("CINQUE", new Double(20.0));
		
		j.setChar("Example Chart...");
		
		j.Show();
	}
}

image resize

This function will resize any input file restricting the width and height to be no more than the specified pixels, and output a binary stream.

function resize_jpg($inputFilename, $new_side){
$imagedata = getimagesize($inputFilename);
$w = $imagedata[0];
$h = $imagedata[1];

if ($h > $w) {
$new_w = ($new_side / $h) * $w;
$new_h = $new_side;
} else {
$new_h = ($new_side / $w) * $h;
$new_w = $new_side;
}

$im2 = ImageCreateTrueColor($new_w, $new_h);
$image = ImageCreateFromJpeg($inputFilename);
imagecopyResampled ($im2, $image, 0, 0, 0, 0, $new_w, $new_h, $imagedata[0], $imagedata[1]);
return $im2;
}

Installing libjpeg on OS X

Get http://www.ijg.org/files/jpegsrc.v6b.tar.gz, and then:

tar zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b
cp /usr/share/libtool/config.sub .
cp /usr/share/libtool/config.guess .
./configure --enable-shared --enable-static
make
sudo make install
sudo ranlib /usr/local/lib/libjpeg.a

Thumbnailer in Ruby and RMagick

require 'RMagick'

maxwidth = 120
maxheight = 160
aspectratio = maxwidth.to_f / maxheight.to_f
imgfile = 'world'

pic = Magick::Image.read(imgfile + '.jpg').first
imgwidth = pic.columns
imgheight = pic.rows
imgratio = imgwidth.to_f / imgheight.to_f
imgratio > aspectratio ? scaleratio = maxwidth.to_f / imgwidth : scaleratio = maxheight.to_f / imgheight
thumb = pic.resize(scaleratio)

white_bg = Magick::Image.new(maxwidth, thumb.height)
pic = white_bg.composite(thumb, Magick::CenterGravity, Magick::OverCompositeOp)
pic.write(imgfile + '.thumb.jpg')
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS