Ruby Fractal Library

Posted by Ryan Baxter Thu, 03 Jul 2008 21:26:00 GMT

Last October, I wrote a small fractal rendering program in Ruby using the Shoes windowing toolkit written by why the lucky stiff. It’s sole purpose was to test Shoes. The code was painfully slow at rendering the Mandelbrot set, but it did, however, begin a small obsession of mine with fractals.

Since I haven’t been able to find a fractal library for Ruby, I decided to write one. Over the last two weeks I’ve written some code to generate both the Mandelbrot and Julia set fractals using the escape time algorithm. The code is still slow, but within a couple weeks I hope to replace the slow portions with inline C.

There may still be some bugs and I haven’t added any error handling, but here it is. An “almost” pure Ruby fractal library. Once this is cleaned up I’ll repost the code. I suppose a gem could be possible as well. Happy 4th!

fractals.rb

# RB

require 'rubygems'
require 'complex'
require 'png'

module Fractals
  module Fractal
    attr_accessor :begin_range, :end_range

    def initialize(begin_range, end_range)
      @begin_range, @end_range = begin_range, end_range
    end

    def draw(height=250, width=250, m=1.0,
      save_as=self.class.to_s.downcase + '.png')
        canvas = PNG::Canvas.new(height, width)

        # Find the complex coordinate for each pixel.
        0.upto(height - 1) { |y|
          i = (y * (@end_range.image - @begin_range.image) / height +
          @begin_range.image) * m
          0.upto(width - 1) { |x|
            r = (x * (@end_range.real - @begin_range.real) / width +
            @begin_range.real) * m
            if self.in_set?(Complex(r, i)) then
              canvas[x, y] = PNG::Color::Black
            else
              canvas[x, y] = fetch_color(self.last_iteration, self.max_iterations)
            end                    
          }
        }

        png = PNG.new(canvas)
        png.save(save_as)
    end  

    private
    def fetch_color(last_iteration, max_iterations)  
      divisor = 765*last_iteration/max_iterations
      case divisor
        when 0..254 then return PNG::Color.new(divisor%255, 0, 0, 255)
        when 255..509 then return PNG::Color.new(255, divisor%255, 0, 255)
        when 510..765 then return PNG::Color.new(255, 255, divisor%255, 255)
      end       
    end
  end

  class Julia
    include Fractal
    attr_accessor :seed, :bailout, :max_iterations
    attr_reader :last_iteration

    def initialize(seed=Complex(0.36, 0.1), bailout=2, max_iterations=100,
    begin_range=Complex(-2.25, -1.5), end_range=Complex(0.75, 1.5))
      super(begin_range, end_range)
      @seed, @bailout, @max_iterations = seed, bailout, max_iterations
    end

    def in_set?(z)
      @max_iterations.times { |i|
        z = z**2 + @seed
        if z > @bailout then
          @last_iteration = i
          return false
        end      
      }
      return true
    end
  end

  class Mandelbrot
    include Fractal  
    attr_accessor :bailout, :max_iterations
    attr_reader :last_iteration

    def initialize(bailout=5, max_iterations=100, begin_range=Complex(-2.25,
    -1.5), end_range=Complex(0.75, 1.5))
      super(begin_range, end_range)
      @bailout, @max_iterations = bailout, max_iterations
    end

    def in_set?(c)
      z = 0
      @max_iterations.times { |i|
        z = z**2 + c
        if z > @bailout then
          @last_iteration = i
          return false
        end            
      }
      return true
    end
  end
end

Using this library is as simple as the following:

require 'fractals'

mandelbrot = Fractals::Mandelbrot.new
mandelbrot.draw

Any suggestions/bug fixes can be posted here. Thanks.

IE7's Inanimate GIF

Posted by Ryan Baxter Wed, 11 Jun 2008 12:06:00 GMT

Animated GIFs are most often used as activity indicators in modern AJAX (asynchronous JavaScript and XML) enabled websites. I decided to use a GIF (Graphic Interchange Format) from ajaxload.info on an application that I’ve been developing at work. The application performs server-side processing on files uploaded by employees. Processing time varies depending on the size of the file. Larger files take longer.

I wanted the animated GIF to appear when the file upload button was clicked so I placed the image within a DIV tag and hid it by setting a blank CSS display property. OnClick of the upload button, a JavaScript function toggled the display value to “block”, making the DIV appear. This worked as expected in Firefox and IE6, but not in IE7. The DIV appeared in IE7, but it’s GIF wasn’t moving.

