<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The paperCrane's view from the reverseFold</title>
	<atom:link href="http://www.reversefold.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.reversefold.com/blog</link>
	<description>Flex, PHP, and anything else I care to talk about</description>
	<lastBuildDate>Sat, 24 Jul 2010 04:55:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Java-style Typesafe Enumerations in AS3</title>
		<link>http://www.reversefold.com/blog/2010/07/22/java-style-typesafe-enumerations-in-as3/</link>
		<comments>http://www.reversefold.com/blog/2010/07/22/java-style-typesafe-enumerations-in-as3/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 01:00:01 +0000</pubDate>
		<dc:creator>papercrane</dc:creator>
				<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://www.reversefold.com/blog/?p=137</guid>
		<description><![CDATA[Enumerations are one of the major language features that I miss in AS3, especially when writing client-side versions of Java code which use Java&#8217;s robust enumerations. This has been discussed elsewhere quite a bit so I&#8217;ll just do a quick recap and then give my new updated version. The simplest version of an enumeration you [...]]]></description>
			<content:encoded><![CDATA[<p>Enumerations are one of the major language features that I miss in AS3, especially when writing client-side versions of Java code which use Java&#8217;s robust enumerations. This has been discussed <a href="http://www.herrodius.com/blog/87">elsewhere</a> <a href="http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/">quite</a> <a href="http://blog.petermolgaard.com/2008/11/02/actionscript-3-enums/">a</a> <a href="http://scottbilas.com/blog/ultimate-as3-fake-enums/">bit</a> so I&#8217;ll just do a quick recap and then give my new updated version.</p>
<p>The simplest version of an enumeration you can do in AS3, of course (and the one most people use) are <code>static const String</code> values that are set in a class which is meant not to be instantiated.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">example</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleEnum <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const ONE : <span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;One&quot;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const TWO : <span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;Two&quot;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const THREE : <span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;Three&quot;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This works for the simplest cases but doesn&#8217;t come near to having the features of Java&#8217;s enumerations. One alternate version uses int values to make the conversion from Java to AS3 easier (Java uses int values to identify each enumeration value which is called an ordinal).</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">example</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleEnum <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const ONE : uint = <span style="color: #cc66cc;">1</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const TWO : uint = <span style="color: #cc66cc;">2</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const THREE : uint = <span style="color: #cc66cc;">3</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The main problem with both of the above is that any time you use one of your <code>SimpleEnum</code> values, you are using a simple type (<code>String</code> or <code>uint</code>) which allows any random value to be passed in, not just those that you have defined.</p>
<p>The next step is to use instances of the Enum class for each one of the constants. This makes your enumerations typesafe and allows you to have your enum values be real objects with member functions and variables.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">example</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleEnum <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const ONE : SimpleEnum = <span style="color: #000000; font-weight: bold;">new</span> SimpleEnum<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;One&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const TWO : SimpleEnum = <span style="color: #000000; font-weight: bold;">new</span> SimpleEnum<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;One&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const THREE : SimpleEnum = <span style="color: #000000; font-weight: bold;">new</span> SimpleEnum<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;One&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_name</span> : <span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> <span style="color: #0066CC;">name</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">_name</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> SimpleEnum<span style="color: #66cc66;">&#40;</span>inName : <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">_name</span> = inName;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This is great as it enforces that any time you want to use a value from <code>SimpleEnum</code> you can ensure that it&#8217;s the right type, but this has a different form of the same issue above. Other code can happily create new versions of <code>SimpleEnum</code> and use them wherever.</p>
<p>It is at this point that some of the more esoteric bits of AS3 coding can be helpful. Private constructors would fix this problem right away but AS3 also lacks this language feature. For Singletons, most developers use a private class which is appended to the same file, after the package block ends. Unfortunately, <a href="http://www.herrodius.com/blog/87">this doesn&#8217;t work</a> as the &#8220;private&#8221; class is not yet loaded when the <code>static const</code> variables are being created. Luckily, <a href="http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/">static initializers fix this problem</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">example</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleEnum <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const ONE : SimpleEnum = <span style="color: #000000; font-weight: bold;">new</span> SimpleEnum<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;One&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const TWO : SimpleEnum = <span style="color: #000000; font-weight: bold;">new</span> SimpleEnum<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;One&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const THREE : SimpleEnum = <span style="color: #000000; font-weight: bold;">new</span> SimpleEnum<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;One&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_name</span> : <span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> <span style="color: #0066CC;">name</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">_name</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> _created : <span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//This code is run statically after the const vars above are created, closing off the constructor</span>
		<span style="color: #66cc66;">&#123;</span>
			_created = <span style="color: #000000; font-weight: bold;">true</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> SimpleEnum<span style="color: #66cc66;">&#40;</span>inName : <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_created<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;SimpleEnum is an enumeration, use the class constants instead.&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">_name</span> = inName;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>I can see only two downsides to this pattern. Any function parameter or variable which is of the <code>SimpleEnum</code> type could be set to <code>null</code>. This is a common issue with Object Oriented programming, though, and can happen with Java&#8217;s <code>enum</code> type as well, so this is minimal. The larger possible issue is that the error for trying to use <code>new SimpleEnum()</code> happens at run-time and not compile-time. This could bite users who mistakenly try to instantiate the <code>SimpleEnum</code> class in their code and could potentially not be found right away, since a specific set of run-time conditions may have to be satisfied to call the offending code. However, with a helpful enough error message and documentation I think that this is a small issue.</p>
<p>This is essentially the pattern that I have adopted but I&#8217;ve added a few more features. Specifically, I&#8217;ve implemented an ordinal value which can be used for interaction with Java enum values or for advanced serialization or hashing and have added a function to get all of the possible values for use in iteration (or enumeration of the values&#8230;.;-) ). The creation checking is also done in the base class so that a minimum of extra work needs to be done in the concrete enum classes.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">reversefold</span>.<span style="color: #006600;">util</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">Dictionary</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">describeType</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">getQualifiedClassName</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * An abstract class to emulate Enum type.
	 * 
	 * Subclasses should follow this pattern:
	 * 
	 * package com.example {
	 * 	public class TestEnum extends Enum {
	 * 		public static const ONE : TestEnum = new TestEnum();
	 * 		public static const TWO : TestEnum = new TestEnum();
	 * 		public static const THREE : TestEnum = new TestEnum();
	 * 		
	 * 		public static function get values() : Vector.&lt;testenum&gt; {
	 * 			return Vector.&lt;/testenum&gt;&lt;testenum&gt;(Enum.getValues(TestEnum));
	 * 		}
	 * 		
	 * 		{
	 * 			initEnumConstant(TestEnum);
	 * 		}
	 * 	}
	 * }
	 */</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Enum <span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> sequence : Dictionary = <span style="color: #000000; font-weight: bold;">new</span> Dictionary<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * To protect from instantiation after static initializing.
		 */</span>
		protected <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> values : Dictionary = <span style="color: #000000; font-weight: bold;">new</span> Dictionary<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//.&lt;vector .&lt;Enum&gt;&gt;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Function to call for each enum type declared and in static init.
		 */</span>
		protected <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> initEnumConstant<span style="color: #66cc66;">&#40;</span>inType : <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> className : <span style="color: #0066CC;">String</span> = getQualifiedClassName<span style="color: #66cc66;">&#40;</span>inType<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> typeXML : <span style="color: #0066CC;">XML</span> = describeType<span style="color: #66cc66;">&#40;</span>inType<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> newValues : Vector.<span style="color: #66cc66;">&lt;</span>enum<span style="color: #66cc66;">&gt;</span> = <span style="color: #000000; font-weight: bold;">new</span> Vector.<span style="color: #66cc66;">&lt;/</span>enum<span style="color: #66cc66;">&gt;&lt;</span>enum<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">for</span> each <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> constantXML : <span style="color: #0066CC;">XML</span> <span style="color: #b1b100;">in</span> typeXML.<span style="color: #006600;">constant</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">name</span> : <span style="color: #0066CC;">String</span> = constantXML.<span style="color: #66cc66;">@</span><span style="color: #0066CC;">name</span>;
				<span style="color: #000000; font-weight: bold;">var</span> constant : Enum = inType<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">name</span><span style="color: #66cc66;">&#93;</span>;
				constant._label = <span style="color: #0066CC;">name</span>;
				newValues.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>constant<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #808080; font-style: italic;">//sort by ordinal (instantiation order)</span>
			values<span style="color: #66cc66;">&#91;</span>className<span style="color: #66cc66;">&#93;</span> = newValues.<span style="color: #0066CC;">sort</span><span style="color: #66cc66;">&#40;</span>
				<span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>lhs : Enum, rhs : Enum<span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span>
					<span style="color: #b1b100;">return</span> lhs._ordinal <span style="color: #66cc66;">&gt;</span> rhs._ordinal ? <span style="color: #cc66cc;">1</span> : <span style="color: #66cc66;">&#40;</span>lhs._ordinal <span style="color: #66cc66;">&lt;</span> rhs._ordinal ? -<span style="color: #cc66cc;">1</span> : <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> getValues<span style="color: #66cc66;">&#40;</span>inType : <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #66cc66;">&#41;</span> : Vector.<span style="color: #66cc66;">&lt;</span>Enum<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> Vector.<span style="color: #66cc66;">&lt;/</span>enum<span style="color: #66cc66;">&gt;&lt;</span>enum<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span>values<span style="color: #66cc66;">&#91;</span>getQualifiedClassName<span style="color: #66cc66;">&#40;</span>inType<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Enum label.
		 */</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _label : <span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> label<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> _label;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _ordinal : uint;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> ordinal<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : uint <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> _ordinal;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Enum<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> className : <span style="color: #0066CC;">String</span> = getQualifiedClassName<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>values<span style="color: #66cc66;">&#91;</span>className<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Cannot instantiate anymore: &quot;</span> + className<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">var</span> seq : uint;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>sequence<span style="color: #66cc66;">&#91;</span>className<span style="color: #66cc66;">&#93;</span> == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				seq = <span style="color: #cc66cc;">0</span>;
			<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
				seq = sequence<span style="color: #66cc66;">&#91;</span>className<span style="color: #66cc66;">&#93;</span> + <span style="color: #cc66cc;">1</span>;
			<span style="color: #66cc66;">&#125;</span>
			_ordinal = seq;
			sequence<span style="color: #66cc66;">&#91;</span>className<span style="color: #66cc66;">&#93;</span> = seq;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> ordinal + <span style="color: #ff0000;">&quot; &quot;</span> + label;
		<span style="color: #66cc66;">&#125;</span> 
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&lt;/</span>enum<span style="color: #66cc66;">&gt;&lt;/</span>vector<span style="color: #66cc66;">&gt;&lt;/</span>testenum<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p>And here is an example use:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">reversefold</span>.<span style="color: #006600;">shop</span>.<span style="color: #006600;">model</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestEnum <span style="color: #0066CC;">extends</span> Enum <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const ONE : TestEnum = <span style="color: #000000; font-weight: bold;">new</span> TestEnum<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;One&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const TWO : TestEnum = <span style="color: #000000; font-weight: bold;">new</span> TestEnum<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Two&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const THREE : TestEnum = <span style="color: #000000; font-weight: bold;">new</span> TestEnum<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Three&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//This function gives typesafe access to the list of constants that Enum.getValue() gives</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> values<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : Vector.<span style="color: #66cc66;">&lt;</span>testenum<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> Vector.<span style="color: #66cc66;">&lt;/</span>testenum<span style="color: #66cc66;">&gt;&lt;</span>testenum<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span>Enum.<span style="color: #006600;">getValues</span><span style="color: #66cc66;">&#40;</span>TestEnum<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//static initializer, make sure to pass in the current class as a parameter</span>
		<span style="color: #66cc66;">&#123;</span>
			initEnumConstant<span style="color: #66cc66;">&#40;</span>TestEnum<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//read-only getter of the name value</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_name</span> : <span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> <span style="color: #0066CC;">name</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">_name</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> TestEnum<span style="color: #66cc66;">&#40;</span>inName : <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">_name</span> = inName;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">super</span>.<span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot; &quot;</span> + <span style="color: #0066CC;">_name</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&lt;/</span>testenum<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p>Thanks to all of the linked blog postings for their ideas. I only found <a href="http://scottbilas.com/blog/ultimate-as3-fake-enums/">Scott Bilas&#8217; post</a> after I&#8217;d finished writing my own extension of <a href="http://blog.petermolgaard.com/2008/11/02/actionscript-3-enums/">Peter Moelgaard&#8217;s version</a>. Scott&#8217;s does nearly the same things mine does, but in a more C#-like way.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reversefold.com/blog/2010/07/22/java-style-typesafe-enumerations-in-as3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More Fractal Letters</title>
		<link>http://www.reversefold.com/blog/2009/02/10/more-fractal-letters/</link>
		<comments>http://www.reversefold.com/blog/2009/02/10/more-fractal-letters/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 19:16:03 +0000</pubDate>
		<dc:creator>papercrane</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Fractal]]></category>

		<guid isPermaLink="false">http://www.reversefold.com/blog/?p=123</guid>
		<description><![CDATA[I&#8217;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. The pictures link to the updated app. Enjoy, and as usual, don&#8217;t slide the slider up [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve continued adding bits to my <a title="Fractal Letters" href="http://www.reversefold.com/blog/2009/01/20/fractal-letters/">fractal letters</a> 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.</p>
<div id="attachment_125" class="wp-caption aligncenter" style="width: 560px"><img class="size-full wp-image-125" title="Fractal Email - first iteration" src="http://www.reversefold.com/blog/wp-content/uploads/2009/02/fractal_email_11.png" alt="fractal_email_11" width="550" height="298" /><p class="wp-caption-text">Fractal Email - first iteration</p></div>
<p style="text-align: left;">
<div id="attachment_127" class="wp-caption aligncenter" style="width: 560px"><img class="size-full wp-image-127" title="Fractal Email - third iteration" src="http://www.reversefold.com/blog/wp-content/uploads/2009/02/fractal_email_3.png" alt="Fractal Email - third iteration" width="550" height="295" /><p class="wp-caption-text">Fractal Email - third iteration</p></div>
<div id="attachment_128" class="wp-caption aligncenter" style="width: 560px"><img class="size-full wp-image-128" title="Fractal Email - fifth iteration" src="http://www.reversefold.com/blog/wp-content/uploads/2009/02/fractal_email_5.png" alt="Fractal Email - fifth iteration" width="550" height="294" /><p class="wp-caption-text">Fractal Email - fifth iteration</p></div>
<p>The pictures link to the updated app. Enjoy, and as usual, don&#8217;t slide the slider up to the max unless you&#8217;re willing to wait while your entire browser hangs for several minutes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reversefold.com/blog/2009/02/10/more-fractal-letters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASCIIMathML WordPress Plugin</title>
		<link>http://www.reversefold.com/blog/2009/02/06/asciimathml-wordpress-plugin/</link>
		<comments>http://www.reversefold.com/blog/2009/02/06/asciimathml-wordpress-plugin/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 07:57:55 +0000</pubDate>
		<dc:creator>papercrane</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[MathML]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.reversefold.com/blog/?p=121</guid>
		<description><![CDATA[I&#8217;ve updated my ASCIIMathML plugin so that it doesn&#8217;t have quite so many security holes and abuse vectors. I&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve updated my ASCIIMathML plugin so that it doesn&#8217;t have quite so many security holes and abuse vectors. I&#8217;ve put it up for download at <a href="http://www.reversefold.com/blog/asciimathml_0.2.tgz">http://www.reversefold.com/blog/asciimathml_0.2.tgz</a>. Please note that this requires command-line access to run, requires <a href="http://www.python.org/">python</a> and <a href="http://www.inkscape.org/">inkscape</a> 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.</p>
<p>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.</p>
<p>This is an update of a <a href="http://www.reversefold.com/blog/2008/11/27/pretty-equations-in-html/">previous post</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reversefold.com/blog/2009/02/06/asciimathml-wordpress-plugin/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Fractal Letters</title>
		<link>http://www.reversefold.com/blog/2009/01/20/fractal-letters/</link>
		<comments>http://www.reversefold.com/blog/2009/01/20/fractal-letters/#comments</comments>
		<pubDate>Tue, 20 Jan 2009 08:08:23 +0000</pubDate>
		<dc:creator>papercrane</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[FlexBuilder]]></category>
		<category><![CDATA[Fractal]]></category>
		<category><![CDATA[Papervision3D]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[l-system]]></category>

		<guid isPermaLink="false">http://www.reversefold.com/blog/?p=116</guid>
		<description><![CDATA[On the way home last Thursday I decided to try creating fractal letters. The idea was to replace each &#8220;line&#8221; 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 [...]]]></description>
			<content:encoded><![CDATA[<p>On the way home last Thursday I decided to try creating fractal letters. The idea was to replace each &#8220;line&#8221; 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 <a title="L-system" href="http://en.wikipedia.org/wiki/L-system">L-system</a>, of which the <a title="Dragon curve" href="http://en.wikipedia.org/wiki/Dragon_curve">Dragon curve</a> is one. After a weekend&#8217;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).</p>
<div id="attachment_117" class="wp-caption alignnone" style="width: 510px"><a href="http://www.reversefold.com/flex/fractalLetters/"><img class="size-full wp-image-117" title="Fractal Letters, iteration 1" src="http://www.reversefold.com/blog/wp-content/uploads/2009/01/fractalletters_1.png" alt="Fractal Letters, iteration 1" width="500" height="234" /></a><p class="wp-caption-text">Fractal Letters, iteration 1</p></div>
<p>And here is the fifth iteration:</p>
<div id="attachment_118" class="wp-caption alignnone" style="width: 510px"><a href="http://www.reversefold.com/flex/fractalLetters/"><img class="size-full wp-image-118" title="Fractal Letters, iteration 5" src="http://www.reversefold.com/blog/wp-content/uploads/2009/01/fractalletters_5.png" alt="Fractal Letters, iteration 5" width="500" height="251" /></a><p class="wp-caption-text">Fractal Letters, iteration 5</p></div>
<p>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&#8217;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.</p>
<p>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.</p>
<p>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.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reversefold.com/blog/2009/01/20/fractal-letters/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>μ-reversefold</title>
		<link>http://www.reversefold.com/blog/2009/01/07/%ce%bc-reversefold/</link>
		<comments>http://www.reversefold.com/blog/2009/01/07/%ce%bc-reversefold/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 18:58:23 +0000</pubDate>
		<dc:creator>papercrane</dc:creator>
				<category><![CDATA[Origami]]></category>
		<category><![CDATA[reversefold]]></category>

		<guid isPermaLink="false">http://www.reversefold.com/blog/?p=109</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<div class="wp-caption alignnone" style="width: 510px"><a href="http://www.flickr.com/photos/reversefold/3163437415/in/set-72157612124977697/"><img title="Finished creases, back side" src="http://farm2.static.flickr.com/1143/3163437415_4630acb71c.jpg?v=0" alt="Finished creases, back side" width="500" height="375" /></a><p class="wp-caption-text">Finished creases, back side</p></div>
<div class="wp-caption alignnone" style="width: 510px"><a href="http://www.flickr.com/photos/reversefold/3163442179/in/set-72157612124977697/"><img title="Finished creases, front side" src="http://farm2.static.flickr.com/1006/3163442179_449e713f2f.jpg?v=0" alt="Finished creases, front side" width="500" height="375" /></a><p class="wp-caption-text">Finished creases, front side</p></div>
<p>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.</p>
<div class="wp-caption alignnone" style="width: 510px"><a href="http://www.flickr.com/photos/reversefold/3163444651/in/set-72157612124977697/"><img title="Accordion halfway done" src="http://farm2.static.flickr.com/1036/3163444651_435af70c4c.jpg?v=0" alt="Accordion halfway done" width="500" height="375" /></a><p class="wp-caption-text">Accordion halfway done</p></div>
<div class="wp-caption alignnone" style="width: 510px"><a href="http://www.flickr.com/photos/reversefold/3163451773/in/set-72157612124977697/"><img title="Accordion finished" src="http://farm2.static.flickr.com/1106/3163451773_897441ae5f.jpg?v=0" alt="Accordion finished" width="500" height="375" /></a><p class="wp-caption-text">Accordion finished</p></div>
<p>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.</p>
<div class="wp-caption alignnone" style="width: 510px"><a href="http://www.flickr.com/photos/reversefold/3164284164/in/set-72157612124977697/"><img title="Five rows finished" src="http://farm2.static.flickr.com/1192/3164284164_c859b551a5.jpg?v=0" alt="Five rows finished" width="500" height="375" /></a><p class="wp-caption-text">Five rows finished</p></div>
<p>And after many painstaking hours of work, here is the finished product:</p>
<div class="wp-caption alignnone" style="width: 510px"><a href="http://www.flickr.com/photos/reversefold/3163461881/in/set-72157612124977697/"><img title="Finished fold" src="http://farm2.static.flickr.com/1130/3163461881_24c3366def.jpg?v=0" alt="Finished fold" width="500" height="375" /></a><p class="wp-caption-text">Finished fold</p></div>
<div class="wp-caption alignnone" style="width: 510px"><a href="http://www.flickr.com/photos/reversefold/3163459109/in/set-72157612124977697/"><img title="Finished fold, side view" src="http://farm2.static.flickr.com/1024/3163459109_0baea22192.jpg?v=0" alt="Finished fold, side view" width="500" height="375" /></a><p class="wp-caption-text">Finished fold, side view</p></div>
<div class="wp-caption alignnone" style="width: 510px"><a href="http://www.flickr.com/photos/reversefold/3163456711/in/set-72157612124977697/"><img title="Finished fold, 3/4 view" src="http://farm2.static.flickr.com/1087/3163456711_c642e49b18.jpg?v=0" alt="Finished fold, 3/4 view" width="500" height="375" /></a><p class="wp-caption-text">Finished fold, 3/4 view</p></div>
<p>And for a little perspective here is a shot of the finished fold compared to the original paper:</p>
<div class="wp-caption alignnone" style="width: 510px"><a href="http://www.flickr.com/photos/reversefold/3163466807/in/set-72157612124977697/"><img title="Finished fold vs. unfolded paper" src="http://farm4.static.flickr.com/3262/3163466807_08ca8fa83e.jpg?v=0" alt="Finished fold vs. unfolded paper" width="500" height="375" /></a><p class="wp-caption-text">Finished fold vs. unfolded paper</p></div>
<p>And just in case the quarter wasn&#8217;t enough, here&#8217;s the finished fold in my palm. Yes, it is that small. No, I won&#8217;t make you one. ;-)</p>
<div class="wp-caption alignnone" style="width: 510px"><a href="http://www.flickr.com/photos/reversefold/3164303556/in/set-72157612124977697/"><img title="Finished fold in the palm of my hand" src="http://farm4.static.flickr.com/3075/3164303556_d8fecc1739.jpg?v=0" alt="Finished fold in the palm of my hand" width="500" height="375" /></a><p class="wp-caption-text">Finished fold in the palm of my hand</p></div>
<p>All of these photos link to <a href="http://www.flickr.com/photos/reversefold/">my newly created flickr account</a>. The entire set is up there along with higher quality versions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reversefold.com/blog/2009/01/07/%ce%bc-reversefold/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pretty equations in HTML</title>
		<link>http://www.reversefold.com/blog/2008/11/27/pretty-equations-in-html/</link>
		<comments>http://www.reversefold.com/blog/2008/11/27/pretty-equations-in-html/#comments</comments>
		<pubDate>Fri, 28 Nov 2008 02:25:53 +0000</pubDate>
		<dc:creator>papercrane</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[ASCIIMathML]]></category>
		<category><![CDATA[DHTML]]></category>
		<category><![CDATA[MathML]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.reversefold.com/blog/?p=79</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>You may have seen the nice looking equations in <a href="/blog/2008/11/24/fitting-circles-within-a-larger-circle/">my last post</a>. As part of the process of writing that post I researched <a href="http://www.w3.org/Math/">MathML</a> 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&#8217;s widely installed. The second problem is that you can&#8217;t just put MathML in your web page and have Firefox interpret it. It turns out you need <a href="http://www1.chapman.edu/~jipsen/mathml/mathhtml/">a bit of Javascript</a> 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.</p>
<p>Enter <a href="http://www1.chapman.edu/~jipsen/mathml/asciimathsyntax.html">ASCIIMathML</a>, 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 <tt>y=x^2</tt>. A more complicated example from my last post such as:<br />
`arc=2*cos^-1(1-r_2^2/(2*r_1^2))*r_1`<br />
is written as:</p>
<pre>arc=2*cos^-1(1-r_2^2/(2*r_1^2))*r_1</pre>
<p>Another example with a square root:<br />
`y=(+-r_2*sqrt(4*r_1^2-r_2^2))/(2*r_1)`</p>
<pre>y=(+-r_2*sqrt(4*r_1^2-r_2^2))/(2*r_1)</pre>
<p>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&#8217;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 <a href="http://www.oldschool.com.sg/index.php/module/Shared/action/Static/tmpl/ASCIIMathPHP">ASCHIIMathPHP</a>, a version of ASCIIMathML.js to PHP. The resulting MathML has the times symbol replace by one which the later tools understand (replace &amp;#8901; with &amp;#183;). The resulting MathML is then passed to <a href="http://www.grigoriev.ru/svgmath/">svgmath</a>, a python script which converts MathML to SVG. SVG would be great but Internet Explorer still doesn&#8217;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.<br />
Here&#8217;s some sample PHP to run these commands:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ascii</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'y=(+-r_2*sqrt(4*r_1^2-r_2^2))/(2*r_1)'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$pngFile</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ascii</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.png'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pngFile</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'ASCIIMathPHP-2.0.cfg.php'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'ASCIIMathPHP-2.0.class.php'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$ascii_math</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ASCIIMathPHP<span style="color: #009900;">&#40;</span><span style="color: #000088;">$symbol_arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$ascii_math</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>setExpr<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ascii</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$ascii_math</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>genMathML<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$mathml</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ascii_math</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getMathML<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$mathml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'⋅'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'·'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mathml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$mathmlFile</span> <span style="color: #339933;">=</span> <span style="color: #990000;">tempnam</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">sys_get_temp_dir</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'mathml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$td</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mathmlFile</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$td</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mathml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$svgFile</span> <span style="color: #339933;">=</span> <span style="color: #990000;">tempnam</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">sys_get_temp_dir</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'svg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">system</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/usr/bin/env python math2svg.py -s <span style="color: #006699; font-weight: bold;">$mathmlFile</span> &amp;gt; <span style="color: #006699; font-weight: bold;">$svgFile</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #990000;">system</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;inkscape -f <span style="color: #006699; font-weight: bold;">$svgFile</span> -e <span style="color: #006699; font-weight: bold;">$pngFile</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: image/png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pngFile</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>All of this is also wrapped up in a WordPress plugin which includes the JS file in the header. Unfortunately, I don&#8217;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&#8217;ll send you more information.</p>
<p><strong>UPDATE</strong>: I finally did update the plugin to be useful for others and <a href="http://www.reversefold.com/blog/2009/02/06/asciimathml-wordpress-plugin/">posted it</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reversefold.com/blog/2008/11/27/pretty-equations-in-html/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Fitting circles within a larger circle</title>
		<link>http://www.reversefold.com/blog/2008/11/24/fitting-circles-within-a-larger-circle/</link>
		<comments>http://www.reversefold.com/blog/2008/11/24/fitting-circles-within-a-larger-circle/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 20:38:25 +0000</pubDate>
		<dc:creator>papercrane</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[algorithms]]></category>

		<guid isPermaLink="false">http://www.reversefold.com/blog/?p=62</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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&#8217;s radius in from the edge of the large circle. This looks something like this:</p>
<div id="attachment_63" class="wp-caption alignleft" style="width: 261px"><a href="http://www.reversefold.com/blog/wp-content/uploads/2008/11/circles-within-a-circle.png"><img class="size-medium wp-image-63" title="circles-within-a-circle" src="http://www.reversefold.com/blog/wp-content/uploads/2008/11/circles-within-a-circle.png" alt="Circles within a circle" width="251" height="250" /></a><p class="wp-caption-text">Circles within a circle</p></div>
<p>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.</p>
<div id="attachment_88" class="wp-caption alignnone" style="width: 406px"><a href="http://www.reversefold.com/blog/wp-content/uploads/2008/11/geometry1.png"><img src="http://www.reversefold.com/blog/wp-content/uploads/2008/11/geometry1.png" alt="Geometric definition" title="Geometric definition" width="396" height="343" class="size-full wp-image-88" /></a><p class="wp-caption-text">Geometric definition</p></div>
<ul>
<li>The large circle is centered at the origin</li>
<li>`r_1` is the radius of the large circle</li>
<li>`C` is the center of the small circle</li>
<li>`r_2` is the radius of the large circle</li>
<li>`I` are the interseciton points between the circles</li>
<li>`theta` is the angle between the x axis and the line from the origin to `I`</li>
<li>`arc` is the length of the arc between the two interseciton points (`I`)</li>
</ul>
<h3>Given</h3>
<p>`C=(r_1,0)`<br />
`I=(x,+-y)`<br />
`theta=cos^-1(x/r_1)`<br />
`arc=2*theta*r_1`</p>
<h3>Circle Equations</h3>
<p>`x^2+y^2=r_1^2`<br />
`(x-r_1)^2+y^2=r_2^2`</p>
<h3>Solve for `y^2`</h3>
<p>`y^2=r_1^2-x^2`<br />
`y^2=r_2^2-(x-r_1)^2`</p>
<h3>Set equal and solve for `x`</h3>
<p>`r_2^2-(x-r_1)^2=r_1^2-x^2`<br />
`x^2-(x^2-2*x*r_1+r_1^2)=r_1^2-r_2^2`<br />
`2*x*r_1-r_1^2=r_1^2-r_2^2`<br />
`2*x*r_1=2*r_1^2-r_2^2`<br />
`x=(2*r_1^2-r_2^2)/(2*r_1)`<br />
`x=r_1-r_2^2/(2*r_1)`</p>
<h3>`theta` is then:</h3>
<p>`theta=cos^-1((r_1-r_2^2/(2*r_1))/r_1)`<br />
`theta=cos^-1(1-r_2^2/(2*r_1^2))`</p>
<p>You could then use `theta` directly but this will leave you with a remainder of space that&#8217;s empty in the arc. To make the circles equally spaced you need the arc length.</p>
<h3>Replace values for a full `arc` formula</h3>
<p>`arc=2*cos^-1(1-r_2^2/(2*r_1^2))*r_1`</p>
<p>You can then divide the large circle&#8217;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.</p>
<h3>`y` is not needed for the solution, but comes out to be:</h3>
<p>`y=(+-r_2*sqrt(4*r_1^2-r_2^2))/(2*r_1)`</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reversefold.com/blog/2008/11/24/fitting-circles-within-a-larger-circle/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Falling Away</title>
		<link>http://www.reversefold.com/blog/2008/10/28/falling-away/</link>
		<comments>http://www.reversefold.com/blog/2008/10/28/falling-away/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 18:33:06 +0000</pubDate>
		<dc:creator>papercrane</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[bitmapdata]]></category>
		<category><![CDATA[flash 10]]></category>

		<guid isPermaLink="false">http://www.reversefold.com/wordpress/?p=49</guid>
		<description><![CDATA[I had a new idea last night on my way home for a simple use of Flash 10&#8242;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 [...]]]></description>
			<content:encoded><![CDATA[<p>I had a new idea last night on my way home for a simple use of Flash 10&#8242;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.</p>
<p><a href="http://www.reversefold.com/flash/falling_away/"><img class="alignnone" title="Falling Away Screenshot" src="http://www.reversefold.com/flash/falling_away/screenshot.jpg" alt="" width="440" height="331" /></a></p>
<p>This screenshot shows the app running with a flickr satellite photo feed displaying on each falling plane. Unfortunately, since my code uses <tt>BitmapData.draw()</tt>, this won&#8217;t work from the web as I don&#8217;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 <tt>USE_FLICKR = true</tt> you can see the full effect.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reversefold.com/blog/2008/10/28/falling-away/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flash 10 Camp, 3D, Sound Creation, and IK</title>
		<link>http://www.reversefold.com/blog/2008/10/12/flash-10-camp-3d-sound-creation-and-ik/</link>
		<comments>http://www.reversefold.com/blog/2008/10/12/flash-10-camp-3d-sound-creation-and-ik/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 04:54:02 +0000</pubDate>
		<dc:creator>papercrane</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[flash 10]]></category>
		<category><![CDATA[flashcamp]]></category>
		<category><![CDATA[ik]]></category>
		<category><![CDATA[sound]]></category>

		<guid isPermaLink="false">http://www.reversefold.com/wordpress/?p=45</guid>
		<description><![CDATA[I&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just come home from <a title="FlashCamp" href="http://www.flashcamp.org/">Flash(10)Camp</a> 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 <a title="Flash Tones" href="http://www.reversefold.com/flashCamp/flashTones/">Flash Tones</a>.</p>
<p><a href="http://www.reversefold.com/flashCamp/flashTones/"><img class="alignnone" title="Flash Tones" src="http://www.reversefold.com/flashCamp/flashTones/screenshot.png" alt="" width="438" height="318" /></a></p>
<p>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&#8217;ll need Flash Player 10 and a web cam if you want to try it out.</p>
<p>All of the features I saw in Flash Player 10 were ones I&#8217;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&#8217;re expecting. Of course, I&#8217;m assuming you know almost nothing about the specifics of sound, like I do.</p>
<p>IK is fun, but I I focused on the other 2 as I didn&#8217;t have any IK idea that seemed particularly interesting.</p>
<p>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:</p>
<p><a href="http://www.reversefold.com/flashCamp/knockoutSquares/"><img class="alignnone" title="Knockout Squares" src="http://www.reversefold.com/flashCamp/knockoutSquares/screenshot.png" alt="" width="437" height="316" /></a></p>
<p><a href="http://www.reversefold.com/flashCamp/redSquares/"><img class="alignnone" title="Red Squares" src="http://www.reversefold.com/flashCamp/redSquares/screenshot.png" alt="" width="435" height="318" /></a></p>
<p>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 <a title="Sobel Edge Detection" href="http://lukewalsh.co.uk/blog/2008/06/sobel-edge-detection-in-flash.html">Luke Walsh&#8217;s post</a>), added some more blending and filters and voila!</p>
<p><a href="http://www.reversefold.com/flashCamp/blueSquares/"><img class="alignnone" title="Blue Squares" src="http://www.reversefold.com/flashCamp/blueSquares/screenshot.png" alt="" width="437" height="316" /></a></p>
<p>I was quite proud of that and still think it looks cool. I wish I&#8217;d shown it earlier at the Hackathon but then again we won an award so I guess I&#8217;m just being greedy.</p>
<p>As an added bonus, here&#8217;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.</p>
<p><a href="http://www.reversefold.com/flashCamp/flashTones/warble/"><img class="alignnone" title="Warbling Flash Tones" src="http://www.reversefold.com/flashCamp/flashTones/warble/screenshot.png" alt="" width="437" height="317" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.reversefold.com/blog/2008/10/12/flash-10-camp-3d-sound-creation-and-ik/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flex Bug: Icon shadows in a TabNavigator&#8217;s Tabs</title>
		<link>http://www.reversefold.com/blog/2008/05/27/flex-bug-icon-shadows-in-a-tabnavigators-tabs/</link>
		<comments>http://www.reversefold.com/blog/2008/05/27/flex-bug-icon-shadows-in-a-tabnavigators-tabs/#comments</comments>
		<pubDate>Wed, 28 May 2008 01:59:36 +0000</pubDate>
		<dc:creator>papercrane</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[icon]]></category>
		<category><![CDATA[tabnavigator]]></category>

		<guid isPermaLink="false">http://www.reversefold.com/wordpress/?p=42</guid>
		<description><![CDATA[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 &#8220;shadow&#8221; icon at the top-left [...]]]></description>
			<content:encoded><![CDATA[<p>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 &#8220;shadow&#8221; icon at the top-left of the tabs. See the bug for more: <a href="https://bugs.adobe.com/jira/browse/SDK-15656">SDK-15656</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.reversefold.com/blog/2008/05/27/flex-bug-icon-shadows-in-a-tabnavigators-tabs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
