More Fractal Letters

February 10th, 2009

I’ve continued adding bits to my fractal letters over the past few weeks and have finally finished everything to print out my full e-mail address. The @ symbol was particularly interesting and I think it turned out pretty nice.

fractal_email_11

Fractal Email - first iteration

Fractal Email - third iteration

Fractal Email - third iteration

Fractal Email - fifth iteration

Fractal Email - fifth iteration

The pictures link to the updated app. Enjoy, and as usual, don’t slide the slider up to the max unless you’re willing to wait while your entire browser hangs for several minutes.

papercrane Flash, Flex, Fractal , ,

ASCIIMathML Wordpress Plugin

February 6th, 2009

I’ve updated my ASCIIMathML plugin so that it doesn’t have quite so many security holes and abuse vectors. I’ve put it up for download at http://www.reversefold.com/blog/asciimathml_0.2.tgz. Please note that this requires command-line access to run, requires python and inkscape to be installed, and has been tested only on linux. There may also be security problems and bugs. I take no responsibility for anything that may happen if you should use this. The code is released under the LGPL for now.

With the disclaimers out of the way, I hope this is useful to you. Let me know if you have any issues with it or any suggestions for improvements.

This is an update of a previous post.

papercrane PHP, math , , ,

Fractal Letters

January 20th, 2009

On the way home last Thursday I decided to try creating fractal letters. The idea was to replace each “line” in a simple version of a letter with a copy of itself but allow for the aspect ratio of its dimensions to change so that the result is still recognizable as a letter. This is something like an L-system, of which the Dragon curve is one. After a weekend’s worth of work I have what amounts to thirteen different fractal letter algorithms for six different letters. 1 P, 3 A, 2 E, 2 R, 2 C, 3 N. Yes, those spell PAPERCRANE. Here is the first iteration (just the letters).

Fractal Letters, iteration 1

Fractal Letters, iteration 1

And here is the fifth iteration:

Fractal Letters, iteration 5

Fractal Letters, iteration 5

Some letters have variations in how they are drawn or how they repeat, or both (hence the 2 lines). The bottom N uses an algorithm I worked out which didn’t change the aspect ratio and had only one repetition per line. Both of the N variants in the words use two repetitions per line and constrict the width faster than the height, which leaves the N recognizable. The idea here, after all, was to make letters which had a cool fractal look but could still be read.

Click either of the pictures to view the flex app and play with the iterations yourself. Just drag (or click on) the slider on the bottom of the app to change the iteration.

Stay tuned, I have another blog post coming up soon specifically about that N on the bottom, the math required to create the algorithm, and some interesting (and beautiful) results I got when implementing a test program.

papercrane AIR, Flash, Flex, FlexBuilder, Fractal, Papervision3D, math , ,

μ-reversefold

January 7th, 2009

Over the Christmas break I went to Chinatown in San Francisco and got some new origami paper which is smaller than any of my other paper. I quickly decided to make one of my signature reversefolds with the new paper. I started creasing the paper and got to the sane point, which would have given me 4 rows of reversefolds. This would have been small, but not small enough for me. I like my folds to be more complicated.

Finished creases, back side

Finished creases, back side

Finished creases, front side

Finished creases, front side

Yes, that paper is less than 2 quarters wide. Of course, going one more level took a lot of careful and precise folding which took the rest of the night. The next day I started the accordion folds which are the base of the final product.

Accordion halfway done

Accordion halfway done

Accordion finished

Accordion finished

That took several hours. The last step is to pop in the reverse folds, one row at a time. This is the most time consuming part of the process and for this fold it took me several nights with a small crafting tool. Here it is with five rows finished.

Five rows finished

Five rows finished

And after many painstaking hours of work, here is the finished product:

Finished fold

Finished fold

Finished fold, side view

Finished fold, side view

Finished fold, 3/4 view

Finished fold, 3/4 view

And for a little perspective here is a shot of the finished fold compared to the original paper:

Finished fold vs. unfolded paper

Finished fold vs. unfolded paper

And just in case the quarter wasn’t enough, here’s the finished fold in my palm. Yes, it is that small. No, I won’t make you one. ;-)

Finished fold in the palm of my hand

Finished fold in the palm of my hand

All of these photos link to my newly created flickr account. The entire set is up there along with higher quality versions.

papercrane Origami ,

Pretty equations in HTML

November 27th, 2008

You may have seen the nice looking equations in my last post. As part of the process of writing that post I researched MathML as a way of embedding the equations in the post. it turned out to be a complex markup language which renders very nicely but has a few big drawbacks. The first is that only Firefox supports MathML in its core so far. There is a plugin for Internet Explorer which supports it but I doubt it’s widely installed. The second problem is that you can’t just put MathML in your web page and have Firefox interpret it. It turns out you need a bit of Javascript to make it pick up and render the MathML. This is great and makes equations render very well in Firefox but writing the MathML is a pain.

Enter ASCIIMathML, a JavaScript library which converts a simple ASCII format to MathML for rendering in MathML compliant browsers. The syntax is pretty much what you would expect with a few tricks up its sleeve. A simple equation, such as `y=x^2` is written as y=x^2. A more complicated example from my last post such as:
`arc=2*cos^-1(1-r_2^2/(2*r_1^2))*r_1`
is written as:

