<?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 efnx code blog. &#187; fp10</title>
	<atom:link href="http://efnx.com/tag/fp10/feed/" rel="self" type="application/rss+xml" />
	<link>http://efnx.com</link>
	<description>code. blog.</description>
	<lastBuildDate>Sat, 03 Jul 2010 18:02:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>AS3 &#8211; Drawing Circles With IGraphicsData</title>
		<link>http://efnx.com/as3-drawing-circles-with-igraphicsdata/</link>
		<comments>http://efnx.com/as3-drawing-circles-with-igraphicsdata/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 16:20:06 +0000</pubDate>
		<dc:creator>Schell</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[fp10]]></category>
		<category><![CDATA[graphics]]></category>

		<guid isPermaLink="false">http://blog.efnx.com/?p=195</guid>
		<description><![CDATA[Drawing circles with Flash 10's new IGraphicsData API is easy. Here is a function that will populate a GraphicsPath object with points and commands for drawing a circle approximation.]]></description>
			<content:encoded><![CDATA[<p>Lately I&#8217;ve been working on a game  with some of my free time. It&#8217;s a slow process made a little bit faster through the use of <a href="http://www.box2d.org/">Box2D</a>, which is a great 2D physics lib. In my game the user controls a robot that wheels around and smashes other robots. I decided that I would write some functions for drawing geometric primitives, and that I would draw everything into one sprite, or two, depending on how many layers I&#8217;d need. In an attempt to squeeze out some more frames per second I switched these functions over to use flash 10+ IGraphicsData API. It&#8217;s interesting, to say the least. When using the new API we loose the ability to easily draw rectangles and circles. We can still use familiar functions like moveTo, lineTo and curveTo &#8211; so I&#8217;ve written a function that draws a circle using these. It uses some fun almost calculus [parametrization of a curve in so many points] minus any derivatives or integrals. Is that still calculus? Meh. Here&#8217;s what happens:</p>
<p>We create a new GraphicsStroke [the line], a new GraphicsSolidFill [the fill], a new GraphicsPath [the path] and an IGraphicsData Vector to store them all in.</p>
<div class="codecolorer-container actionscript twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> _stroke :GraphicsStroke &nbsp; &nbsp; &nbsp; &nbsp; = <span style="color: #000000; font-weight: bold;">new</span> GraphicsStroke<span style="color: #66cc66;">&#40;</span>1<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; _stroke.<span style="color: #006600;">fill</span> = <span style="color: #000000; font-weight: bold;">new</span> GraphicsSolidFill<span style="color: #66cc66;">&#40;</span>0xFF00FF, 1<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">var</span> _fill &nbsp; :GraphicsSolidFill &nbsp;&nbsp; &nbsp; = <span style="color: #000000; font-weight: bold;">new</span> GraphicsSolidFill<span style="color: #66cc66;">&#40;</span>0xF0F0F0, 1<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">var</span> _path &nbsp; :GraphicsPath &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = <span style="color: #000000; font-weight: bold;">new</span> GraphicsPath<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">var</span> _graph&nbsp; :Vector.<span style="color: #66cc66;">&lt;</span>IGraphicsData<span style="color: #66cc66;">&gt;</span> = <span style="color: #000000; font-weight: bold;">new</span> Vector.<span style="color: #66cc66;">&lt;</span>IGraphicsData<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>Now what we&#8217;ll need to do is populate the path with some commands and some points. To do this, we can use GraphicsPath&#8217;s familiar functions moveTo, lineTo and curveTo. These functions will fill path.command and path.data with commands and data, respectively. The parameters to each command are stored in the data array, where as a number representing each command are stored in the command array. You can read more about it <a href="http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS749610B4-4709-4f75-BBA0-650BF52623CA.html">here</a>. So here is a function that will fill your path with points and commands to form a circle.</p>
<div class="codecolorer-container actionscript twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> r_addCircle<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_x</span>:<span style="color: #0066CC;">Number</span>, <span style="color: #0066CC;">_y</span>:<span style="color: #0066CC;">Number</span>, r:<span style="color: #0066CC;">Number</span>, path:GraphicsPath, numPoints:<span style="color: #0066CC;">int</span> = 8<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> twoPI:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">PI</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">2</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> curve:<span style="color: #0066CC;">Number</span> = 1 + 1<span style="color: #66cc66;">/</span><span style="color: #66cc66;">&#40;</span>numPoints<span style="color: #66cc66;">*</span>1.75<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; path.<span style="color: #0066CC;">moveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_x</span> + r<span style="color: #66cc66;">&#41;</span>, <span style="color: #0066CC;">_y</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">1</span>; i <span style="color: #66cc66;">&lt;</span>= numPoints; i++<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> th&nbsp; :<span style="color: #0066CC;">Number</span> = twoPI <span style="color: #66cc66;">*</span> i<span style="color: #66cc66;">/</span>numPoints;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> thm :<span style="color: #0066CC;">Number</span> = twoPI <span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#40;</span>i-0.5<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">/</span>numPoints;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> px:<span style="color: #0066CC;">Number</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_x</span> + r <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">cos</span><span style="color: #66cc66;">&#40;</span>th<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> py:<span style="color: #0066CC;">Number</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_y</span> + r <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sin</span><span style="color: #66cc66;">&#40;</span>th<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> hx:<span style="color: #0066CC;">Number</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_x</span> + r <span style="color: #66cc66;">*</span> curve <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">cos</span><span style="color: #66cc66;">&#40;</span>thm<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> hy:<span style="color: #0066CC;">Number</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_y</span> + r <span style="color: #66cc66;">*</span> curve <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sin</span><span style="color: #66cc66;">&#40;</span>thm<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; path.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span>hx, hy, px, py<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>In this function _x and _y represent the center of the circle, r is the radius and path is your GraphicsPath. numPoints refers to the number of points you&#8217;d like to use for approximating your circle. The more points, the more &#8220;perfect&#8221; the circle will look, although more points will tax your frameRate. We can get a pretty nice looking circle with 8 points. 4 looks a little boxy, but around 8 is nice. Experiment. Here&#8217;s the next step &#8211; we&#8217;ll add the points to our path and then add the stroke, fill and path to our Vector and then have a sprite draw our graphics data:</p>
<div class="codecolorer-container actionscript twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">r_addCircle<span style="color: #66cc66;">&#40;</span>50, 50, 50, _path, 2<span style="color: #66cc66;">&#41;</span>;<br />
r_addCircle<span style="color: #66cc66;">&#40;</span>150, 50, 50, _path, 4<span style="color: #66cc66;">&#41;</span>;<br />
r_addCircle<span style="color: #66cc66;">&#40;</span>250, 50, 50, _path, 6<span style="color: #66cc66;">&#41;</span>;<br />
r_addCircle<span style="color: #66cc66;">&#40;</span>350, 50, 50, _path, 8<span style="color: #66cc66;">&#41;</span>;<br />
<br />
_graph.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>_stroke, _fill, _path<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <br />
<span style="color: #000000; font-weight: bold;">var</span> sprite:Sprite = <span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
addChild<span style="color: #66cc66;">&#40;</span>sprite<span style="color: #66cc66;">&#41;</span>; &nbsp; <br />
<br />
sprite.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawGraphicsData</span><span style="color: #66cc66;">&#40;</span>_graph<span style="color: #66cc66;">&#41;</span>;</div></div>
<p>This should draw 4 circle approximations of different resolution. This is what it looks like:<br />
<div id="attachment_197" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.efnx.com/wp-content/uploads/2009/06/Picture-1.png" rel="lightbox"><img src="http://blog.efnx.com/wp-content/uploads/2009/06/Picture-1-300x206.png" alt="approximated circles" title="approximated circles" width="300" height="206" class="size-medium wp-image-197" /></a><p class="wp-caption-text">approximated circles</p></div></p>
<p>You can see that as n [numPoints] increases, the closer to an actual circle our object becomes. I hope this entry helps some of you out there save a little time.</p>
]]></content:encoded>
			<wfw:commentRss>http://efnx.com/as3-drawing-circles-with-igraphicsdata/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