A little googling turned up a helpful comment on Rick Strahl’s blog.

I’ve run into a problem with animated gifs inside of a hidden area of a Web page that is hidden with style.display=’none’. When the area is made visible again, in Internet Explorer this causes the image to not be displayed an animated GIF whatever I try. [sic]

Apparently IE7 doesn’t like to animate hidden GIFs. User submitted comments on Rick Strahl’s blog provided many solutions, but only a couple worked in my situation. The first uses the JavaScript setTimeout method to populate the image’s SRC attribute 200 ms after the function call. The second sets the DIV’s innerHTML to a string containing an IMG tag with the animated GIF.

<script language='javascript'> 
    function ShowLoading(elementId) 
    {
        document.getElementById(elementId).style.display = "block"; 
        setTimeout('document.images["loadingImage"].src = "../images/loading.gif"', 200); 
    } 
</script>

Neither solution is ideal, but setting the element’s innerHTML felt a little bit cleaner to me.

<script language='javascript'>
    function ShowLoading(elementId)
    {   
        var element = document.getElementById(elementId);    

        element.innerHTML = "<img src='../images/loading.gif'>";    
        element.style.display = "block";
    }
</script>

*IE7 may have animations disabled. Go to Tools > Internet Options > Advanced > Multimedia. Checking “Play animations in webpages” may affect how IE7 renders your animated GIFs.

Samba Network Shares with Nautilus in Hardy Heron Part 2

Posted by Ryan Baxter Sun, 01 Jun 2008 23:07:00 GMT

Nearly a month ago I wrote about my problems connecting to the network shares of my Linksys NAS200 using Nautilus in Ubuntu, Hardy Heron. My fix was simple and it worked. Unfortunately, the solution’s WAF (Wife Acceptance Factor) was low. Apparently she didn’t think it was easy enough to execute smbmount from a Terminal window. Whatever :) I have since written a small shell script that is called from Session Startup.

Initially, the script failed to connect to my network shares because my wireless network connection hadn’t finished negotiating before the script was executed. I coded around this by creating a while loop that greps the output of a ping to my router. If successful, the mount_shares function is called. Otherwise. the thread will sleep for 10 seconds and try again.

#!/bin/bash

user="thorbardin/ryan%password"
root_dir="/home/ryan/Network Shares"

mount_shares() {
  public_dir="$root_dir"/"Public on Thorbardin"
  home_dir="$root_dir"/"Home on Thorbardin"

  [ -d "$public_dir" ] || mkdir -p "$public_dir"
  [ -d "$home_dir" ] || mkdir -p "$home_dir"

  smbmount "//192.168.1.105/public disk" "$public_dir" -o user="$user"
  smbmount "//192.168.1.105/ryan" "$home_dir" -o user="$user"
}

while [ 1 ]; do
  if ping -c2 192.168.1.1 2>&1 | grep ttl; then
    mount_shares

    exit 0   
  fi
  sleep 10
done

exit 0

I’m already thinking about rewriting this script so that it scans my network using smbtree and automatically mounts all available network shares. That’ll be Part 3!

Temporary Identity Impersonation in ASP.NET

Posted by Ryan Baxter Tue, 27 May 2008 16:01:00 GMT

Uploading files in an ASP.NET application is relatively easy to do. Uploading to a remote machine is a little bit trickier, but certainly doable. I followed a set of instructions on aspalliance.com, but rather than declare an account to impersonate in my web.config file, I decided to do it in code. That way I could use impersonation only when needed and encapsulate it for later use.

I referred to an article on Microsoft’s Help and Support website about how to implement impersonation. Their code worked great, but I decided to put it in a class to help keep things DRY.

using System;
using System.Web;
using System.Web.Security;
using System.Security.Principal;
using System.Runtime.InteropServices;

namespace Utilities
{    
    public class ImpersonateUser
    {
        public const int LOGON32_LOGON_INTERACTIVE = 2;
        public const int LOGON32_PROVIDER_DEFAULT = 0;

        WindowsImpersonationContext impersonationContext;

