<?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>Random Meditations</title>
	<atom:link href="http://blog.melihelibol.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://blog.melihelibol.com</link>
	<description>on pertinent substance, and persistent deliberation on quaint curiosities.</description>
	<lastBuildDate>Tue, 13 Jul 2010 15:09:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Papervision3D Camera3D unprojectMatrix function</title>
		<link>http://blog.melihelibol.com/?p=25</link>
		<comments>http://blog.melihelibol.com/?p=25#comments</comments>
		<pubDate>Fri, 24 Oct 2008 07:29:14 +0000</pubDate>
		<dc:creator>H</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[camera]]></category>
		<category><![CDATA[camera3D]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[papervision]]></category>
		<category><![CDATA[Papervision3D]]></category>
		<category><![CDATA[unproject]]></category>

		<guid isPermaLink="false">http://blog.computerelibol.com/?p=25</guid>
		<description><![CDATA[During my studies in 3D Mathematics, I have mostly found that projection is achieved with a view frustum described as a matrix which involves variables near/far plane. This isn&#8217;t how projection in papervision is done by default, so, this set me on to a new path for unprojecting coordinates. This new path uses the perspective [...]]]></description>
			<content:encoded><![CDATA[<p>During my studies in 3D Mathematics, I have mostly found that projection is achieved with a view frustum described as a matrix which involves variables near/far plane. This isn&#8217;t how projection in papervision is done by default, so, this set me on to a new path for unprojecting coordinates. This new path uses the perspective matrix.<br />
<span id="more-25"></span><br />
<strong>Without perspective matrix</strong><br />
I first wanted to use the unproject function to unproject an xy plane to a z position in camera space. What I ended up doing was &#8220;untransforming&#8221; my coordinates back to the mX/mY parameters, given focus as z. This alone caused me to abandon this approach, since if I were doing this, I may as well override unproject and rewrite it to achieve what the Coordinate Utility Class currently does since &#8220;untransforming&#8221; is an extra and unoptimal step in achieving my goal. Bless that utility class, I am quite sure it will never be used, but nonetheless may have served as knowledgeable a prospect to others as it has to me. If you know of a better way to do this that does not involve &#8220;untransforming&#8221; unprojected coordinates, please let me know.</p>
<p><strong>With perspective matrix</strong><br />
Following is a function that will unproject an xy plane with variable z in camera space coordinates. This only works if useProjectionMatrix is set to true. With the projection matrix used to project coordinates in your world, we can use unproject techniques used in other 3D engines and/or languages:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> unprojectMatrix<span style="color: #000000;">&#40;</span>screen<span style="color: #000000; font-weight: bold;">:</span>Number3D<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>Number3D <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>useProjectionMatrix<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> s4<span style="color: #000000; font-weight: bold;">:</span>Quaternion = <span style="color: #0033ff; font-weight: bold;">new</span> Quaternion<span style="color: #000000;">&#40;</span>screen.<span style="color: #004993;">x</span>, <span style="color: #000000; font-weight: bold;">-</span>screen.<span style="color: #004993;">y</span>, screen.z<span style="color: #000000; font-weight: bold;">+</span><span style="color: #004993;">focus</span><span style="color: #000000;">&#41;</span>;
        	<span style="color: #6699cc; font-weight: bold;">var</span> up4<span style="color: #000000; font-weight: bold;">:</span>Quaternion = Quaternion.createFromMatrix<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>._projection<span style="color: #000000;">&#41;</span>;
        	up4 = Quaternion.multiply<span style="color: #000000;">&#40;</span>s4, up4<span style="color: #000000;">&#41;</span>;
        	<span style="color: #6699cc; font-weight: bold;">var</span> up3<span style="color: #000000; font-weight: bold;">:</span>Number3D = <span style="color: #0033ff; font-weight: bold;">new</span> Number3D<span style="color: #000000;">&#40;</span>up4.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">/</span>up4.w, up4.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">/</span>up4.w, up4.z<span style="color: #000000; font-weight: bold;">/</span>up4.w<span style="color: #000000;">&#41;</span>;
        	Matrix3D.multiplyVector3x3<span style="color: #000000;">&#40;</span><span style="color: #004993;">transform</span>, up3<span style="color: #000000;">&#41;</span>;
        	<span style="color: #0033ff; font-weight: bold;">return</span> up3;
	<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">return</span> unproject<span style="color: #000000;">&#40;</span>screen.<span style="color: #004993;">x</span>, screen.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> getTrueScaleDistance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #004993;">focus</span><span style="color: #000000; font-weight: bold;">*</span>zoom<span style="color: #000000; font-weight: bold;">-</span><span style="color: #004993;">focus</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>You can add these functions to an extended camera with projectionMatrix turned on, and it will unproject your coordinates to the xy plane at a given z depth described with the screen:Number3D parameter. You will need to make private var _projection protected. The point of this is to expose the projection matrix to the function and can also be exposed if the function is added to the Camera3D class directly.</p>
<p>A sample use of the extended camera:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> zDepth<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = camera.getTrueScaleDistance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> screen<span style="color: #000000; font-weight: bold;">:</span>Number3D = <span style="color: #0033ff; font-weight: bold;">new</span> Number3D<span style="color: #000000;">&#40;</span>viewPort.containerSprite.<span style="color: #004993;">mouseX</span>, viewPort.containerSprite.<span style="color: #004993;">mouseY</span>, zDepth<span style="color: #000000;">&#41;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> ray<span style="color: #000000; font-weight: bold;">:</span>Number3D = camera.unprojectMatrix<span style="color: #000000;">&#40;</span>screen<span style="color: #000000;">&#41;</span>;
ray= Number3D.<span style="color: #004993;">add</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> Number3D<span style="color: #000000;">&#40;</span>camera.<span style="color: #004993;">x</span>, camera.<span style="color: #004993;">y</span>, camera.z<span style="color: #000000;">&#41;</span>, ray<span style="color: #000000;">&#41;</span>;
plane.<span style="color: #004993;">x</span> = ray.<span style="color: #004993;">x</span>, plane.<span style="color: #004993;">y</span> = ray.<span style="color: #004993;">y</span>, plane.z = ray.z;</pre></div></div>

<p>Where camera is a subclass of Camera3D with the adjustments outlined above.</p>
<p><strong>What is happening?</strong><br />
This is something I realized I would need before I found out how to do. When I was reading about <a href="http://en.wikipedia.org/wiki/Quaternion" target="_blank">Quaternions</a>, it explained why and how <a href="http://en.wikipedia.org/wiki/William_Rowan_Hamilton" target="_blank">William Rowan Hamilton</a> realized that he needed them to begin with. I found that a Quaternion thought of as a <a href="http://en.wikipedia.org/wiki/Division_ring" target="_blank">division ring</a> made more sense in the context of 3D math. We divide the x,y,z components of our 4D object (Quaternion) by the 4th dimension, <strong>w</strong>, after rotating our space in 4D. I think of w as a piece of information that carries the proportion of the lower dimensions so that by dividing them by w restores this proportion to renderable 3D coordinates. I have generalized this process to n-dimensions, maybe incorrectly. I will find out soon enough. If you have been with me since the start of my 3D math journey, you can think of w as perspective, and 1/w as equal to how we negated perspective.</p>
<p>That&#8217;s about where I&#8217;m at right now. <a href="http://blog.computerelibol.com/download/sample/unprojectMatrix.zip">Here is some sample code that does this</a>. The source is intended to be compiled with the Flex compiler, but can be compiled by making Main the document object of a .fla document.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.melihelibol.com/?feed=rss2&amp;p=25</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>A brief study on Cosine Generators</title>
		<link>http://blog.melihelibol.com/?p=19</link>
		<comments>http://blog.melihelibol.com/?p=19#comments</comments>
		<pubDate>Sat, 18 Oct 2008 17:06:39 +0000</pubDate>
		<dc:creator>H</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[cosine]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[trigonometry]]></category>

		<guid isPermaLink="false">http://blog.computerelibol.com/?p=19</guid>
		<description><![CDATA[Recently I have been studying rotations. I was particularly interested in the primitive mathematics involved in the conversion between Polar and Cartesian coordinate systems. Usually you are told to use the trig functions to do this. What was actually happening always seemed hidden in the implementation of the trig functions, so I took the time [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I have been studying rotations. I was particularly interested in the primitive mathematics involved in the conversion between Polar and Cartesian coordinate systems. Usually you are told to use the trig functions to do this. What was actually happening always seemed hidden in the implementation of the trig functions, so I took the time to implement a cosine generator based on a report a man by the name of Dr. William D. Bishop had come up with.<br />
<span id="more-19"></span><br />
Although this is just a study of the implementation of just one of these functions, it has changed the way I think of trigonometry, and even mathematics.</p>
<p>Here is a cosine generator that Dr. Bishop says is faster than the traditional <a href="http://en.wikipedia.org/wiki/CORDIC" target="_blank">CORDIC</a> algorithm. This algorithm was written for hardware implementation. This is purely for educational purposes and is rather useless &#8211; it is an order of magnitude slower than the AVM internal cosine function.</p>
<p><strong>Actionscript benchmark implementation</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> computeSeries<span style="color: #000000;">&#40;</span>precision<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">12</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> r<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span>;
    <span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> n<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>;<span style="color: #000000; font-weight: bold;">++</span>n <span style="color: #000000; font-weight: bold;">&amp;</span>lt; precision ;<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        r<span style="color: #000000;">&#91;</span>n<span style="color: #000000;">&#93;</span> = <span style="color: #004993;">Math</span>.<span style="color: #004993;">pow</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>, n<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>factorial<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">*</span>n<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> r;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> factorial<span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">&amp;</span>gt;<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">return</span> i<span style="color: #000000; font-weight: bold;">*</span><span style="color: #004993;">arguments</span>.<span style="color: #004993;">callee</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000; font-weight:bold;">1</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> cosFunc<span style="color: #000000;">&#40;</span>a<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> r<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">0</span>;
    <span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> n<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>;<span style="color: #000000; font-weight: bold;">++</span>n <span style="color: #000000; font-weight: bold;">&amp;</span>lt; series.<span style="color: #004993;">length</span> ;<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        r <span style="color: #000000; font-weight: bold;">+</span>= series<span style="color: #000000;">&#91;</span>n<span style="color: #000000;">&#93;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">pow</span><span style="color: #000000;">&#40;</span>a, <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">*</span>n<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> r;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> series<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = computeSeries<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">12</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> startTime<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #004993;">flash.utils</span>.<span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>;<span style="color: #000000; font-weight: bold;">++</span>i<span style="color: #000000; font-weight: bold;">&amp;</span>lt;<span style="color: #000000; font-weight:bold;">1000000</span>;<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    <span style="color: #009900;">//Math.cos(Math.PI);</span>
    cosFunc<span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//whoops wicked slow.</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">-</span>startTime<span style="color: #000000;">&#41;</span>;</pre></div></div>

<p><strong>The Math</strong><br />
<a href="http://www.dgp.toronto.edu/~derek/cosine_report.pdf">http://www.dgp.toronto.edu/~derek/cosine_report.pdf</a></p>
<p><strong>Concrete knowledge</strong><br />
In this study, it is clear to me why and how sine/cosine are functions of radians. They are directly related to PI since a radian is the unit measure of angles to the limit of PI, and the result of sine/cosine can be thought of as a normalized axis coordinate. On the other hand, normalizing a scalar can get us the radian measure of an angle using the inverse trigonometric functions &#8211; arc sine/cosine etc. This isn&#8217;t anything I did not already know, but I am beginning to understand how systems like Trigonometry work with normal quantities.</p>
<p><strong>Conclusion</strong><br />
The implementation of trigonometric functions has been a question mark in my head for quite some time. When I was studying trigonometry, I looked for how the functions were implemented. All I could find was how to use them or what they do and all of the annoying identities and proofs that it works. The implementation of trigonometric functions is never really talked about in mathematics. I have learned that other concepts in mathematics are able to describe Trigonometry in general terms. Viewing it in general terms has both helped me understand and properly place the system and the mechanics it is composed of.</p>
<p><strong>Other thoughts</strong><br />
PI as they say keeps everything together in the universe &#8211; but in order to protect myself from narrow mindedness, I am trying to think of PI as a result of a certain geometric way of interpreting and measuring space. I haven&#8217;t done enough studying to be sure of this, but I plan to, and in the mean time, this will be my approach to higher maths. I am hoping it will help me avoid getting trapped in formulating useless analogies. If anyone has any guidance on this, please leave a comment <img src='http://blog.melihelibol.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.melihelibol.com/?feed=rss2&amp;p=19</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Coordinate Utility class for Papervision 3D</title>
		<link>http://blog.melihelibol.com/?p=21</link>
		<comments>http://blog.melihelibol.com/?p=21#comments</comments>
		<pubDate>Fri, 10 Oct 2008 00:54:25 +0000</pubDate>
		<dc:creator>H</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[coordinate spaces]]></category>
		<category><![CDATA[coordinates]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[papervision]]></category>
		<category><![CDATA[position]]></category>
		<category><![CDATA[scale]]></category>

		<guid isPermaLink="false">http://blog.computerelibol.com/?p=21</guid>
		<description><![CDATA[This class factors out a lot of work that needs to be done to position 3D objects at variable rotations, world coordinates, and screen distances. Use the link below to get the utility class. Using this class requires papervision. If you don&#8217;t already have papervision, then you probably don&#8217;t need this class&#8230; Also, the sample [...]]]></description>
			<content:encoded><![CDATA[<p>This class factors out a lot of work that needs to be done to position 3D objects at variable rotations, world coordinates, and screen distances. Use the link below to get the utility class. Using this class requires papervision. If you don&#8217;t already have papervision, then you probably don&#8217;t need this class&#8230; Also, the sample isn&#8217;t pretty (graphically), and the source has some weird key controls. They are completely unintuitive but get the job done for testing <img src='http://blog.melihelibol.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<span id="more-21"></span><br />
<a href="http://blog.computerelibol.com/download/sample/CoordinateUtilitySample.zip">Coordinate Utility class with sample source</a><br />
<a href="http://blog.computerelibol.com/demo/CoordinateUtility" target="_blank">Coordinate Utility demo</a></p>
<p><strong>Sample</strong><br />
Here is an API snippet to give you an idea of what it does. The following code will reposition a plane to the coordinates defined by the screenCoordinates object. It will also render the plane in the scale of the movieclip material source.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//gets the distance the plane should be so the movieclip renders at its actual scale.</span>
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">distance</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = CoordinateUtil.getTrueScaleDistance<span style="color: #000000;">&#40;</span>camera<span style="color: #000000;">&#41;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> screenCoordinates<span style="color: #000000; font-weight: bold;">:</span>Number2D = <span style="color: #0033ff; font-weight: bold;">new</span> Number2D<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span>, <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//based on a centered registration point</span>
CoordinateUtil.setObjectByScreenCoordinates<span style="color: #000000;">&#40;</span>camera, plane, screenCoordinates, <span style="color: #004993;">distance</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p><strong>History</strong><br />
Some time ago I wrote an article titled <a href="http://blog.computerelibol.com/?p=3">Plane Scaling and Positioning with Papervision.</a> I shared the solutions I had used to over come some of the challenges us RIA developers face when developing 3D interfaces. Some time later I was asked about how to solve these problems under different conditions.</p>
<p>Well, I know this is a tough thing for most flash developers to accomplish. I know your boss is expecting you to do it and to do it right. I know we need to be able to do this stuff just by the way the typical web shop work flow goes. The designer design something and gives you a psd. Great. Now how are we suppose to precisely implement all of these 3D transformations without iterations of trial and error? I think this is something our community needs.</p>
<p>Sorry it took so long for those who have asked about this. Working on this, and related projects, I have fallen into knowledge tangents about 3D math (and just plain math and physics in general) in the past couple of months. It has made me even more knowledge hungry. It&#8217;s this particular issue of making papervision practical for interfaces, my interest in particle physics, FP10, and the video game I&#8217;m working on using another open source rendering engine in C++ that has really been taking up every second of my life. What can I say, I really love this stuff. And, sadly, lately I&#8217;ve just had nothing better to do.</p>
<p>Some of the more advanced readers might be wondering &#8220;Umm&#8230; Why didn&#8217;t you use unproject?&#8221; Well, to be honest, because it just did not do as much as I needed it to do. It was also slightly inaccurate. I didn&#8217;t spend much time with it. It&#8217;s entirely possible that I may have just been using it improperly.</p>
<p>That&#8217;s it for now. Hope this helps some of you out there! Please post any bugs that you might come across. It is absolutely possible that I will update this article as papervision evolves or if I, or you, come up with better (more optimal) techniques for this class.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.melihelibol.com/?feed=rss2&amp;p=21</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>FileReference &#8211; Can I get an IO Error?</title>
		<link>http://blog.melihelibol.com/?p=17</link>
		<comments>http://blog.melihelibol.com/?p=17#comments</comments>
		<pubDate>Fri, 15 Aug 2008 05:00:47 +0000</pubDate>
		<dc:creator>H</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[Actionscript 3.0]]></category>
		<category><![CDATA[bug?]]></category>
		<category><![CDATA[FileReference]]></category>
		<category><![CDATA[IO Error]]></category>

		<guid isPermaLink="false">http://blog.computerelibol.com/?p=17</guid>
		<description><![CDATA[It&#8217;s funny, actually, recently I was trying to remember which class was doing what I&#8217;m about to explain, and about the same time it happened.
If I had to choose, FileReference would be the strange fruit of the actionscript 3.0 classes. Only if I had to choose, though&#8230; I mean, I&#8217;m in a life or death [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s funny, actually, recently I was trying to remember which class was doing what I&#8217;m about to explain, and about the same time it happened.</p>
<p>If I had to choose, FileReference would be the strange fruit of the actionscript 3.0 classes. Only if I had to choose, though&#8230; I mean, I&#8217;m in a life or death situation, and my only way out is to choose which class is the strangest. In that case I would choose FileReference. Some might choose the Sound class, but me, yea, definitely FileReference&#8230;<span id="more-17"></span></p>
<p>Ok, so it goes like this: if you define FileReference inside the same function body that you call download()/upload() without declaring a nested function &#8211; it will fail. If you&#8217;re lucky, you&#8217;ll get an IO Error&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> fail<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> urlRequest<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">URLRequest</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLRequest</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;http://www.lataco.com/taco_man.jpg&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #6699cc; font-weight: bold;">var</span> fileRef<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">FileReference</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">FileReference</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	fileRef.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">COMPLETE</span>, onComplete<span style="color: #000000;">&#41;</span>;
	fileRef.<span style="color: #004993;">download</span><span style="color: #000000;">&#40;</span>urlRequest<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> onComplete<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;It's complete&quot;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>But, this works:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> fail<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> urlRequest<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">URLRequest</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLRequest</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;http://www.lataco.com/taco_man.jpg&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #6699cc; font-weight: bold;">var</span> fileRef<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">FileReference</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">FileReference</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	fileRef.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">COMPLETE</span>, onComplete<span style="color: #000000;">&#41;</span>;
	fileRef.<span style="color: #004993;">download</span><span style="color: #000000;">&#40;</span>urlRequest<span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #6699cc; font-weight: bold;">var</span> succeed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Function</span> = <span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> onComplete<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;It's complete&quot;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Why? Beats me. I can only tell you how it works: If you&#8217;ve declared the FileReference in the same scope as the function calling upload()/download(), there needs to be a defined function declaration on the same scope for it to work.</p>
<p>I figure it has to do with referencing a location on your file system and garbage collection. Something about a nested function&#8230;</p>
<p>When it fails, sometimes it dispatches an IOErrorEvent, sometimes it does not. Not quite sure why.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.melihelibol.com/?feed=rss2&amp;p=17</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>CNN Fails Flash 10 Detection</title>
		<link>http://blog.melihelibol.com/?p=16</link>
		<comments>http://blog.melihelibol.com/?p=16#comments</comments>
		<pubDate>Tue, 05 Aug 2008 14:52:44 +0000</pubDate>
		<dc:creator>H</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[adobe]]></category>
		<category><![CDATA[cnn]]></category>
		<category><![CDATA[detection]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flash player]]></category>
		<category><![CDATA[flash player 10]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[swfobject]]></category>

		<guid isPermaLink="false">http://blog.computerelibol.com/?p=16</guid>
		<description><![CDATA[A friend posted an article about how the flash detection on CNN is failing for Flash Player 10 on certain browsers. This immediately sparked my interest as I have taken it upon myself to make sure he understands that Flash Player isn&#8217;t just another pile of proprietary software.
Anyways, I checked it out, and my first [...]]]></description>
			<content:encoded><![CDATA[<p>A friend posted an <a href="http://pandion.ferrus.net/2008/08/02/apparently-10-is-not-greater-than-8" target="_blank">article</a> about how the flash detection on CNN is failing for Flash Player 10 on certain browsers. This immediately sparked my interest as I have taken it upon myself to make sure he understands that Flash Player isn&#8217;t just another pile of proprietary software.<span id="more-16"></span></p>
<p>Anyways, I checked it out, and my first immediate guess was that detection was failing due to the browser he was using, but I realized what it was more likely to be when I drifted off into a thought tangent at work. 10 <strong>is</strong> less than 9 when they are compared as strings. I figured, if this just occurred, then all this time their detection script was succeeding because comparing integers 0-9 as strings yields the same result as comparing them as integers.</p>
<p>I&#8217;m pretty certain of this, I actually went ahead and looked at the source:</p>
<p>http://i.cdn.turner.com/cnn/.element/js/2.0/main.js</p>
<p>and had a look at the class CNN_FlashDetect, and specifically the following function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">CNN_FlashDetect.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">detectVersion</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span> num <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> isVersionSupported <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> isNaN<span style="color: #009900;">&#40;</span> num <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		isVersionSupported <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getVersion</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> parseInt<span style="color: #009900;">&#40;</span> num <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #006600; font-style: italic;">//this alert was added by me</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getVersion</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> num<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getVersion</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>num<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> isVersionSupported<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//10.0.0,9,string,number,false</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span> isVersionSupported <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This script is designed to just test major versions as parseInt(num) hints at. Unfortunately, &#8220;10.0.0&#8243; is not being converted to a number, which seems like something that should have been done at getVersion() by virtue of detectVersion()&#8217;s task. It isn&#8217;t actually a valid number, so it is not converted for >=. Instead, 9 is converted to a string for comparison, which is the crux of the issue. I tested this script in IE. Running FP10 on IE6, the script reports 8 as my flash player version and is a number&#8230;</p>
<p>Consistency makes me all fuzzy inside.</p>
<p>Now, I know this site works on IE6, and that is because it&#8217;s actually testing for version 8 instead of 9 as the minimum version.</p>
<p>As far as I can tell, the site is actually loading the adobe detection script &#8211; which also doesn&#8217;t seem to be working. The detection script returns -1 when it tries to detect flash for IE6 when running fp10. I haven&#8217;t tested it for other versions, but I would figure it would do the same. Apparently the version you have installed precedes the existence of flash player. I tested some vars, and it looks like the script is failing to initialize bools that allow the detection scripts to determine which browser they&#8217;re in. None of the detection conditions are met, so it returns -1. This doesn&#8217;t seem to have anything to do with flash player 10.</p>
<p>The CNN script tries 3 detection techniques, all of which are dynamic with limits &#8211; js/vb for active x, and a word search for plugin flash player. For active x, the script tries to create different versions of the flash player in a try catch, and when it succeeds, the iterator is used as the player version, so we get a number in IE, which is typically 8 which is the upper bound of the function. For plugins, it does a word search on the description property of the plugin and uses isNaN to test if parseInt() succeeded in converting a word to a number. When this succeeds though the assigned variable to the result is not parsed, so we get a string&#8230;</p>
<p>The truth is, &#8220;10.0.0&#8243; IS < &#8220;9&#8243;. It turns out that the issue is implementation specific. It has nothing to do with browsers or the flash player&#8230; !</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.melihelibol.com/?feed=rss2&amp;p=16</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Actionscript Permutation Generator</title>
		<link>http://blog.melihelibol.com/?p=9</link>
		<comments>http://blog.melihelibol.com/?p=9#comments</comments>
		<pubDate>Thu, 17 Jul 2008 04:24:28 +0000</pubDate>
		<dc:creator>H</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[Actionscript 3.0]]></category>
		<category><![CDATA[Combinatorics]]></category>
		<category><![CDATA[Factoradic]]></category>
		<category><![CDATA[Factorial]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Permutation]]></category>

		<guid isPermaLink="false">http://blog.computerelibol.com/?p=9</guid>
		<description><![CDATA[For those of you who just need a permutation generator:
Actionscript 3.0 Permutation Utility class
Actionscript 2.0 Permutation Utility class
Onward&#8230;
Some time ago on a tech interview website discussion, we were challenged with the following problem:
Given a number N generate all the permutations e.g. N=3
1 2 3
1 3 2
2 1 3
2 3 1
3 2 1
3 1 2
How to [...]]]></description>
			<content:encoded><![CDATA[<p>For those of you who just need a permutation generator:<br />
<a href="http://blog.computerelibol.com/download/as3/PermutationUtil.as">Actionscript 3.0 Permutation Utility class</a><br />
<a href="http://blog.computerelibol.com/download/as2/PermutationUtil.as">Actionscript 2.0 Permutation Utility class</a></p>
<p>Onward&#8230;</p>
<p>Some time ago on a tech interview website discussion, we were challenged with the following problem:<span id="more-9"></span></p>
<blockquote><p>Given a number N generate all the permutations e.g. N=3<br />
1 2 3<br />
1 3 2<br />
2 1 3<br />
2 3 1<br />
3 2 1<br />
3 1 2</p>
<p>How to generate the N! permutations efficiently ?</p></blockquote>
<p>The following is a <a href="http://en.wikipedia.org/wiki/Permutation">permutation</a> generator using the <a href="http://en.wikipedia.org/wiki/Factoradic">factoradic</a> number system. This technique correlates permutations to a lexicographical index. For those of you interested in this kind of thing, I&#8217;ve written a brief description of the steps of the algorithm which traces all permutations of n, and included an unoptimized version to further articulate the technique.</p>
<p>You will need a function which computes factorials.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> factorial<span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">return</span> i<span style="color: #000000; font-weight: bold;">*</span>factorial<span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000; font-weight:bold;">1</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Pick n.<br />
Get factorial of order n and assign it to kLen.<br />
Reduce kLen and assign it to k for kth permutation of order n.<br />
Once we have the factoradic of k, we can derive kth permutation of n by increasing each element of r by 1 if elements computed leading up to p are less than p.<br />
In this algo though, we increase r[i] for each element greater than r[p] while computing the factoradic of k. This is an optimization to avoid additional loop to compute factoradic.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> n<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">3</span>, kLen<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = factorial<span style="color: #000000;">&#40;</span>n<span style="color: #000000;">&#41;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> r<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span>, j<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>, i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>, p<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>, k<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>;
<span style="color: #0033ff; font-weight: bold;">while</span><span style="color: #000000;">&#40;</span>kLen<span style="color: #000000; font-weight: bold;">--</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	r = <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span>, j = <span style="color: #000000; font-weight:bold;">1</span>, k = kLen;
	r<span style="color: #000000;">&#91;</span>n<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span>;
	<span style="color: #0033ff; font-weight: bold;">while</span><span style="color: #000000;">&#40;</span>j<span style="color: #000000; font-weight: bold;">++&lt;</span>n<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		p = i = n<span style="color: #000000; font-weight: bold;">-</span>j;
		r<span style="color: #000000;">&#91;</span>p<span style="color: #000000;">&#93;</span> = <span style="color: #004993;">uint</span><span style="color: #000000;">&#40;</span>k<span style="color: #000000; font-weight: bold;">/</span>=<span style="color: #000000;">&#40;</span>j<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">%</span>j;
		<span style="color: #0033ff; font-weight: bold;">while</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">++&lt;</span>n<span style="color: #000000;">&#41;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>r<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">&gt;</span>= r<span style="color: #000000;">&#91;</span>p<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000; font-weight: bold;">++</span>r<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>;
	<span style="color: #000000;">&#125;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>r<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>here is the unoptimized version which gets kth permutation of n</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> n<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">3</span>, k<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">6</span>;
<span style="color: #009900;">//determine factoradic of k</span>
<span style="color: #6699cc; font-weight: bold;">var</span> factoradic<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span>;
<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> j<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">1</span>;j<span style="color: #000000; font-weight: bold;">++&lt;</span>n;<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900;">//start to create factoradic</span>
	<span style="color: #009900;">//get rid of last remainder to move on to next radix</span>
	k = k<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">&#40;</span>j<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #009900;">//mod j so element is radix j</span>
	factoradic.<span style="color: #004993;">unshift</span><span style="color: #000000;">&#40;</span>k<span style="color: #000000; font-weight: bold;">%</span>j<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #009900;">//derive permutation from factoradic</span>
<span style="color: #6699cc; font-weight: bold;">var</span> r<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = factoradic.<span style="color: #004993;">slice</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, m<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">1</span>;
<span style="color: #0033ff; font-weight: bold;">while</span><span style="color: #000000;">&#40;</span>m<span style="color: #000000; font-weight: bold;">++&lt;</span>n<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = n<span style="color: #000000; font-weight: bold;">-</span>m;
	<span style="color: #0033ff; font-weight: bold;">while</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">++&lt;</span>n<span style="color: #000000;">&#41;</span>
		<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>r<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">&gt;</span>= r<span style="color: #000000;">&#91;</span>n<span style="color: #000000; font-weight: bold;">-</span>m<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">++</span>r<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>r<span style="color: #000000;">&#41;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.melihelibol.com/?feed=rss2&amp;p=9</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Another Ease Function</title>
		<link>http://blog.melihelibol.com/?p=7</link>
		<comments>http://blog.melihelibol.com/?p=7#comments</comments>
		<pubDate>Mon, 21 Apr 2008 15:24:20 +0000</pubDate>
		<dc:creator>H</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[easing]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[robert penner]]></category>

		<guid isPermaLink="false">http://blog.computerelibol.com/?p=7</guid>
		<description><![CDATA[I had been thinking about making an ease function that eases a transition in a way similar to the way group94 sometimes uses for their transitions. After some time of sleeping on it, I took some time to write it out.
I have left the following code unmodified to illustrate my process.
The initial function I wrote:
Note [...]]]></description>
			<content:encoded><![CDATA[<p>I had been thinking about making an ease function that eases a transition in a way similar to the way group94 sometimes uses for their transitions. After some time of sleeping on it, I took some time to write it out.<span id="more-7"></span></p>
<p>I have left the following code unmodified to illustrate my process.</p>
<p>The initial function I wrote:</p>
<p>Note that the order of parameters of this function are not standard.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> group94EaseOut<span style="color: #000000;">&#40;</span>t<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, <span style="color: #004993;">d</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, <span style="color: #004993;">c</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, <span style="color: #004993;">b</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>t<span style="color: #000000; font-weight: bold;">/</span>=<span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">0.1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #009900;">//ease here later, but for now linear</span>
		<span style="color: #009900;">//trace(t);</span>
		<span style="color: #0033ff; font-weight: bold;">return</span> t<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">0.1</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #004993;">c</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">0.9</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #004993;">b</span>;
	<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #009900;">//trace(t);</span>
		<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000;">&#40;</span>t<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.1</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">0.9</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #004993;">c</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">0.1</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #004993;">c</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">0.9</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #004993;">b</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The idea is to distribute a greater part of the change over a lesser portion of the duration as the first part of the tween, and to distribute the remaining change over the remaining duration. The condition which determines which equation to use is the portion of the duration we distribute the first part of change over.</p>
<p>I realized that the coefficient that determined the distribution could be parameterized, so I added r. The initial function will execute faster, but the following function is more robust.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> group94EaseOut<span style="color: #000000;">&#40;</span>t<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, <span style="color: #004993;">d</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, <span style="color: #004993;">c</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, <span style="color: #004993;">b</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, r<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0.1</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>t<span style="color: #000000; font-weight: bold;">/</span>=<span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">&lt;</span> r<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">return</span> t<span style="color: #000000; font-weight: bold;">/</span>r<span style="color: #000000; font-weight: bold;">*</span><span style="color: #004993;">c</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">-</span>r<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #004993;">b</span>;
	<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000;">&#40;</span>t<span style="color: #000000; font-weight: bold;">-</span>r<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">-</span>r<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #004993;">c</span><span style="color: #000000; font-weight: bold;">*</span>r<span style="color: #000000; font-weight: bold;">+</span><span style="color: #004993;">c</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">-</span>r<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #004993;">b</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>I then used sine easing to ease in for the first part of the tween, and sine easing to ease out for the second part of the tween.</p>
<p>Here is the final function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> easeG94<span style="color: #000000;">&#40;</span>t<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, <span style="color: #004993;">b</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, <span style="color: #004993;">c</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, <span style="color: #004993;">d</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, r<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0.1</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000;">&#40;</span>t<span style="color: #000000; font-weight: bold;">/</span>=<span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">&lt;</span> r<span style="color: #000000; font-weight: bold;">?</span>
		<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span>t<span style="color: #000000; font-weight: bold;">/</span>r<span style="color: #000000; font-weight: bold;">*</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #004993;">c</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">-</span>r<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #004993;">b</span><span style="color: #000000; font-weight: bold;">:</span>
		<span style="color: #004993;">Math</span>.<span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>t<span style="color: #000000; font-weight: bold;">-</span>r<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">-</span>r<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #004993;">c</span><span style="color: #000000; font-weight: bold;">*</span>r<span style="color: #000000; font-weight: bold;">+</span><span style="color: #004993;">c</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">-</span>r<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #004993;">b</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Here is a working example of the final function:</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="400" height="300" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://blog.computerelibol.com/wp-content/uploads/2008/04/easeg94_example.swf" /><embed type="application/x-shockwave-flash" width="400" height="300" src="http://blog.computerelibol.com/wp-content/uploads/2008/04/easeg94_example.swf"></embed></object></p>
<p>Click anywhere on the stage to move the ball. The red line is the curve of the current distribution, which is displayed in the top right corner. Press up and down arrows to change the distribution.</p>
<p>I realize that any of <a href="http://robertpenner.com/flashblog/" target="_blank">Robert Penners</a> easing functions could be used to ease in and out of the two parts of this function, but that much computation may hinder the smoothness of the transition. I do however have something in the works that should improve the performance of all ease functions. That I will write about soon.</p>
<p>Another thing to note, with r=0.5, using any of the standard easeIn and easeOut functions should have the curve of their corresponding easeInOut function.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.melihelibol.com/?feed=rss2&amp;p=7</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Plane Scaling and Positioning with Papervision</title>
		<link>http://blog.melihelibol.com/?p=3</link>
		<comments>http://blog.melihelibol.com/?p=3#comments</comments>
		<pubDate>Tue, 15 Apr 2008 06:14:46 +0000</pubDate>
		<dc:creator>H</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[2d]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[carousel]]></category>
		<category><![CDATA[equations]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[formulas]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[papervision]]></category>

		<guid isPermaLink="false">http://blog.computerelibol.com/?p=3</guid>
		<description><![CDATA[Recently, I used papervision for a project. I needed to create a carousel like 3D system. Even though I think it would be helpful and interesting to some to write about the carousel, I&#8217;m going to focus on the equations this task led me to instead. I think they&#8217;ll prove to be very useful to [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I used papervision for a project. I needed to create a carousel like 3D system. Even though I think it would be helpful and interesting to some to write about the carousel, I&#8217;m going to focus on the equations this task led me to instead. I think they&#8217;ll prove to be very useful to anyone that needs to match design compositions with pixel perfect precision. To really get the most out of this article, you&#8217;ll need some papervision experience. Having made a carousel will help too <img src='http://blog.melihelibol.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  At the moment, I&#8217;m unable to share any working example of the carousel, but at some point I should be able to post an example.<span id="more-3"></span></p>
<p>[<strong>Update 10/09/2008:</strong> I have adjusted the equations in the article so that they work with the latest of papervision to keep the validity of this article intact. I have modified some semantics, notably screenZ is now just referred to as distance. A link to the updated <a href="http://blog.melihelibol.com/download/sample/pvPlanes.zip">sample source</a>. This is not different than the one below.]</p>
<p>[<strong>Update 08/22/2008:</strong> The equations in this article and in the source will only work for builds prior to 639. I am in the process of writing an article about a set of functions that should achieve these effects for versions of papervision after 639. I am leaving this article intact for educational purposes.]</p>
<p>[<strong>Update 08/15/2008:</strong> <a href="http://blog.melihelibol.com/download/sample/pvPlanes.zip">sample source</a> - it is not the carousel, but it demonstrates the use of the equations pretty well.]</p>
<p><!-- <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="718" height="391" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://blog.melihelibol.com/article_content/Seagate_Carousel.swf" /><embed type="application/x-shockwave-flash" width="718" height="391" src="http://blog.melihelibol.com/article_content/Seagate_Carousel.swf"></embed></object> &#8211;></p>
<p><strong>Carousel Explained</strong><br />
Think of a carousel as having the interactive properties of an ordinary list component. The way this (and probably all) carousels work is by displaying a circular collection of selectable items on the screen. Only one item is selectable at a time. The selected item is visually distinguishable in some way (highlighted, repositioned, etc.) In our carousel, the selected item is distinguished by repositioning it to the center of the collection of items. The items to the left and to the right of the selected item are rotated slightly, and moved down the z axis to create the contrast needed between selected and deselected items. With papervision, the display object for each item is typically a Plane, but anything can be used.</p>
<p><strong>Scaling: Problem</strong><br />
The problem started out with having the selected plane object to display at the scale of the design composition I was given. Usually, the projects we do don&#8217;t have any 3D UI. Consequently, we have no process for exporting Collada, since the medium the composition is designed in cannot actually produce such a format (Please let me know if there is a way to export Collada from After Effects.) Consequently, I was left to replicate the composition by measuring 3D data on a 2D plane.</p>
<p><strong>Scaling: Solution</strong><br />
I soon came to realize that I essentially needed to be able to negate perspective (later this becomes even more critical since I need to apply perspective to 2D measurements, and negate perspective from 3D measurements), but I wasn&#8217;t exactly sure about how to do this. I started by trying different constants on the cameras zoom and focus properties, I kept the object Z at 100, and camera focus at default (100) because this obviously had a lot to do with whether the object was at the intended scale. I did find some constants that gave me the results I was looking for, but at a Z position of 100, I was getting hardcore perspective distortion.</p>
<p>After some revisions, I checked out the papervision source and came across the equation for computing perspective. It looks like this:<br />
[<strong>Update 10/09/2008:</strong> This has changed.]</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">perspective = <span style="color: #004993;">focus</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #004993;">zoom</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #004993;">focus</span></pre></div></div>

<p>Focus and zoom are properties of the camera, or in other words, our perspective of the object. Given that the object and camera both have zero rotation, or, possibly a right angle or 180 depending on the object type, modifying any one of focus, zoom to a certain value will produce a pixel perfect match to the composition. After spacing out into a series of day dreams, and some trial and error, I came up with equations that will solve for any of the variables, which when set, will produce the correct distance between the camera and an object to display it at a scale that matches a regular DisplayObject as it would be if you were to simply add it to the stage, that is, as opposed to making it the material of a Plane, and possibly other display types.</p>
<p><strong>Scaling: Equations</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">&nbsp;
<span style="color: #009900;">//Solve for perfect distance</span>
<span style="color: #004993;">distance</span> = cam.<span style="color: #004993;">focus</span><span style="color: #000000; font-weight: bold;">*</span>cam.zoom<span style="color: #000000; font-weight: bold;">-</span>cam.<span style="color: #004993;">focus</span>;
&nbsp;
<span style="color: #009900;">//or just get it</span>
<span style="color: #004993;">distance</span> = <span style="color: #004993;">Math</span>.<span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span>item.z<span style="color: #000000; font-weight: bold;">-</span>cam.z<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//Solve for cam.zoom</span>
cam.<span style="color: #004993;">zoom</span> = <span style="color: #000000;">&#40;</span>cam.<span style="color: #004993;">focus</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #004993;">distance</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> cam.<span style="color: #004993;">focus</span>;
&nbsp;
<span style="color: #009900;">//Solve for cam.focus</span>
cam.<span style="color: #004993;">focus</span> = <span style="color: #004993;">distance</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">&#40;</span>cam.<span style="color: #004993;">zoom</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span>cam.<span style="color: #004993;">zoom</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #009900;">//negate perspective from screenX/screenY</span>
<span style="color: #009900;">//this equation has been changed since 10/09/2008 to exclude the addition of cam.focus to distance before the division</span>
<span style="color: #6699cc; font-weight: bold;">var</span> negatePersp<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">&#40;</span>cam.<span style="color: #004993;">focus</span><span style="color: #000000; font-weight: bold;">*</span>cam.<span style="color: #004993;">zoom</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #004993;">distance</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p><strong>Positioning: Problem</strong><br />
Being able to get the position that a 2D coordinate should have on the x/z plane or the y/z plane was also necessary. I needed to evenly distribute unselected objects over the left and the right areas of the selected object.</p>
<p><strong>Positioning: Solution</strong><br />
We need a way to compute the x position that a plane should have based on a limited amount of screen real estate on the x axis at some variable z position. We will need some predefined variables. <em>item</em> is the Plane we&#8217;re computing the x position for, <em>cam</em> is our Camera3D, and <em>xArea</em> is the area we have to display objects on the left or right side of the selected object. Note that in the actual carousel, I only compute these values once, and use them as needed during transitions. The main difference is that a predefined z position is used to compute screenZ, which would take the place of item.screen.z.</p>
<p><strong>Positioning: Equations</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">&nbsp;
<span style="color: #009900;">//compute the distance</span>
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">distance</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = cam.<span style="color: #004993;">focus</span><span style="color: #000000; font-weight: bold;">*</span>cam.zoom<span style="color: #000000; font-weight: bold;">-</span>cam.<span style="color: #004993;">focus</span>;
&nbsp;
<span style="color: #009900;">//Compute the perspective</span>
<span style="color: #6699cc; font-weight: bold;">var</span> persp<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = cam.<span style="color: #004993;">focus</span><span style="color: #000000; font-weight: bold;">*</span>cam.<span style="color: #004993;">zoom</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #004993;">distance</span>;
&nbsp;
<span style="color: #009900;">//Compute half of the width of the item at the computed distance,</span>
<span style="color: #009900;">//we need this value because the registration point of 3D display objects are at their center,</span>
<span style="color: #009900;">//and we don't want objects falling outside of xArea</span>
<span style="color: #6699cc; font-weight: bold;">var</span> perspItemWidth<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = item.<span style="color: #004993;">width</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">*</span>persp;
&nbsp;
<span style="color: #009900;">//The following operation gives us a value which will produce the inverse ratio of persp</span>
<span style="color: #6699cc; font-weight: bold;">var</span> negatePersp<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span>persp;
&nbsp;
<span style="color: #009900;">//First subtract item.width,</span>
<span style="color: #009900;">//which we've computed perspective for at item.screen.z from xArea,</span>
<span style="color: #009900;">//then apply the inverse operation of perspective on xArea,</span>
<span style="color: #009900;">//we should think of xArea as if it were the screen.x of an object at item.sceen.z</span>
<span style="color: #6699cc; font-weight: bold;">var</span> negatedXArea<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000;">&#40;</span>xArea<span style="color: #000000; font-weight: bold;">-</span>perspItemWidth<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span>negatedPersp;</pre></div></div>

<p><strong>Inverse Ratio of Perspective Explained</strong><br />
Let&#8217;s say xArea is a 2D object at 0,0 with a width of 100. It&#8217;s taking up 100 pixels on the screen on the x axis. If we want to evenly distribute objects across this area at a z position of 1300, we can&#8217;t just use the width of xArea to compute the distribution. Let&#8217;s say we magically add a z property to xArea, and we send it to z position 1300. Now that it&#8217;s further away, it doesn&#8217;t take up as much space as it did when it was right in our face, so it doesn&#8217;t take up the 100 pixels of screen space that we need it to. What we do is compute the perspective to apply to an object at such a distance, and then we divide 1 by the perspective to get the inverse ratio of the perspective. We then multiply our new ratio by the width of xArea (100.) This gives us the distribution scale we need (127) in order to use a screen pixel width of 100 (as opposed to 78) at z position 1300. We can say that we need to distribute objects across 127 &#8220;papervision 3D units&#8221; in order to distribute them evenly across 100 screen pixels at a z position of 1300.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.melihelibol.com/?feed=rss2&amp;p=3</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