arc=2*cos^-1(1-r_2^2/(2*r_1^2))*r_1

Another example with a square root:
`y=(+-r_2*sqrt(4*r_1^2-r_2^2))/(2*r_1)`

y=(+-r_2*sqrt(4*r_1^2-r_2^2))/(2*r_1)

Now, those if you viewing these posts in a non-Firefox browser are probably wondering why you are also seeing pretty equations here. The short answer is that you’re seeing images imstead of MathML rendered by your browser. The long answer involves a four part toolchain. The first piece is ASCIIMathML, slightly altered to insert an image tag instead of MathML. The images are generated by a PHP script which first runs the ASCII through ASCHIIMathPHP, a version of ASCIIMathML.js to PHP. The resulting MathML has the times symbol replace by one which the later tools understand (replace ⋅ with ·). The resulting MathML is then passed to svgmath, a python script which converts MathML to SVG. SVG would be great but Internet Explorer still doesn’t support it without a plugin. Because of this the SVG is then passed through inkscape via command-line to render out a png which is what you see in your browser.
Here’s some sample PHP to run these commands:

$ascii = 'y=(+-r_2*sqrt(4*r_1^2-r_2^2))/(2*r_1)';
 
$pngFile = md5($ascii).'.png';
if (!file_exists($pngFile)) {
    require 'ASCIIMathPHP-2.0.cfg.php';
    require 'ASCIIMathPHP-2.0.class.php';
 
    $ascii_math = new ASCIIMathPHP($symbol_arr);
    $ascii_math->setExpr($ascii);
    $ascii_math->genMathML();
 
    $mathml = $ascii_math->getMathML();
 
    $mathml = str_replace('⋅', '·', $mathml);
 
    $mathmlFile = tempnam(sys_get_temp_dir(), 'mathml');
    $td = fopen($mathmlFile, 'w');
    fwrite($td, $mathml);
 
    $svgFile = tempnam(sys_get_temp_dir(), 'svg');
    system("/usr/bin/env python math2svg.py -s $mathmlFile > $svgFile");
 
    system("inkscape -f $svgFile -e $pngFile");
 }
 
header('Content-Type: image/png');
die(file_get_contents($pngFile));

All of this is also wrapped up in a Wordpress plugin which includes the JS file in the header. Unfortunately, I don’t have an easy way to package this up as it requires python and inkscape but if you would like to get more info just let me know and I’ll send you more information.

UPDATE: I finally did update the plugin to be useful for others and posted it.

papercrane JavaScript, math , , , , ,

Fitting circles within a larger circle

November 24th, 2008

I have been working on an internal graphical tool at work and had an interesting problem arise. How can I know how many smaller circles I can fit within a larger circle? The best way to lay them out that I came up with was to put the smaller circles in an arc inside of the larger circle, just one smaller circle’s radius in from the edge of the large circle. This looks something like this:

Circles within a circle

Circles within a circle

When the first arc is filled up you then move to an arc which is the diameter of the small circles further within the large circle. The problem, however, is how to know how many circles can fit along that arc. I started originally with a simplistic realization that the arc length would be similar to the diameter of the small circles. This works out ok but ends up creating problems especially when you get close to the center of the larger circle (i.e. when the radius of the arc is close to the radius of the small circles). The correct solution is to calculate the exact angle along the arc that the circle takes up. After a few false starts with incorrect assumptions regarding tangent lines and their lengths I came up with the following.

Geometric definition

Geometric definition

  • The large circle is centered at the origin
  • `r_1` is the radius of the large circle
  • `C` is the center of the small circle
  • `r_2` is the radius of the large circle
  • `I` are the interseciton points between the circles
  • `theta` is the angle between the x axis and the line from the origin to `I`
  • `arc` is the length of the arc between the two interseciton points (`I`)

Given

`C=(r_1,0)`
`I=(x,+-y)`
`theta=cos^-1(x/r_1)`
`arc=2*theta*r_1`

Circle Equations

`x^2+y^2=r_1^2`
`(x-r_1)^2+y^2=r_2^2`

Solve for `y^2`

`y^2=r_1^2-x^2`
`y^2=r_2^2-(x-r_1)^2`

Set equal and solve for `x`

`r_2^2-(x-r_1)^2=r_1^2-x^2`
`x^2-(x^2-2*x*r_1+r_1^2)=r_1^2-r_2^2`
`2*x*r_1-r_1^2=r_1^2-r_2^2`
`2*x*r_1=2*r_1^2-r_2^2`
`x=(2*r_1^2-r_2^2)/(2*r_1)`
`x=r_1-r_2^2/(2*r_1)`

`theta` is then:

`theta=cos^-1((r_1-r_2^2/(2*r_1))/r_1)`
`theta=cos^-1(1-r_2^2/(2*r_1^2))`

You could then use `theta` directly but this will leave you with a remainder of space that’s empty in the arc. To make the circles equally spaced you need the arc length.

Replace values for a full `arc` formula