        [DllImport("advapi32.dll")]
        public static extern int LogonUserA(String lpszUserName,
            String lpszDomain,
            String lpszPassword,
            int dwLogonType,
            int dwLogonProvider,
            ref IntPtr phToken);
        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int DuplicateToken(IntPtr hToken,
            int impersonationLevel,
            ref IntPtr hNewToken);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool RevertToSelf();

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern bool CloseHandle(IntPtr handle);

        public bool ImpersonateValidUser(String userName, String domain, String password)
        {
            WindowsIdentity tempWindowsIdentity;
            IntPtr token = IntPtr.Zero;
            IntPtr tokenDuplicate = IntPtr.Zero;

            if (RevertToSelf())
            {
                if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
                    LOGON32_PROVIDER_DEFAULT, ref token) != 0)
                {
                    if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                    {
                        tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                        impersonationContext = tempWindowsIdentity.Impersonate();
                        if (impersonationContext != null)
                        {
                            CloseHandle(token);
                            CloseHandle(tokenDuplicate);
                            return true;
                        }
                    }
                }
            }
            if (token != IntPtr.Zero)
                CloseHandle(token);
            if (tokenDuplicate != IntPtr.Zero)
                CloseHandle(tokenDuplicate);
            return false;
        }

        public void UndoImpersonation()
        {
            impersonationContext.Undo();
        }
    }
}

After referencing my Utilities namespace I was then able to impersonate the account required for uploading:

if (FileUpload1.HasFile)
{
    ImpersonateUser impersonateUser = new ImpersonateUser();

    if (impersonateUser.ImpersonateValidUser("userName", "", "password"))
    {
        FileUpload1.SaveAs(Server.MapPath("~/files/fileName.txt"));
        impersonateUser.UndoImpersonation();
    }
    else
    {
        throw new Exception("Identity impersonation has failed.");
    }   
}

*The SaveAs method of the FileUpload control requires a root path. Using Server.MapPath will provide the root path of your IIS virtual folder.

A Few Date Methods

Posted by Ryan Baxter Tue, 06 May 2008 18:45:00 GMT

An ASP.NET project of mine recently required the calculation of the start and end date of the current date’s previous month. This was more difficult putting into words than code. It did, however, get me thinking about other common date routines.

public static DateTime FirstDayOfPreviousMonth
{
    get
    {
        return new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-1);
    }    
}

public static DateTime LastDayOfPreviousMonth
{
    get
    {
        return new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
    }
}

Being preemptive, I decided to include a few methods for determining the start and end date of the current date’s fiscal quarter. I started by calculating the current date’s quarter. This was accomplished with just a little division.

public static int CurrentQuarter
{
    get
    {
        return (DateTime.Now.Month + 2) / 3;
    }
}

Finding the start and end dates of the current quarter was harder, but could still be expressed in a single line of code (or two).

public static DateTime FirstDayOfCurrentQuarter
{
    get
    {
        return new DateTime(DateTime.Now.Year, 1 + ((CurrentQuarter - 1) * 3), 1);
    }
}

public static DateTime LastDayOfCurrentQuarter
{
    get
    {
        int lastMonthOfCurrentQuarter = 3 + ((CurrentQuarter - 1) * 3);

        return new DateTime(DateTime.Now.Year, lastMonthOfCurrentQuarter, DateTime.DaysInMonth(DateTime.Now.Year, lastMonthOfCurrentQuarter));
    }
}

I’d like to extend this collection to include other common date routines. Feel free to post your date methods as comments if you’d like to share.

*The methods I’ve created for calculating the current quarter’s start and end dates assume the fiscal year starts on January 1st. This may not be suitable for your needs.

Setting Focus in ASP.NET Ajax Pages

Posted by Ryan Baxter Mon, 31 Mar 2008 22:24:00 GMT

At the new job I’ve been using a lot of ASP.NET Ajax to help ease the transition of users from VB6 desktop applications to web applications on our company intranet. In doing this, the UpdatePanel has become my new best friend. Albeit charming, my old ASP.NET 2.0 friends were not as impressed.

With ASP.NET 2.0, came the long-awaited Focus method that allowed developers to set the page focus without having to write any JavaScript. Developers loved it and all was right with the world. That is until ASP.NET Ajax showed up.

I recently spent way too much time trying set the page focus on a page that contained just a few TextBox controls and an UpdatePanel. My site’s ScriptManager was located in a MasterPage, but more on that later. My obligatory googling turned up many work arounds, but none as simple as the following:

// Example

if (Page.IsPostback)
{   
    // Use the GetCurrent method if your ScriptManager is located
    // in a MasterPage. Word.
    ScriptManager.GetCurrent(this.Page).SetFocus(TextBox1);
}
else
{
    // Here is the money.
    AjaxControlToolkit.Utility.SetFocusOnLoad(TextBox2);
}

The above example is a two-fer. The ScriptManager’s SetFocus method is handy, but remember that only one ScriptManager is allowed per page and if you’ve put your ScriptManager in a MasterPage, then you’ll need to access it in code using the GetCurrent method of the ScriptManager class.

Two. The ASP.NET 2.0 UpdatePanel is not the only Ajax control. Download the ASP.NET Ajax Control Toolkit. With this toolkit, you’ll have access to dozens of controls and best of all – the SetFocusOnLoad method. Located in the Utility class, the SetFocusOnLoad method is the answer to your !Page.IsPostback problems.

Code Snippet: Ruby Walks with Shoes

Posted by Ryan Baxter Tue, 30 Oct 2007 14:30:00 GMT

_Why of Why’s (Poignant) Guide to Ruby has announced the publication of a 52 page comic style book illustrating the ins and outs of his new windowing toolkit, Shoes. In his words, “Shoes is a very informal graphics and windowing toolkit. It’s for making regular old apps that run on Windows, Mac OS X and Linux. It’s a blend of my favorite things from the Web, some Ruby style, and a sprinkling of cross-platform widgets.”

Using the the latest Shoes revision, I’ve written some code to generate a Mandelbrot fractal. Try it out with the following command (assuming your script is located in the Shoes samples directory):

./shoes samples/mandelbrot.rb

You may want to go make a sandwich while you wait. Here is a sample of the output:

Here is the code:

# RB

require 'complex'

class Mandelbrot  
  def initialize(bailout=10, iterations=100)
    @bailout, @iterations = bailout, iterations   
  end  

  # A method for determining if a point is within
  # the Mandelbrot set.
  def in_set?(x, y)
    c = Complex(x, y)
    z = 0
    @iterations.times do |i|
      z = z**2 + c                       
      return false if z > @bailout
    end
    return true 
  end  
end

class Generator < Shoes
  url '/', :index

  def index()    
    render(250, 250, rgb(205, 102, 0))    
  end

  def render(height=250, width=250, color=rgb(255, 0, 0))
    nostroke
    fill color   

    mandelbrot = Mandelbrot.new

    # Render the fractal.
    0.upto(height) do |y|
      0.upto(width) do |x|
        scaled_x = -2 + (3 * x / width.to_f)
        scaled_y = 1 + (-2 * y / height.to_f)
        if mandelbrot.in_set?(scaled_x, scaled_y) then        
          rect :left => x, :top => y, :width => 1, :height => 1
          # oval :left => x, :top => y, :radius => 1, :center => true                           
        end      
      end
    end
  end
end

Shoes.app :title => 'Mandelbrot', :height => 250, :width => 250

Code Snippet: Ruby Image Scraper

Posted by Ryan Baxter Tue, 14 Aug 2007 03:46:00 GMT

I stumbled upon a screen scraping library for Ruby last week called scrAPI. It’s extremely flexible and can be seen in action on the co.mments blog post scraper. The scrAPI library can be installed by issuing the following command from your console:

gem install scrapi

Testing scrAPI was fairly easy once I figured out how to define a scraper. With that aside, I wrote a small script that saves images from a URL provided by the user. The scrAPI library could be used for good or evil, but only you can decide.

#!/usr/bin/ruby

require 'fileutils'
require 'open-uri'
require 'pathname'
require 'rubygems'
require 'scrapi'

# Get the URL input.
puts 'Enter a URL:'
url = gets.chomp

# Get the HTML source.
html = nil
open(url) {|source| html = source.read()}

# Define the scraper.
scraper = Scraper.define do
  array :images
  process "img", :images => "@src"
  result :images
end

# Scrape the HTML for images.
images = scraper.scrape(html)