`arc=2*cos^-1(1-r_2^2/(2*r_1^2))*r_1`

You can then divide the large circle’s circumfrence by the arc length to get the actual number of circles that will fit. You then divide the circle into that many pieces and voila, you get equally spaced circles like the first image above.

`y` is not needed for the solution, but comes out to be:

`y=(+-r_2*sqrt(4*r_1^2-r_2^2))/(2*r_1)`

papercrane JavaScript, html, math ,

Falling Away

October 28th, 2008

I had a new idea last night on my way home for a simple use of Flash 10’s 3D to do a simple visualization. Imagine that you are looking down at the ground from high up. Suddenly the screen falls away and starts falling to the ground. Then the ground you were looking at falls away and you rezlize that it was only a screen. Repeat.

This screenshot shows the app running with a flickr satellite photo feed displaying on each falling plane. Unfortunately, since my code uses BitmapData.draw(), this won’t work from the web as I don’t want to run a flickr proxy. The linked version simply colors the planes, but you can get the idea from it. Right click the movie to view its code. If you run it locally with USE_FLICKR = true you can see the full effect.

papercrane Flash , , ,

Flash 10 Camp, 3D, Sound Creation, and IK

October 12th, 2008

I’ve just come home from Flash(10)Camp and am winding down. I had a lot of fun, saw some coll stuff, and even won Best Audio (with my team) with our app Flash Tones.

I was actually very surprised, there were a lot of well done apps, including others that generated sound but I guess we just had the app polished well enough. Thanks to Rod for that, he gave us the last few bits that really made it look nice. You’ll need Flash Player 10 and a web cam if you want to try it out.

All of the features I saw in Flash Player 10 were ones I’d seen before, except for the sound generation. Things were more in depth and better put together this time, though. The sound generation is nice, but complicated and hard to make it do what you’re expecting. Of course, I’m assuming you know almost nothing about the specifics of sound, like I do.

IK is fun, but I I focused on the other 2 as I didn’t have any IK idea that seemed particularly interesting.

The 3D is very nice, being able to move and rotate objects in all three dimensions opens up a lot of new possibilities. I had a lot of fun creating some simple squares and rotating them, then applying various blend modes and filters. Here are 2 of my favorites:

Those were fun but then someone mentioned webcams. I successfully connected up webcam video to my project and then spent several hours getting the video mapped to each of the rotating squares then getting it all matched up with the video behind it. I then added edge detection (thanks to Luke Walsh’s post), added some more blending and filters and voila!

I was quite proud of that and still think it looks cool. I wish I’d shown it earlier at the Hackathon but then again we won an award so I guess I’m just being greedy.

As an added bonus, here’s an earlier version of Flash Tones which warbled in an interesting way. Try putting your hand up at the very right edge of the screen.

papercrane Flash , , , ,

Flex Bug: Icon shadows in a TabNavigator’s Tabs

May 27th, 2008

This is likely a generic problem with anything that uses a Button but I have found this issue with Tabs in a TabNavigator specifically. When you have a TabNavigator with multiple complicated children that take a while to load, clicking one tab then clicking another quickly can lead to a “shadow” icon at the top-left of the tabs. See the bug for more: SDK-15656

papercrane Flex , ,

Running JavaScript when the page finishes loading

May 23rd, 2008

I know what you’re thinking. window.onload and <body onload> already let you run javascript when the page loads. Why do I need something more? The answer is that the standard onload only runs when the entire page and its dependencies are finished loading. This means that if you have multiple images (such as tracking tags) or scripts or a large swf, the onloaded code will only run once all of those pieces load. The point of the following is to run some JavaScript once the page itself has loaded and been parsed. This allows you to do things like set the background color, add additional elements, or start loading other dependencies while the rest of the page’s depdencies are loading. This is particularly useful if you need to do something on a page while a large swf is loading.

The following is a combination of several posts on Dean Edwards’ blog as well as an onload handler for those browsers that don’t support this kind of solution that I found elsewhere. This may not work in Opera, I am not sure. According to the second post Opera supports DOMContentLoaded but I don’t know if this code will catch Opera properly.

function init() {
  // quit if this function has already been called
  if (arguments.callee.done) return;
  // flag this function so we don't do the same thing twice
  arguments.callee.done = true;
 
  //CODE TO RUN GOES HERE
}
 
// for Mozilla browsers
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", init, false);
} else
// for Safari
if (/WebKit/i.test(navigator.userAgent)) { // sniff
  var _timer = setInterval(function() {
    if (/loaded|complete/.test(document.readyState)) {
      clearInterval(_timer);
      init(); // call the onload handler
    }
  }, 10);
} else
//for those other poor souls
{
  function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
      window.onload = func;
    } else {
      window.onload = function() {
        oldonload();
        func();
      }
    }
  }
  addLoadEvent(init);
}
// for Internet Explorer (using conditional comments)
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id='__ie_onload' src='javascript:void(0)'></script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
  if (this.readyState == "complete") {
    init(); // call the onload handler
  }
};
/*@end @*/

UPDATE: fixed the code above. Wordpress was screwing up the last bit due to its insistence on changing the preformatted HTML in the code.

papercrane JavaScript ,