# Create a directory to save the images in.
directory = url.gsub(/http:\/\//, '')
FileUtils.mkdir directory

images.each do |image_path|
  # Determine if image_path is absolute or relative. 
  path = Pathname.new(image_path)  
  if not path.relative? then image_path = url + image_path end

  # Write the image to disk.
  open(image_path) do |source|
    file_name = image_path.split('/').last
    open(directory + '/' + file_name, 'wb') {|file| file.write(source.read())}
  end
end

puts 'Finished...'

Code Snippet: Ruby Word Masher

Posted by Ryan Baxter Sat, 28 Jul 2007 20:54:00 GMT

I can be extremely indecisive about things. So much, in fact, that I even wrote a script to help me choose a name for this website. Is it strange that a random number generator can make me feel better about making decisions? My wife thinks I’m crazy, but she also calls domain names, donames. Besides, I think there is something novel in a computer choosing a name for itself. Anyway… Given some user input, the code below will read words from a file and then mash them together to provide unique combinations. Here is a words.dat file to help get you started. Happy mashing.

#!/usr/bin/ruby

words = Array.new
i = 0

def mash_words(words, mash_count)
  new_word = ''

  1.upto(mash_count) do
    new_word += words[rand(words.length)]
  end

  return new_word
end

begin
  puts 'How many mashed words would you like to create?'
  word_count = gets.chomp.to_i  

  puts 'How many words would you like to mash?'
  mash_count = gets.chomp.to_i  

  input_file = File.new("words.dat", "r")
  while (line = input_file.gets)    
    words[i] = line.chomp    
    i += 1
  end  

  1.upto(word_count) do    
    puts mash_words(words, mash_count)
  end  

  input_file.close  
rescue => err
  puts "Exception: #{err}"
  err
end

Code Snippet: Fibonacci with IronRuby & WPF

Posted by Ryan Baxter Thu, 26 Jul 2007 04:28:00 GMT

On Tuesday, John Lam announced the Pre-Alpha Release of IronRuby. To satisfy my curiosity, I downloaded and compiled the source code found on his blog. A simple Build.cmd file was provided and made the compilation a breeze. Rather than write another Hello World, I decided to kick the tires by writing a simple application to calculate Fibonacci numbers. I didn’t use many of the niceties that make Ruby what it is, but I did, however, uncover some oddities between Ruby and .NET types. I coded around a few of the problems only to find other features that were either broken or missing. The release was labeled “Pre-Alpha” for a reason so don’t be surprised if you have a similar experience with IronRuby.

I have to admit that I felt a bit dirty hacking together bits of the Windows Presentation Foundation (WPF) with Ruby only to achieve a single, ugly window with one text box, some words, and a button. It wouldn’t have been so bad if only I could have managed to use the .NET HorizontalAlignment enumeration with the controls on my window. Every effort I made in aligning a control caused an Exception. Defining the Orientation of my StackPanel also resulted in an Exception. Taking input was easy. Performing validation on the input was not. I could not get a string comparison to work between .NET and Ruby string types. I tried a half dozen combinations of .ToString, to_s, and Convert.ToString with no success. I gave up and wrapped my problem expression in a begin-rescue block.

begin
  fibonacci_label.content = fetch_iterative_fibonacci(Convert.ToInt32(input_text_box.Text)).to_s 
rescue
  MessageBox.show('Please enter an integer.')
end

If Microsoft creating their own implementation of Ruby isn’t interesting enough, John Lam has said that IronRuby will be hosted at RubyForge. His reason for this is simple. John believes that Open Source developers have more experience using Subversion rather than Microsoft’s Team Foundation Server. I agree and believe this decision will only benefit IronRuby by involving the whole Ruby community. This will most likely cause those non-Microsoft developers to contribute who otherwise wouldn’t if the project were hosted at CodePlex. Check out episode 254 of .NET Rocks! for more on this and John Lam’s work on IronRuby and the Dynamic Language Runtime.

I am not disappointed with the status of this release. Since IronRuby will be open for community contribution, the code will be reviewed by many eyes and the bugs that I did encounter will be fixed quickly. The integration of IronRuby and WPF was not pleasant, but I do believe that over time the two technologies could compliment each other nicely. The power of the presentation foundation combined with Ruby’s syntax sugar will undoubtedly make for an impressive stack that could give others a run for their money. Now can I have this in Mono please?

Both an iterative and recursive solution can be found within my source. I provided both in the odd chance that somebody reading this might want to try a rudimentary O(2^n) performance test between the Ruby interpretor and IronRuby. The source code for my Fibonacci example can be downloaded from the links below.

Fibonacci.tar.gz
Fibonacci.zip

Older posts: 1 2