<?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>Silveira Neto &#187; C</title>
	<atom:link href="http://silveiraneto.net/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://silveiraneto.net</link>
	<description>the world is a pixel</description>
	<lastBuildDate>Sun, 08 Jan 2012 05:17:57 +0000</lastBuildDate>
	<language>pt-br</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>C# class properties example</title>
		<link>http://silveiraneto.net/2011/12/15/c-class-properties-example/</link>
		<comments>http://silveiraneto.net/2011/12/15/c-class-properties-example/#comments</comments>
		<pubDate>Thu, 15 Dec 2011 15:26:31 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[C-Sharp]]></category>
		<category><![CDATA[celsius]]></category>
		<category><![CDATA[fahrenheit]]></category>
		<category><![CDATA[kelvin]]></category>
		<category><![CDATA[temperature]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=4000</guid>
		<description><![CDATA[A example of use of C# class properties to convert temperatures in Celsius, Fahrenheit or Kelvin. The temperature is encapsulated and stored in a internal representation, in this example, in Celcius (private double c). Each conversion is accessible by getting or setting a property. using System; &#160; public class Temperature &#123; private double c; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>A example of use of C# class properties to convert temperatures in Celsius, Fahrenheit or Kelvin. The temperature is encapsulated and stored in a internal representation, in this example, in Celcius (private double c). Each conversion is accessible by getting or setting a property.</p>
<div class="wp_syntax">
<div class="code">
<pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span>;
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Temperature <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">double</span> c;
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">double</span> celsius <span style="color: #000000;">&#123;</span>
		get <span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">return</span> c;
		<span style="color: #000000;">&#125;</span>
		set <span style="color: #000000;">&#123;</span>
			c <span style="color: #008000;">=</span> value;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">double</span> fahrenheit <span style="color: #000000;">&#123;</span>
		get <span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>c <span style="color: #008000;">*</span> <span style="color: #FF0000;">9</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">32</span>;
		<span style="color: #000000;">&#125;</span>
		set <span style="color: #000000;">&#123;</span>
			c <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>value <span style="color: #008000;">-</span> <span style="color: #FF0000;">32</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">*</span> <span style="color: #FF0000;">5</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">9</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">double</span> kelvin <span style="color: #000000;">&#123;</span>
		get <span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">return</span> c <span style="color: #008000;">+</span> <span style="color: #FF0000;">273.15</span>;
		<span style="color: #000000;">&#125;</span>
		set <span style="color: #000000;">&#123;</span>
			c <span style="color: #008000;">=</span> value <span style="color: #008000;">-</span> <span style="color: #FF0000;">273.15</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> TemperatureExample <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		Temperature fortaleza <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Temperature<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		fortaleza.<span style="color: #0000FF;">celsius</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">26</span>;
&nbsp;
		Temperature washington <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Temperature<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		washington.<span style="color: #0000FF;">fahrenheit</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">32</span>;
&nbsp;
		Temperature sun <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Temperature<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		sun.<span style="color: #0000FF;">kelvin</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">5778</span>;
&nbsp;
		Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Fortaleza {0}°C / {1}°F / {2} K&quot;</span>,
			fortaleza.<span style="color: #0000FF;">celsius</span>, fortaleza.<span style="color: #0000FF;">fahrenheit</span>, fortaleza.<span style="color: #0000FF;">kelvin</span><span style="color: #000000;">&#41;</span>;
		Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Washington {0}°C / {1}°F / {2} K&quot;</span>,
			washington.<span style="color: #0000FF;">celsius</span>, washington.<span style="color: #0000FF;">fahrenheit</span>, washington.<span style="color: #0000FF;">kelvin</span><span style="color: #000000;">&#41;</span>;
		Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Sun {0}°C / {1}°F / {2} K&quot;</span>,
			sun.<span style="color: #0000FF;">celsius</span>, sun.<span style="color: #0000FF;">fahrenheit</span>, sun.<span style="color: #0000FF;">kelvin</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre>
</div>
</div>
<p>Output:</p>
<pre>Fortaleza 26°C / 78.8°F / 299.15 K
Washington 0°C / 32°F / 273.15 K
Sun 5504.85°C / 9940.73°F / 5778 K
</pre>
<p>There is some good examples of C# class properties at <a href="http://msdn.microsoft.com/en-us/library/w86s7x04.aspx">Using Properties (C# Programming Guide) at MSDN</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2011/12/15/c-class-properties-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Atiaia early releases</title>
		<link>http://silveiraneto.net/2011/07/23/atiaia-early-releases/</link>
		<comments>http://silveiraneto.net/2011/07/23/atiaia-early-releases/#comments</comments>
		<pubDate>Sun, 24 Jul 2011 00:57:09 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[Atiaia]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Marco Diego]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Ray Tracying]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3841</guid>
		<description><![CDATA[This was a project that me and Marco Diego created during our graduation for the Computer Graphics course. It is a ray tracing engine build from scratch in C. It was great exercise of experimentation on how implement object-oriented design patterns in ANSI C. Later Marco continued it in his master&#8217;s degree thesis implementing more [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><iframe width="640" height="510" src="http://www.youtube.com/embed/Jpk4vl48veY" frameborder="0" allowfullscreen></iframe></p>
<p>This was a project that me and <a href="http://plus.google.com/107875064284677597038/posts">Marco Diego</a> created during our graduation for the Computer Graphics course. It is a <a href="http://en.wikipedia.org/wiki/Ray_tracing_(graphics)">ray tracing</a> engine build from scratch in C. It was great exercise of experimentation on how implement object-oriented design patterns in ANSI C. Later Marco continued it in his master&#8217;s degree thesis implementing more features. </p>
<p>Parts of the sources were lost during a disk failure in the forge we hosted the project. I found some early releases and packed them here for future use. It can be useful for someone studying C or how to implement a ray tracer.</p>
<p><strong>Download:</strong> <a href='http://silveiraneto.net/wp-content/uploads/2011/07/atiaia_sources_by_2006.zip'>atiaia_sources_by_2006.zip</a></p>
<p>Enjoy it.</p>
<p>ps: with this project we won the 1st place project of class and maximum grade. ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2011/07/23/atiaia-early-releases/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tiled TMX Map Loader for Pygame</title>
		<link>http://silveiraneto.net/2009/12/19/tiled-tmx-map-loader-for-pygame/</link>
		<comments>http://silveiraneto.net/2009/12/19/tiled-tmx-map-loader-for-pygame/#comments</comments>
		<pubDate>Sat, 19 Dec 2009 10:15:48 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Qt]]></category>
		<category><![CDATA[Tiled]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3063</guid>
		<description><![CDATA[I&#8217;m using the Tiled Map Editor for a while, I even wrote that tutorial about it. It&#8217;s a general purpose tile map editor, written in Java but now migrating to C++ with Qt, that can be easily used with my set of free pixelart tiles. A map done with Tiled is stored in a file [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m using the <a href="http://mapeditor.org/">Tiled Map Editor</a> for a while, I even wrote <a href="http://silveiraneto.net/2009/01/11/game-map-edition-using-tiled/">that tutorial about it</a>. It&#8217;s a general purpose tile map editor, written in Java but now migrating to C++ with Qt, that can be easily used with <a title="pixel art work" href="http://silveiraneto.net/tag/pixelart">my set of free pixelart tiles</a>.</p>
<p><center><a href="../wp-content/uploads/2009/01/map_editor_tiles_tileset_game-deveopment.png"><img title="map editor tiles tileset game deveopment" src="../wp-content/uploads/2009/01/map_editor_tiles_tileset_game-deveopment-500x343.png" alt="map editor tiles tileset game deveopment" width="500" height="343" /></a></center></p>
<p>A map done with Tiled is stored in a file with TMX extension. It&#8217;s just a XML file, easy to understand.</p>
<p>As I&#8217;m creating a map loader for my owns purposes, the procedure I&#8217;m doing here works we need some simplifications. I&#8217;m handling orthogonal maps only. I&#8217;m not supporting tile properties as well. I also don&#8217;t want to handle base64 and zlib encoding in this version, so in the Tiled editor, go at the menu <em>Edit → Preferences</em> and in the <em>Saving</em> tab unmark the options &#8220;Use binary encoding&#8221; and &#8220;Compress Layer Data (gzip)&#8221;, like this:</p>
<p style="text-align: center;"><img class="size-full wp-image-3064 aligncenter" title="Tiled Preferences Window" src="http://silveiraneto.net/wp-content/uploads/2009/12/Tiled_preferences.png" alt="Tiled Preferences Window" width="370" height="309" /></p>
<p>When saving a map it will produce a TMX file like this:</p>
<div class="wp_syntax">
<div class="code">
<pre class="xml xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE map SYSTEM &quot;http://mapeditor.org/dtd/1.0/map.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;map</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">orientation</span>=<span style="color: #ff0000;">&quot;orthogonal&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">tilewidth</span>=<span style="color: #ff0000;">&quot;32&quot;</span> <span style="color: #000066;">tileheight</span>=<span style="color: #ff0000;">&quot;32&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Author&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Silveira Neto&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Year&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;2009&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tileset</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;mytiles&quot;</span> <span style="color: #000066;">firstgid</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">tilewidth</span>=<span style="color: #ff0000;">&quot;32&quot;</span> <span style="color: #000066;">tileheight</span>=<span style="color: #ff0000;">&quot;32&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;image</span> <span style="color: #000066;">source</span>=<span style="color: #ff0000;">&quot;free_tileset_version_10.png&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tileset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;layer</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;grass&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;10&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tile</span> <span style="color: #000066;">gid</span>=<span style="color: #ff0000;">&quot;261&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tile</span> <span style="color: #000066;">gid</span>=<span style="color: #ff0000;">&quot;260&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    ...
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tile</span> <span style="color: #000066;">gid</span>=<span style="color: #ff0000;">&quot;160&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tile</span> <span style="color: #000066;">gid</span>=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/layer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>
</div>
</div>
<p>For processing it on Python I&#8217;m using the <a href="http://docs.python.org/whatsnew/2.0.html#sax2-support">event oriented SAX approach for XML</a>. So I create a ContentHandler that handles events the start and end of XML elements. In the first element, map, I know enough to create a <a href="http://www.pygame.org/docs/ref/surface.html">Pygame surface</a> with the correct size. I&#8217;m also storing the map properties so I can use it later for add some logics or effects on the map. After that we create a instance of the Tileset class from where we will get the each tile by an gid number. Each layer has it&#8217;s a bunch of gids in the correct order. So it&#8217;s enough information to mount and draw a map.</p>
<div class="wp_syntax">
<div class="code">
<pre class="python python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Author: Silveira Neto</span>
<span style="color: #808080; font-style: italic;"># License: GPLv3</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>, pygame
<span style="color: #ff7700;font-weight:bold;">from</span> pygame.<span style="color: #008000;">locals</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">from</span> pygame <span style="color: #ff7700;font-weight:bold;">import</span> Rect
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">xml</span> <span style="color: #ff7700;font-weight:bold;">import</span> sax
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Tileset:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">file</span>, tile_width, tile_height<span style="color: black;">&#41;</span>:
        image = pygame.<span style="color: black;">image</span>.<span style="color: black;">load</span><span style="color: black;">&#40;</span><span style="color: #008000;">file</span><span style="color: black;">&#41;</span>.<span style="color: black;">convert_alpha</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> image:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Error creating new Tileset: file %s not found&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">file</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span> = tile_width
        <span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span> = tile_height
        <span style="color: #008000;">self</span>.<span style="color: black;">tiles</span> = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>image.<span style="color: black;">get_height</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>/<span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">for</span> column <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>image.<span style="color: black;">get_width</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>/<span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span><span style="color: black;">&#41;</span>:
                pos = Rect<span style="color: black;">&#40;</span>
                        column<span style="color: #66cc66;">*</span><span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span>,
                        line<span style="color: #66cc66;">*</span><span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span>,
                        <span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span>,
                        <span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span> <span style="color: black;">&#41;</span>
                <span style="color: #008000;">self</span>.<span style="color: black;">tiles</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>image.<span style="color: black;">subsurface</span><span style="color: black;">&#40;</span>pos<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get_tile<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, gid<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">tiles</span><span style="color: black;">&#91;</span>gid<span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> TMXHandler<span style="color: black;">&#40;</span>sax.<span style="color: black;">ContentHandler</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">width</span> = 0
        <span style="color: #008000;">self</span>.<span style="color: black;">height</span> = 0
        <span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span> = 0
        <span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span> = 0
        <span style="color: #008000;">self</span>.<span style="color: black;">columns</span> = 0
        <span style="color: #008000;">self</span>.<span style="color: black;">lines</span>  = 0
        <span style="color: #008000;">self</span>.<span style="color: black;">properties</span> = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">image</span> = <span style="color: #008000;">None</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">tileset</span> = <span style="color: #008000;">None</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> startElement<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name, attrs<span style="color: black;">&#41;</span>:
        <span style="color: #808080; font-style: italic;"># get most general map informations and create a surface</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> name == <span style="color: #483d8b;">'map'</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">columns</span> = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>attrs.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'width'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">lines</span>  = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>attrs.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'height'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span> = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>attrs.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'tilewidth'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span> = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>attrs.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'tileheight'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">width</span> = <span style="color: #008000;">self</span>.<span style="color: black;">columns</span> <span style="color: #66cc66;">*</span> <span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">height</span> = <span style="color: #008000;">self</span>.<span style="color: black;">lines</span> <span style="color: #66cc66;">*</span> <span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">image</span> = pygame.<span style="color: black;">Surface</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #008000;">self</span>.<span style="color: black;">width</span>, <span style="color: #008000;">self</span>.<span style="color: black;">height</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>.<span style="color: black;">convert</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># create a tileset</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> name==<span style="color: #483d8b;">&quot;image&quot;</span>:
            source = attrs.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'source'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">tileset</span> = Tileset<span style="color: black;">&#40;</span>source, <span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span>, <span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># store additional properties.</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> name == <span style="color: #483d8b;">'property'</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">properties</span><span style="color: black;">&#91;</span>attrs.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'name'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> = attrs.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'value'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># starting counting</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> name == <span style="color: #483d8b;">'layer'</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">line</span> = 0
            <span style="color: #008000;">self</span>.<span style="color: black;">column</span> = 0
        <span style="color: #808080; font-style: italic;"># get information of each tile and put on the surface using the tileset</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> name == <span style="color: #483d8b;">'tile'</span>:
            gid = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>attrs.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'gid'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> - <span style="color: #ff4500;">1</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> gid <span style="color: #66cc66;">&lt;</span>0: gid = 0
            tile = <span style="color: #008000;">self</span>.<span style="color: black;">tileset</span>.<span style="color: black;">get_tile</span><span style="color: black;">&#40;</span>gid<span style="color: black;">&#41;</span>
            pos = <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">column</span><span style="color: #66cc66;">*</span><span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span>, <span style="color: #008000;">self</span>.<span style="color: black;">line</span><span style="color: #66cc66;">*</span><span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">image</span>.<span style="color: black;">blit</span><span style="color: black;">&#40;</span>tile, pos<span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #008000;">self</span>.<span style="color: black;">column</span> += <span style="color: #ff4500;">1</span>
            <span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">column</span><span style="color: #66cc66;">&gt;</span>=<span style="color: #008000;">self</span>.<span style="color: black;">columns</span><span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">column</span> = 0
                <span style="color: #008000;">self</span>.<span style="color: black;">line</span> += <span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># just for debugging</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> endDocument<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">self</span>.<span style="color: black;">width</span>, <span style="color: #008000;">self</span>.<span style="color: black;">height</span>, <span style="color: #008000;">self</span>.<span style="color: black;">tile_width</span>, <span style="color: #008000;">self</span>.<span style="color: black;">tile_height</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">self</span>.<span style="color: black;">properties</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">self</span>.<span style="color: black;">image</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">!</span>=<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Usage:<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>{0} filename'</span>.<span style="color: black;">format</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
    pygame.<span style="color: black;">init</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    screen = pygame.<span style="color: black;">display</span>.<span style="color: black;">set_mode</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">800</span>, <span style="color: #ff4500;">480</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span> = sax.<span style="color: black;">make_parser</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    tmxhandler = TMXHandler<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">setContentHandler</span><span style="color: black;">&#40;</span>tmxhandler<span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff4500;">1</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> event <span style="color: #ff7700;font-weight:bold;">in</span> pygame.<span style="color: black;">event</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> event.<span style="color: #008000;">type</span> == QUIT:
                <span style="color: #ff7700;font-weight:bold;">return</span>
            <span style="color: #ff7700;font-weight:bold;">elif</span> event.<span style="color: #008000;">type</span> == KEYDOWN <span style="color: #ff7700;font-weight:bold;">and</span> event.<span style="color: black;">key</span> == K_ESCAPE:
                <span style="color: #ff7700;font-weight:bold;">return</span>
        screen.<span style="color: black;">fill</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">255</span>,<span style="color: #ff4500;">255</span>,<span style="color: #ff4500;">255</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        screen.<span style="color: black;">blit</span><span style="color: black;">&#40;</span>tmxhandler.<span style="color: black;">image</span>, <span style="color: black;">&#40;</span>0,0<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        pygame.<span style="color: black;">display</span>.<span style="color: black;">flip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        pygame.<span style="color: #dc143c;">time</span>.<span style="color: black;">delay</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1000</span>/<span style="color: #ff4500;">60</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>: main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
</div>
</div>
<p>Here is the result for opening a four layers map file:</p>
<p><center><a href="http://silveiraneto.net/wp-content/uploads/2009/12/netbeans_python_openning_map.png"><img src="http://silveiraneto.net/wp-content/uploads/2009/12/netbeans_python_openning_map-500x375.png" alt="netbeans python openning map" title="netbeans python openning map" width="500" height="375" class="alignnone size-medium wp-image-3067" /></a> </center></p>
<p>That&#8217;s it. You can get this code and adapt for your game because next versions will be a lot more coupled for my own purposes and not so general.</p>
<p><strong>Download:</strong><a href="http://silveiraneto.net/downloads/maploader.tar.bz2"><img src="http://silveiraneto.net/wp-content/uploads/2009/12/package_32x32.png" alt="package" title="package" width="32" height="32" class="alignnone size-full wp-image-3069" />maploader.tar.bz2</a> It&#8217;s the Netbeans 6.7 (Python EA 2) project file but that can be opened or used with another IDE or without one. Also contains the village.tmx map and the tileset.</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/12/19/tiled-tmx-map-loader-for-pygame/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>OpenCV: adding two images</title>
		<link>http://silveiraneto.net/2009/12/08/opencv-adding-two-images/</link>
		<comments>http://silveiraneto.net/2009/12/08/opencv-adding-two-images/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 04:55:05 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Computer Vision]]></category>
		<category><![CDATA[cvadd]]></category>
		<category><![CDATA[opencv]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[surfer]]></category>
		<category><![CDATA[universe]]></category>
		<category><![CDATA[wikimedia]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3013</guid>
		<description><![CDATA[This is a very simple example of how to open two images and display them added. I got two pictures at project Commons from Wikimedia that were highlighted on Featured Pictures. I did a crop on both to have the same size, as I&#8217;m trying to make this example as simple as possible. The first [...]]]></description>
			<content:encoded><![CDATA[<p>This is a very simple example of how to open two images and display them added.</p>
<p>I got two pictures at project <a href="http://commons.wikimedia.org">Commons</a> from <a href="http://wikimedia.org">Wikimedia</a> <span id="result_box"><span style="background-color: #ffffff;" title="Duas imagens que ganharam destaque.">that were highlighted on Featured Pictures</span></span>. I did a crop on both to have the same size, as I&#8217;m trying to make this example as simple as possible.</p>
<p>The <a title="Wikipedia" href="http://commons.wikimedia.org/wiki/File:Guisard_-_Milky_Way.jpg">first one is a photo of our Milky Way</a>, taken at <a title="Wikipedia" href="http://en.wikipedia.org/wiki/Paranal_Observatory">Paranal Observatory</a> by Stéphane Guisard.</p>
<p style="text-align: center;"><img class="size-full wp-image-3014 aligncenter" title="milkyway sky Stéphane Guisard Paranal" src="http://silveiraneto.net/wp-content/uploads/2009/12/milkyway.jpg" alt="milkyway " width="500" height="400" /></p>
<p>The second one is a <a href="http://commons.wikimedia.org/wiki/File:California_surfer_inside_wave.jpg">California surfer inside wave</a>, taken by <a href="http://home.comcast.net/~milazinkova/Fogshadow.html">Mila Zinkova</a>.</p>
<p style="text-align: center;"><img class="size-full wp-image-3015 aligncenter" title="surfer" src="http://silveiraneto.net/wp-content/uploads/2009/12/surfer.jpg" alt="surfer" width="500" height="400" /></p>
<p>In this simple <a title="OpenCV" href="http://sourceforge.net/projects/opencvlibrary/">OpenCV</a> code below, we open the images, create a new one to display the result and use cvAdd to add them. We do not save the result or handle more than the ordinary case of two images with the same size.</p>
<div class="wp_syntax">
<div class="code">
<pre class="c c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;cv.h&gt;</span>
<span style="color: #339933;">#include &lt;highgui.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span>argv <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    IplImage <span style="color: #339933;">*</span>surfer<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>milkyway<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>result;
    <span style="color: #993333;">int</span> key <span style="color: #339933;">=</span> <span style="color:#800080;">0</span>;
    CvSize size;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* load images, check, get size (both should have the same) */</span>
    surfer <span style="color: #339933;">=</span> cvLoadImage<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;surfer.jpg&quot;</span><span style="color: #339933;">,</span> CV_LOAD_IMAGE_COLOR<span style="color: #009900;">&#41;</span>;
    milkyway <span style="color: #339933;">=</span> cvLoadImage<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;milkyway.jpg&quot;</span><span style="color: #339933;">,</span> CV_LOAD_IMAGE_COLOR<span style="color: #009900;">&#41;</span>;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>surfer<span style="color: #009900;">&#41;</span>||<span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>milkyway<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Could not open one or more images.&quot;</span><span style="color: #009900;">&#41;</span>;
        exit <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span>;
    <span style="color: #009900;">&#125;</span>
    size <span style="color: #339933;">=</span> cvGetSize<span style="color: #009900;">&#40;</span>surfer<span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* create a empty image, same size, depth and channels of others */</span>
    result <span style="color: #339933;">=</span> cvCreateImage<span style="color: #009900;">&#40;</span>size<span style="color: #339933;">,</span> surfer<span style="color: #339933;">-&gt;</span>depth<span style="color: #339933;">,</span> surfer<span style="color: #339933;">-&gt;</span>nChannels<span style="color: #009900;">&#41;</span>;
    cvZero<span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* result = surfer + milkyway (NULL mask)*/</span>
    cvAdd<span style="color: #009900;">&#40;</span>surfer<span style="color: #339933;">,</span> milkyway<span style="color: #339933;">,</span> result<span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* create a window, display the result, wait for a key */</span>
    cvNamedWindow<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;example&quot;</span><span style="color: #339933;">,</span> CV_WINDOW_AUTOSIZE<span style="color: #009900;">&#41;</span>;
    cvShowImage<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;example&quot;</span><span style="color: #339933;">,</span> result<span style="color: #009900;">&#41;</span>;
    cvWaitKey<span style="color: #009900;">&#40;</span><span style="color:#800080;">0</span><span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* free memory and get out */</span>
    cvDestroyWindow<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;example&quot;</span><span style="color: #009900;">&#41;</span>;
    cvReleaseImage<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>surfer<span style="color: #009900;">&#41;</span>;
    cvReleaseImage<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>milkyway<span style="color: #009900;">&#41;</span>;
    cvReleaseImage<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>result<span style="color: #009900;">&#41;</span>;
    <span style="color: #b1b100;">return</span> <span style="color:#800080;">0</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* gcc add.c -o add `pkg-config opencv --libs --cflags` */</span></pre>
</div>
</div>
<p>Compile it (on a well configured OpenCV development environment) and run it:</p>
<blockquote><p>gcc add.c -o add `pkg-config opencv &#8211;libs &#8211;cflags`<br />
./add</p></blockquote>
<p>The result got pretty cool, a milky way surfer.</p>
<p style="text-align: center;"><img class="size-full wp-image-3016 aligncenter" title="surfer in the milk way" src="http://silveiraneto.net/wp-content/uploads/2009/12/result.jpg" alt="surfer in the milk way" width="500" height="400" /></p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/12/08/opencv-adding-two-images/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Simple Face Detection Player</title>
		<link>http://silveiraneto.net/2009/12/01/simple-face-detection-player/</link>
		<comments>http://silveiraneto.net/2009/12/01/simple-face-detection-player/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 22:03:15 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[intel]]></category>
		<category><![CDATA[opencv]]></category>
		<category><![CDATA[Paul Viola]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Rainer Lienhar]]></category>
		<category><![CDATA[vision]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=2987</guid>
		<description><![CDATA[Here&#8217;s a simple video player that also performs facial detection thought the Open Computer Vision Library. Here&#8217;s a code developed using codes from nashruddin.com and samples from OpenCV, including the haar classifier xml. More detailed explanation on the theory about how the OpenCV face detection algorithm works can be found here. The code: #include &#60;highgui.h&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a simple video player that also performs facial detection thought the <a title="OpenCV at Wikipedia" href="http://en.wikipedia.org/wiki/OpenCV">Open Computer Vision Library</a>.</p>
<p>Here&#8217;s a code developed using codes <a title="OpenCV Face Detection" href="http://nashruddin.com/OpenCV_Face_Detection">from nashruddin.com</a> and samples from <a title="OpenCV Project" href="http://sourceforge.net/projects/opencvlibrary/">OpenCV</a>, including the haar classifier xml. More detailed explanation on the theory about how the OpenCV face detection algorithm works can be found <a title="Face Detection on OpenCV" href="http://opencv.willowgarage.com/wiki/FaceDetection">here</a>.</p>
<p>The code:</p>
<div class="wp_syntax">
<div class="code">
<pre class="c c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;highgui.h&gt;</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;cv.h&gt;</span>
&nbsp;
CvHaarClassifierCascade <span style="color: #339933;">*</span>cascade;
CvMemStorage <span style="color: #339933;">*</span>storage;
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    CvCapture <span style="color: #339933;">*</span>video <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">NULL</span>;
    IplImage <span style="color: #339933;">*</span>frame <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">NULL</span>;
    <span style="color: #993333;">int</span> delay <span style="color: #339933;">=</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> key<span style="color: #339933;">,</span> i<span style="color: #339933;">=</span><span style="color:#800080;">0</span>;
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>window_name <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;Video&quot;</span>;
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>cascadefile <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;haarcascade_frontalface_alt.xml&quot;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* check for video file passed by command line */</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>argc<span style="color: #339933;">&gt;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        video <span style="color: #339933;">=</span> cvCaptureFromFile<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Usage: %s VIDEO_FILE<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> argv<span style="color: #009900;">&#91;</span><span style="color:#800080;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span>;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* check file was correctly opened */</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>video<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Unable to open <span style="color: #000099; font-weight: bold;">\&quot;</span>%s<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span>;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* load the classifier */</span>
    cascade <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> CvHaarClassifierCascade<span style="color: #339933;">*</span> <span style="color: #009900;">&#41;</span>cvLoad<span style="color: #009900;">&#40;</span> cascadefile<span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span> <span style="color: #009900;">&#41;</span>;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>cascade<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Error loading the classifier.&quot;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span>;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* setup the memory buffer for the face detector */</span>
    storage <span style="color: #339933;">=</span> cvCreateMemStorage<span style="color: #009900;">&#40;</span> <span style="color:#800080;">0</span> <span style="color: #009900;">&#41;</span>;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>storage<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Error creating the memory storage.&quot;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span>;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* create a video window, auto size */</span>
    cvNamedWindow<span style="color: #009900;">&#40;</span>window_name<span style="color: #339933;">,</span> CV_WINDOW_AUTOSIZE<span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* get a frame. Necessary for use the cvGetCaptureProperty */</span>
    frame <span style="color: #339933;">=</span> cvQueryFrame<span style="color: #009900;">&#40;</span>video<span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* calculate the delay between each frame and display video's FPS */</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%2.2f FPS<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> cvGetCaptureProperty<span style="color: #009900;">&#40;</span>video<span style="color: #339933;">,</span> CV_CAP_PROP_FPS<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
    delay <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1000</span><span style="color: #339933;">/</span>cvGetCaptureProperty<span style="color: #009900;">&#40;</span>video<span style="color: #339933;">,</span> CV_CAP_PROP_FPS<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>frame<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #808080; font-style: italic;">/* show loaded frame */</span>
        cvShowImage<span style="color: #009900;">&#40;</span>window_name<span style="color: #339933;">,</span> frame<span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/* wait delay and check for the quit key */</span>
        key <span style="color: #339933;">=</span> cvWaitKey<span style="color: #009900;">&#40;</span>delay<span style="color: #009900;">&#41;</span>;
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>key<span style="color: #339933;">==</span><span style="color: #ff0000;">'q'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">break</span>;
	<span style="color: #808080; font-style: italic;">/* load and check next frame*/</span>
        frame <span style="color: #339933;">=</span> cvQueryFrame<span style="color: #009900;">&#40;</span>video<span style="color: #009900;">&#41;</span>;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>frame<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;error loading frame.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">/* detect faces */</span>
        CvSeq <span style="color: #339933;">*</span>faces <span style="color: #339933;">=</span> cvHaarDetectObjects<span style="color: #009900;">&#40;</span>
            frame<span style="color: #339933;">,</span> <span style="color: #808080; font-style: italic;">/* image to detect objects in */</span>
            cascade<span style="color: #339933;">,</span> <span style="color: #808080; font-style: italic;">/* haar classifier cascade */</span>
            storage<span style="color: #339933;">,</span> <span style="color: #808080; font-style: italic;">/* resultant sequence of the object candidate rectangles */</span>
            <span style="color:#800080;">1.1</span><span style="color: #339933;">,</span> <span style="color: #808080; font-style: italic;">/* increse window by 10% between the subsequent scans*/</span>
            <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> <span style="color: #808080; font-style: italic;">/* 3 neighbors makes up an object */</span>
            <span style="color:#800080;">0</span> <span style="color: #808080; font-style: italic;">/* flags CV_HAAR_DO_CANNY_PRUNNING */</span><span style="color: #339933;">,</span>
            cvSize<span style="color: #009900;">&#40;</span> <span style="color: #0000dd;">40</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">40</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">/* for each face found, draw a red box */</span>
        <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> i <span style="color: #339933;">=</span> <span style="color:#800080;">0</span> ; i <span style="color: #339933;">&lt;</span> <span style="color: #009900;">&#40;</span> faces <span style="color: #339933;">?</span> faces<span style="color: #339933;">-&gt;</span>total <span style="color: #339933;">:</span> <span style="color:#800080;">0</span> <span style="color: #009900;">&#41;</span> ; i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
             CvRect <span style="color: #339933;">*</span>r <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> CvRect<span style="color: #339933;">*</span> <span style="color: #009900;">&#41;</span>cvGetSeqElem<span style="color: #009900;">&#40;</span> faces<span style="color: #339933;">,</span> i <span style="color: #009900;">&#41;</span>;
             cvRectangle<span style="color: #009900;">&#40;</span> frame<span style="color: #339933;">,</span>
                  cvPoint<span style="color: #009900;">&#40;</span> r<span style="color: #339933;">-&gt;</span>x<span style="color: #339933;">,</span> r<span style="color: #339933;">-&gt;</span>y <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                  cvPoint<span style="color: #009900;">&#40;</span> r<span style="color: #339933;">-&gt;</span>x <span style="color: #339933;">+</span> r<span style="color: #339933;">-&gt;</span>width<span style="color: #339933;">,</span> r<span style="color: #339933;">-&gt;</span>y <span style="color: #339933;">+</span> r<span style="color: #339933;">-&gt;</span>height <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                  CV_RGB<span style="color: #009900;">&#40;</span> <span style="color: #0000dd;">255</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span> <span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p><small>Yeah, I know the code needs a few adjustments. ¬¬</small></p>
<p>To compile it in a well configured OpenCV development environment:</p>
<blockquote><p>gcc faceplayer.c  -o faceplayer `pkg-config opencv &#8209;&#8209;libs &#8209;&#8209;cflags`</p></blockquote>
<p>To run it you have to put in the same directory of the binary the XML classifier (haarcascade_frontalface_alt.xml) that comes with OpenCV sources at OpenCV-2.0.0/data/haarcascades/. And so:</p>
<blockquote><p>./faceplayer video.avi</p></blockquote>
<p>The results I got so far is that it works well for faces but sometimes its also detects more than faces. And here a video of it working live.</p>
<p><center><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/es_O3c-Kc-Q&#038;hl=pt_BR&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/es_O3c-Kc-Q&#038;hl=pt_BR&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></center></p>
<p>A example of good result:</p>
<p style="text-align: center;"><img class="size-full wp-image-2988 aligncenter" title="rick roll face detection" src="http://silveiraneto.net/wp-content/uploads/2009/12/rickroll_face_detection.jpg" alt="rick roll face detection" width="400" height="240" /></p>
<p>A example of bad result:</p>
<p style="text-align: center;"><img src="http://silveiraneto.net/wp-content/uploads/2009/12/rickroll_face_detection_bad_result.jpg" alt="rick roll face detection bad result" title="rick roll face detection bad result" width="400" height="240" class="alignnone size-full wp-image-2991" /></p>
<p>Maybe with some adjustments it could performs even better. But was really easy to create it using OpenCV.</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/12/01/simple-face-detection-player/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Morse Code Translator with Arduino</title>
		<link>http://silveiraneto.net/2009/02/28/morse-code-translator-with-arduino/</link>
		<comments>http://silveiraneto.net/2009/02/28/morse-code-translator-with-arduino/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 16:16:58 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[buzzer]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Diecimila]]></category>
		<category><![CDATA[embedded device]]></category>
		<category><![CDATA[Free Software]]></category>
		<category><![CDATA[Freeduino]]></category>
		<category><![CDATA[Open Hardware]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[sound]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=2328</guid>
		<description><![CDATA[You write in your computer, sends a message thought USB and Arduino translates it into a Morse code. Just a Arduino board with a buzzer connected at the digital output 12 (one wire in the ground and the other in the 12). I tried to make the code as general as possible so you can [...]]]></description>
			<content:encoded><![CDATA[<p>You write in your computer, sends a message thought USB and Arduino translates it into a <a href="http://en.wikipedia.org/wiki/Morse_code">Morse code</a>.</p>
<p style="text-align: center;"><object width="425" height="344" data="http://www.youtube.com/v/1_X5AwVrkUo&amp;hl=pt-br&amp;fs=1" type="application/x-shockwave-flash"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/1_X5AwVrkUo&amp;hl=pt-br&amp;fs=1" /><param name="allowfullscreen" value="true" /></object></pre>
<p>Just a Arduino board with a buzzer connected at the digital output 12 (one wire in the ground and the other in the 12).</p>
<p style="text-align: center;"><img src='http://farm4.static.flickr.com/3495/3315777991_75f0f92578.jpg' alt='Arduino'/></pre>
<p>I tried to make the code as general as possible so you can easily adapt it for anthers ways of transmitting a Morse code. To do that you just need to rewrite a few functions.</p>
<p style="text-align: center;">
<pre>
                                                  +-------------------+
                                                  | 3) Interpretation |
                                                  +-------------------+
                                                  |   2) Translation  |
+-------------------+                             +-------------------+
|     Computer      |<========USB (Serial)=======>|     1) Reading    |
+-------------------+                             +-------------------+
</pre>
</p>
<ol>
<li>Reads a character from Serial. Main function loop().</li>
<li>Translate a ascii char into a Morse code using a reference table. A letter 'K' becomes a string word "-.-". Function say_char().</li>
<li>Interpret the Morse word as light and sound. Mostly at function say_morse_word(). The Interpretation needs 5 functions to say all Morse words, dot(), dash(), shortgap(), mediumgap() and intragap().</li>
</ol>
<p>For a more details on Morse code I strongly recommend the <a href="http://en.wikipedia.org/wiki/Morse_code">English Wikipedia article on it</a>.</p>
<div class="wp_syntax">
<div class="code">
<pre class="c c" style="font-family:monospace;"><span style="color: #993333;">int</span> led <span style="color: #339933;">=</span> <span style="color: #0000dd;">13</span>;                   <span style="color: #666666; font-style: italic;">// LED connected to digital pin 13</span>
<span style="color: #993333;">int</span> buzzer <span style="color: #339933;">=</span> <span style="color: #0000dd;">12</span>;                <span style="color: #666666; font-style: italic;">// buzzer connected to digital pin 12</span>
<span style="color: #993333;">int</span> unit <span style="color: #339933;">=</span> <span style="color: #0000dd;">50</span>;                  <span style="color: #666666; font-style: italic;">// duration of a pulse</span>
&nbsp;
<span style="color: #993333;">char</span> <span style="color: #339933;">*</span> morsecode<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #ff0000;">&quot;-----&quot;</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// 0</span>
    <span style="color: #ff0000;">&quot;.----&quot;</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// 1</span>
    <span style="color: #ff0000;">&quot;..---&quot;</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// 2</span>
    <span style="color: #ff0000;">&quot;...--&quot;</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// 3</span>
    <span style="color: #ff0000;">&quot;....-&quot;</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// 4</span>
    <span style="color: #ff0000;">&quot;.....&quot;</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// 5</span>
    <span style="color: #ff0000;">&quot;-....&quot;</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// 6 </span>
    <span style="color: #ff0000;">&quot;--...&quot;</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// 7</span>
    <span style="color: #ff0000;">&quot;---..&quot;</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// 8</span>
    <span style="color: #ff0000;">&quot;----.&quot;</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// 9</span>
    <span style="color: #ff0000;">&quot;---...&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// :</span>
    <span style="color: #ff0000;">&quot;-.-.-.&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// ;</span>
    <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">,</span>       <span style="color: #666666; font-style: italic;">// &lt; (there's no morse for this simbol)</span>
    <span style="color: #ff0000;">&quot;-...-&quot;</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// =</span>
    <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">,</span>       <span style="color: #666666; font-style: italic;">// &gt; (there's no morse for this simbol)</span>
    <span style="color: #ff0000;">&quot;..--..&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// ?</span>
    <span style="color: #ff0000;">&quot;.--._.&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// @</span>
    <span style="color: #ff0000;">&quot;.-&quot;</span><span style="color: #339933;">,</span>     <span style="color: #666666; font-style: italic;">// A</span>
    <span style="color: #ff0000;">&quot;-...&quot;</span><span style="color: #339933;">,</span>   <span style="color: #666666; font-style: italic;">// B</span>
    <span style="color: #ff0000;">&quot;-.-.&quot;</span><span style="color: #339933;">,</span>   <span style="color: #666666; font-style: italic;">// C</span>
    <span style="color: #ff0000;">&quot;-..&quot;</span><span style="color: #339933;">,</span>    <span style="color: #666666; font-style: italic;">// D</span>
    <span style="color: #ff0000;">&quot;.&quot;</span><span style="color: #339933;">,</span>      <span style="color: #666666; font-style: italic;">// E</span>
    <span style="color: #ff0000;">&quot;..-.&quot;</span><span style="color: #339933;">,</span>   <span style="color: #666666; font-style: italic;">// F</span>
    <span style="color: #ff0000;">&quot;--.&quot;</span><span style="color: #339933;">,</span>    <span style="color: #666666; font-style: italic;">// G</span>
    <span style="color: #ff0000;">&quot;....&quot;</span><span style="color: #339933;">,</span>   <span style="color: #666666; font-style: italic;">// H</span>
    <span style="color: #ff0000;">&quot;..&quot;</span><span style="color: #339933;">,</span>     <span style="color: #666666; font-style: italic;">// I</span>
    <span style="color: #ff0000;">&quot;.---&quot;</span><span style="color: #339933;">,</span>   <span style="color: #666666; font-style: italic;">// J</span>
    <span style="color: #ff0000;">&quot;-.-&quot;</span><span style="color: #339933;">,</span>    <span style="color: #666666; font-style: italic;">// K</span>
    <span style="color: #ff0000;">&quot;.-..&quot;</span><span style="color: #339933;">,</span>   <span style="color: #666666; font-style: italic;">// L</span>
    <span style="color: #ff0000;">&quot;--&quot;</span><span style="color: #339933;">,</span>     <span style="color: #666666; font-style: italic;">// M</span>
    <span style="color: #ff0000;">&quot;-.&quot;</span><span style="color: #339933;">,</span>     <span style="color: #666666; font-style: italic;">// N</span>
    <span style="color: #ff0000;">&quot;---&quot;</span><span style="color: #339933;">,</span>    <span style="color: #666666; font-style: italic;">// O</span>
    <span style="color: #ff0000;">&quot;.--.&quot;</span><span style="color: #339933;">,</span>   <span style="color: #666666; font-style: italic;">// P</span>
    <span style="color: #ff0000;">&quot;--.-&quot;</span><span style="color: #339933;">,</span>   <span style="color: #666666; font-style: italic;">// Q</span>
    <span style="color: #ff0000;">&quot;.-.&quot;</span><span style="color: #339933;">,</span>    <span style="color: #666666; font-style: italic;">// R</span>
    <span style="color: #ff0000;">&quot;...&quot;</span><span style="color: #339933;">,</span>    <span style="color: #666666; font-style: italic;">// S</span>
    <span style="color: #ff0000;">&quot;-&quot;</span><span style="color: #339933;">,</span>      <span style="color: #666666; font-style: italic;">// T</span>
    <span style="color: #ff0000;">&quot;..-&quot;</span><span style="color: #339933;">,</span>    <span style="color: #666666; font-style: italic;">// U</span>
    <span style="color: #ff0000;">&quot;...-&quot;</span><span style="color: #339933;">,</span>   <span style="color: #666666; font-style: italic;">// V</span>
    <span style="color: #ff0000;">&quot;.--&quot;</span><span style="color: #339933;">,</span>    <span style="color: #666666; font-style: italic;">// W</span>
    <span style="color: #ff0000;">&quot;-..-&quot;</span><span style="color: #339933;">,</span>   <span style="color: #666666; font-style: italic;">// X</span>
    <span style="color: #ff0000;">&quot;-.--&quot;</span><span style="color: #339933;">,</span>   <span style="color: #666666; font-style: italic;">// Y</span>
    <span style="color: #ff0000;">&quot;--..&quot;</span>    <span style="color: #666666; font-style: italic;">// Z</span>
<span style="color: #009900;">&#125;</span>;
&nbsp;
<span style="color: #993333;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  pinMode<span style="color: #009900;">&#40;</span>led<span style="color: #339933;">,</span> OUTPUT<span style="color: #009900;">&#41;</span>;
  pinMode<span style="color: #009900;">&#40;</span>buzzer<span style="color: #339933;">,</span> OUTPUT<span style="color: #009900;">&#41;</span>;
  Serial.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">9600</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> say_morse_word<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span> msg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span> index <span style="color: #339933;">=</span> <span style="color:#800080;">0</span>;
  <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #339933;">!=</span><span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\0</span>'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// say a dash</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #ff0000;">'-'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      dash<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">// say a dot</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #ff0000;">'.'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      dot<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">// gap beetween simbols</span>
    intragap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    index++;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// beep</span>
<span style="color: #993333;">void</span> beep<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> time<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span> i;
  <span style="color: #993333;">int</span> t <span style="color: #339933;">=</span> <span style="color: #0000dd;">100</span>; <span style="color: #666666; font-style: italic;">// period of the wav. bigger means lower pitch.</span>
  <span style="color: #993333;">int</span> beepduration <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span>time<span style="color: #339933;">/</span>t<span style="color: #339933;">*</span><span style="color: #0000dd;">1800</span><span style="color: #009900;">&#41;</span>;
  digitalWrite<span style="color: #009900;">&#40;</span>led<span style="color: #339933;">,</span> HIGH<span style="color: #009900;">&#41;</span>;
  <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color:#800080;">0</span>;i<span style="color: #339933;">&lt;</span>beepduration;i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    digitalWrite<span style="color: #009900;">&#40;</span>buzzer<span style="color: #339933;">,</span> HIGH<span style="color: #009900;">&#41;</span>;
    delayMicroseconds<span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span>;
    digitalWrite<span style="color: #009900;">&#40;</span>buzzer<span style="color: #339933;">,</span> LOW<span style="color: #009900;">&#41;</span>;
    delayMicroseconds<span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
  delay<span style="color: #009900;">&#40;</span>time<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// silence</span>
<span style="color: #993333;">void</span> silence<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> time<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  digitalWrite<span style="color: #009900;">&#40;</span>led<span style="color: #339933;">,</span> LOW<span style="color: #009900;">&#41;</span>;
  delay<span style="color: #009900;">&#40;</span>time<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// general procedure for .</span>
<span style="color: #993333;">void</span> dot<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  beep<span style="color: #009900;">&#40;</span>unit<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// general procedure for -</span>
<span style="color: #993333;">void</span> dash<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  beep<span style="color: #009900;">&#40;</span>unit<span style="color: #339933;">*</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// gap between dots and dashes</span>
<span style="color: #993333;">void</span> intragap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  silence<span style="color: #009900;">&#40;</span>unit<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// gap between letters</span>
<span style="color: #993333;">void</span> shortgap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  silence<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #339933;">*</span>unit<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// gap be  tween words</span>
<span style="color: #993333;">void</span> mediumgap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  silence<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">7</span><span style="color: #339933;">*</span>unit<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> say_char<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> letter<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>letter<span style="color: #339933;">&gt;=</span><span style="color: #ff0000;">'0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span>letter<span style="color: #339933;">&lt;=</span><span style="color: #ff0000;">'Z'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span>letter<span style="color: #339933;">!=</span><span style="color: #ff0000;">'&lt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span>letter<span style="color: #339933;">!=</span><span style="color: #ff0000;">'&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span>morsecode<span style="color: #009900;">&#91;</span>letter<span style="color: #339933;">-</span><span style="color: #ff0000;">'0'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
    Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">' '</span><span style="color: #009900;">&#41;</span>;
    say_morse_word<span style="color: #009900;">&#40;</span>morsecode<span style="color: #009900;">&#91;</span>letter<span style="color: #339933;">-</span><span style="color: #ff0000;">'0'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
    shortgap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>letter<span style="color: #339933;">==</span><span style="color: #ff0000;">' '</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; <span style="color: #000099; font-weight: bold;">\\</span> &quot;</span><span style="color: #009900;">&#41;</span>;
      mediumgap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
      Serial.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;X&quot;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>Serial.<span style="color: #202020;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    say_char<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span><span style="color: #009900;">&#41;</span>Serial.<span style="color: #202020;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>Additionally you can put another function to say entire strings, like say_string("HELLO WORLD")</p>
<div class="wp_syntax">
<div class="code">
<pre class="c c" style="font-family:monospace;"><span style="color: #993333;">void</span> say_string<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span> asciimsg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span> index <span style="color: #339933;">=</span> <span style="color:#800080;">0</span>;
  <span style="color: #993333;">char</span> charac;
  charac <span style="color: #339933;">=</span> asciimsg<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span>;
  <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>charac<span style="color: #339933;">!=</span><span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\0</span>'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    say_char<span style="color: #009900;">&#40;</span>morsecode<span style="color: #009900;">&#91;</span>charac<span style="color: #339933;">-</span><span style="color: #ff0000;">'0'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
    Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span>morsecode<span style="color: #009900;">&#91;</span>charac<span style="color: #339933;">-</span><span style="color: #ff0000;">'0'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
    charac <span style="color: #339933;">=</span> asciimsg<span style="color: #009900;">&#91;</span><span style="color: #339933;">++</span>index<span style="color: #009900;">&#93;</span>;
    shortgap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>You can use the Arduino IDE itself or any other program that talks with the serial port USB.</p>
<p style="text-align: center;"><a href="http://silveiraneto.net/wp-content/uploads/2009/02/arduino_interface.png"><img src="http://silveiraneto.net/wp-content/uploads/2009/02/arduino_interface.png" alt="arduino interface" title="arduino interface" width="545" height="585" class="alignnone size-full wp-image-2342" /></a></pre>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/02/28/morse-code-translator-with-arduino/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>C Gaussian Elimination Implementation</title>
		<link>http://silveiraneto.net/2008/12/09/c-gaussian-elimination-implementation/</link>
		<comments>http://silveiraneto.net/2008/12/09/c-gaussian-elimination-implementation/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 05:35:29 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[gauss]]></category>
		<category><![CDATA[Gaussian elimination]]></category>
		<category><![CDATA[linear programming]]></category>
		<category><![CDATA[mathematics]]></category>
		<category><![CDATA[numerical methods]]></category>
		<category><![CDATA[numericla calculus]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=1896</guid>
		<description><![CDATA[A simple gaussian elimination implemented in C. To simplify, I hard coded the linear system 10 x1 + 2 x2 + 3 x3 + 4 x4 = 5 6 x1 + 17 x2 + 8 x3 + 9 x4 = 10 11 x1 + 12 x2 + 23 x3 + 14 x4 = 15 16 [...]]]></description>
			<content:encoded><![CDATA[<p>A simple gaussian elimination implemented in C.</p>
<p>To simplify, I hard coded the linear system</p>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td rowspan="4"><img class="alignnone size-full wp-image-1895" title="colchete" src="http://silveiraneto.net/wp-content/uploads/2008/12/colchete.gif" alt="" width="10" height="59" /></td>
<td align="right">10 <em>x</em><sub>1</sub></td>
<td align="right">+ 2 <em>x</em><sub>2</sub></td>
<td align="right">+ 3 <em>x</em><sub>3</sub></td>
<td align="right">+ 4 <em>x</em><sub>4</sub></td>
<td>= 5</td>
</tr>
<tr>
<td align="right">6 <em>x</em><sub>1</sub></td>
<td align="right">+ 17 <em>x</em><sub>2</sub></td>
<td align="right">+ 8 <em>x</em><sub>3</sub></td>
<td align="right">+ 9 <em>x</em><sub>4</sub></td>
<td>= 10</td>
</tr>
<tr>
<td align="right">11 <em>x</em><sub>1</sub></td>
<td align="right">+ 12 <em>x</em><sub>2</sub></td>
<td align="right">+ 23 <em>x</em><sub>3</sub></td>
<td align="right">+ 14 <em>x</em><sub>4</sub></td>
<td>= 15</td>
</tr>
<tr>
<td align="right">16 <em>x</em><sub>1</sub></td>
<td align="right">+ 17 <em>x</em><sub>2</sub></td>
<td align="right">+ 18 <em>x</em><sub>3</sub></td>
<td align="right">+ 29 <em>x</em><sub>4</sub></td>
<td>= 20</td>
</tr>
</tbody>
</table>
<p>into the AB float matrix.</p>
<div class="wp_syntax">
<div class="code">
<pre class="c c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
 * Description: Solve a hard coded linear system by gaussian elimination
 * Author: Silveira Neto
 * License: Public Domain
 */</span>
&nbsp;
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
&nbsp;
<span style="color: #339933;">#define ROWS 4</span>
<span style="color: #339933;">#define COLS 5</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/**
 * Linear System, Ax = B
 *
 * 10*x1 +  2*x2 +  3*x3 +  4*x4 = 5
 *  6*x1 + 17*x2 +  8*x3 +  9*x4 = 10
 * 11*x1 + 12*x2 + 23*x3 + 14*x4 = 15
 * 16*x1 + 17*x2 + 18*x3 + 29*x4 = 20
 */</span>
<span style="color: #993333;">float</span> AB<span style="color: #009900;">&#91;</span>ROWS<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>COLS<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#123;</span><span style="color: #0000dd;">10</span><span style="color: #339933;">,</span>  <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span>  <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span>  <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span>  <span style="color: #0000dd;">5</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> <span style="color: #0000dd;">6</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">17</span><span style="color: #339933;">,</span>  <span style="color: #0000dd;">8</span><span style="color: #339933;">,</span>  <span style="color: #0000dd;">9</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span><span style="color: #0000dd;">11</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">12</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">23</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">14</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">15</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span><span style="color: #0000dd;">16</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">17</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">18</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">29</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">20</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/* Answer x from Ax=B */</span>
<span style="color: #993333;">float</span> X<span style="color: #009900;">&#91;</span>ROWS<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color:#800080;">0</span><span style="color: #339933;">,</span><span style="color:#800080;">0</span><span style="color: #339933;">,</span><span style="color:#800080;">0</span><span style="color: #339933;">,</span><span style="color:#800080;">0</span><span style="color: #009900;">&#125;</span>;
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span><span style="color: #339933;">**</span> argv<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> row<span style="color: #339933;">,</span> col<span style="color: #339933;">,</span> i;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* gaussian elimination */</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>col<span style="color: #339933;">=</span><span style="color:#800080;">0</span>; col<span style="color: #339933;">&lt;</span>COLS<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span>; col<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>row<span style="color: #339933;">=</span><span style="color:#800080;">0</span>; row<span style="color: #339933;">&lt;</span>ROWS; line<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #993333;">float</span> pivot <span style="color: #339933;">=</span>  AB<span style="color: #009900;">&#91;</span>row<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>col<span style="color: #009900;">&#93;</span><span style="color: #339933;">/</span>AB<span style="color: #009900;">&#91;</span>col<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>col<span style="color: #009900;">&#93;</span>;
            <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>row<span style="color: #339933;">!=</span>col<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color:#800080;">0</span>; i<span style="color: #339933;">&lt;</span>COLS; i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    AB<span style="color: #009900;">&#91;</span>row<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> AB<span style="color: #009900;">&#91;</span>row<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> pivot <span style="color: #339933;">*</span> AB<span style="color: #009900;">&#91;</span>col<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>;
                <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* X = B/A and show X */</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>row<span style="color: #339933;">=</span><span style="color:#800080;">0</span>; row<span style="color: #339933;">&lt;</span>ROWS; line<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        X<span style="color: #009900;">&#91;</span>row<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> AB<span style="color: #009900;">&#91;</span>row<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>ROWS<span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> AB<span style="color: #009900;">&#91;</span>row<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>row<span style="color: #009900;">&#93;</span>;
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%3.5f &quot;</span><span style="color: #339933;">,</span> X<span style="color: #009900;">&#91;</span>row<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;n&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>EXIT_SUCCESS<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>Before the gaugassian elimination, AB is</p>
<blockquote><pre>10  2  3  4  5
 6 17  8  9 10
11 12 23 14 15
16 17 18 29 20</pre>
</blockquote>
<p>and after it is </p>
<blockquote><pre>10.00000 0.00000 0.00000 0.00000 2.82486
0.00000 15.80000 0.00000 0.00000 3.92768
0.00000 0.00000 15.85443 0.00000 3.85164
0.00000 0.00000 0.00000 14.13174 3.35329 </pre>
</blockquote>
<p>that corresponds to </p>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td rowspan="4"><img class="alignnone size-full wp-image-1895" title="colchete" src="http://silveiraneto.net/wp-content/uploads/2008/12/colchete.gif" alt="" width="10" height="59" /></td>
<td align="right">10 <em>x</em><sub>1</sub></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td>= 2.82486</td>
</tr>
<tr>
<td align="right"></td>
<td align="right">15.80000 <em>x</em><sub>2</sub></td>
<td align="right"></td>
<td align="right"></td>
<td>= 3.92768</td>
</tr>
<tr>
<td align="right"></td>
<td align="right"></td>
<td align="right">15.85443  <em>x</em><sub>3</sub></td>
<td align="right"></td>
<td>= 3.85164</td>
</tr>
<tr>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right">14.13174 <em>x</em><sub>4</sub></td>
<td>= 3.35329</td>
</tr>
</tbody>
</table>
<p>The solution vector is  X = (<em>x</em><sub>1</sub>, <em>x</em><sub>2</sub>, <em>x</em><sub>3</sub>, <em>x</em><sub>4</sub>). We get it by X=B/A.</p>
<p>The program output, X, is</p>
<blockquote><pre>0.28249 0.24859 0.24294 0.23729</pre>
</blockquote>
<p><strong>Benchmarking:</strong><br />
I&#8217;m this serial implementation over one node of our cluster, a machine with 4 processors (Intel Xeon 1.8 Ghz) and 1Gb RAM memory. I tried random systems from 1000 to 5000 variables and got the average time.</p>
<p><center><img class="aligncenter size-full wp-image-1927" title="gaugassian elimination serial" src="http://silveiraneto.net/wp-content/uploads/2008/12/gaugassian_elimination_serial.png" alt="gaugassian elimination serial" width="511" height="323" /></center></p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2008/12/09/c-gaussian-elimination-implementation/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>JavaFX, rectangular collision detection</title>
		<link>http://silveiraneto.net/2008/10/30/javafx-rectangular-collision-detection/</link>
		<comments>http://silveiraneto.net/2008/10/30/javafx-rectangular-collision-detection/#comments</comments>
		<pubDate>Thu, 30 Oct 2008 15:26:36 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[JavaFX]]></category>
		<category><![CDATA[jfx]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[openjfx]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rectangle collision]]></category>
		<category><![CDATA[rectangular collision]]></category>
		<category><![CDATA[rectangular collision detection]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=1629</guid>
		<description><![CDATA[In a game I wrote some years ago we handled simple rectangular collisions. Given the points: We did: // returning 0 means collision int collision&#40;int ax, int ay, int bx, int by, int cx, int cy, int dx, int dy&#41;&#123; return &#40;&#40;ax &#62; dx&#41;&#124;&#124;&#40;bx &#60; cx&#41;&#124;&#124;&#40;ay &#62; dy&#41;&#124;&#124;&#40;by &#60; cy&#41;&#41;; &#125; I&#8217;ll show here a [...]]]></description>
			<content:encoded><![CDATA[<p><center><object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/NRwRTHPGg6M&amp;rel=0&amp;color1=0x3a3a3a&amp;color2=0x999999"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/NRwRTHPGg6M&amp;rel=0&amp;color1=0x3a3a3a&amp;color2=0x999999" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></object></center></p>
<p>In <a title="Batalhão" href="http://batalhao.codigolivre.org.br/">a game</a> I wrote some years ago we handled simple rectangular collisions. Given the points:</p>
<p style="text-align: center;"><img class="size-full wp-image-1630 aligncenter" title="abcd" src="http://silveiraneto.net/wp-content/uploads/2008/10/abcd.png" alt="" width="200" height="145" /></p>
<p>We did:</p>
<div class="wp_syntax">
<div class="code">
<pre class="c c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// returning 0 means collision</span>
<span style="color: #993333;">int</span> collision<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> ax<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> ay<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> bx<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> by<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> cx<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> cy<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> dx<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> dy<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ax <span style="color: #339933;">&gt;</span> dx<span style="color: #009900;">&#41;</span>||<span style="color: #009900;">&#40;</span>bx <span style="color: #339933;">&lt;</span> cx<span style="color: #009900;">&#41;</span>||<span style="color: #009900;">&#40;</span>ay <span style="color: #339933;">&gt;</span> dy<span style="color: #009900;">&#41;</span>||<span style="color: #009900;">&#40;</span>by <span style="color: #339933;">&lt;</span> cy<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>I&#8217;ll show here a little demo about how implement simple rectangular collisions on JavaFX.<br />
First I created a movable rectangle using the same idea of draggable nodes I already had <a href="http://silveiraneto.net/2008/08/11/javafx-draggable-node/">posted before</a>.</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.input.MouseEvent</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.geometry.Rectangle</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MovableRectangle <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Rectangle</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> attribute startX <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.0</span>;
    <span style="color: #000000; font-weight: bold;">private</span> attribute startY <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.0</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> attribute onMove <span style="color: #339933;">=</span> function<span style="color: #009900;">&#40;</span>e<span style="color: #339933;">:</span><span style="color: #003399;">MouseEvent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #003399;">Void</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
    override attribute onMousePressed <span style="color: #339933;">=</span> function<span style="color: #009900;">&#40;</span>e<span style="color: #339933;">:</span><span style="color: #003399;">MouseEvent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #003399;">Void</span> <span style="color: #009900;">&#123;</span>
        startX <span style="color: #339933;">=</span> e.<span style="color: #006633;">getDragX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span>translateX;
        startY <span style="color: #339933;">=</span> e.<span style="color: #006633;">getDragY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span>translateY;
        onMove<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    override attribute onMouseDragged <span style="color: #339933;">=</span> function<span style="color: #009900;">&#40;</span>e<span style="color: #339933;">:</span><span style="color: #003399;">MouseEvent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #003399;">Void</span> <span style="color: #009900;">&#123;</span>
        translateX <span style="color: #339933;">=</span> e.<span style="color: #006633;">getDragX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span>startX;
        translateY <span style="color: #339933;">=</span> e.<span style="color: #006633;">getDragY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span>startY;
        onMove<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>In the main code I some important things:</p>
<ul>
<li><strong>colide</strong>, a color that represents the collision effect. White means no collision and gray means collision.</li>
<li><strong>rec1</strong> and <strong>rec2</strong>, the two rectangles that can collide.</li>
<li><strong>checkcollision()</strong> the function that checks and handles a possible collision.</li>
</ul>
<p>Here is the main code:</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.application.Frame</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.application.Stage</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.geometry.Rectangle</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.scene.paint.Color</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javafx.input.MouseEvent</span>;
&nbsp;
var colide <span style="color: #339933;">=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">WHITE</span>;
&nbsp;
function checkcollision<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #003399;">Void</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>
        <span style="color: #009900;">&#40;</span>rec1.<span style="color: #006633;">getBoundsX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> rec2.<span style="color: #006633;">getBoundsX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> rec2.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> or
        <span style="color: #009900;">&#40;</span>rec1.<span style="color: #006633;">getBoundsX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> rec1.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> rec2.<span style="color: #006633;">getBoundsX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> or
        <span style="color: #009900;">&#40;</span>rec1.<span style="color: #006633;">getBoundsY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> rec2.<span style="color: #006633;">getBoundsY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> rec2.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> or
        <span style="color: #009900;">&#40;</span>rec1.<span style="color: #006633;">getBoundsY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> rec1.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> rec2.<span style="color: #006633;">getBoundsY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        colide <span style="color: #339933;">=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">WHITE</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        colide <span style="color: #339933;">=</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">LIGHTGRAY</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
var rec1<span style="color: #339933;">:</span> MovableRectangle <span style="color: #339933;">=</span> MovableRectangle <span style="color: #009900;">&#123;</span>
    x<span style="color: #339933;">:</span> <span style="color: #cc66cc;">10</span>, y<span style="color: #339933;">:</span> <span style="color: #cc66cc;">10</span>, width<span style="color: #339933;">:</span> <span style="color: #cc66cc;">50</span>, height<span style="color: #339933;">:</span> <span style="color: #cc66cc;">60</span>, fill<span style="color: #339933;">:</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">RED</span>
    onMove<span style="color: #339933;">:</span> function<span style="color: #009900;">&#40;</span>e<span style="color: #339933;">:</span><span style="color: #003399;">MouseEvent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #003399;">Void</span> <span style="color: #009900;">&#123;</span>
        checkcollision<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
var rec2<span style="color: #339933;">:</span> MovableRectangle <span style="color: #339933;">=</span> MovableRectangle <span style="color: #009900;">&#123;</span>
    x<span style="color: #339933;">:</span> <span style="color: #cc66cc;">100</span>, y<span style="color: #339933;">:</span> <span style="color: #cc66cc;">100</span>, width<span style="color: #339933;">:</span> <span style="color: #cc66cc;">70</span>, height<span style="color: #339933;">:</span> <span style="color: #cc66cc;">30</span>, fill<span style="color: #339933;">:</span> <span style="color: #003399;">Color</span>.<span style="color: #006633;">BLUE</span>
    onMove<span style="color: #339933;">:</span> function<span style="color: #009900;">&#40;</span><span style="color: #003399;">MouseEvent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #003399;">Void</span> <span style="color: #009900;">&#123;</span>
        checkcollision<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003399;">Frame</span> <span style="color: #009900;">&#123;</span>
    title<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Rectangular Collisions&quot;</span>, width<span style="color: #339933;">:</span> <span style="color: #cc66cc;">300</span>, height<span style="color: #339933;">:</span> <span style="color: #cc66cc;">300</span>
    closeAction<span style="color: #339933;">:</span> function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span> 0 <span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
    visible<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">true</span>
&nbsp;
    stage<span style="color: #339933;">:</span> Stage <span style="color: #009900;">&#123;</span>
        fill<span style="color: #339933;">:</span> bind colide
        content<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>rec1, rec2<span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>Try it via Java Web Start:</p>
<p><center><a href="http://silveiraneto.net/downloads/collisiondemo/launch.jnlp"><img src="http://silveiraneto.net/wp-content/uploads/2008/10/webstart.png" alt="Java Web Start" /></a></center></p>
<p>Some considerations:</p>
<ul>
<li>You can use rectangular collisions to create bounding boxes to handle collisions in more complex shapes or sprites. Is a common approach in 2d games to avoid more expensive calculations.</li>
<li>There are space for optimizations.</li>
<li>In this case I&#8217;m using only two objects. Some problems raises when I have N objects to handle.</li>
</ul>
<p>More generally, we can code:</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;">function collission<span style="color: #009900;">&#40;</span>ax, ay, bx, by, cx, cy, dx, dy<span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #003399;">Boolean</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> not <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ax <span style="color: #339933;">&gt;</span> dx<span style="color: #009900;">&#41;</span>or<span style="color: #009900;">&#40;</span>bx <span style="color: #339933;">&lt;</span> cx<span style="color: #009900;">&#41;</span>or<span style="color: #009900;">&#40;</span>ay <span style="color: #339933;">&gt;</span> dy<span style="color: #009900;">&#41;</span>or<span style="color: #009900;">&#40;</span>by <span style="color: #339933;">&lt;</span> cy<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
function hitnode<span style="color: #009900;">&#40;</span>a<span style="color: #339933;">:</span> Node, b<span style="color: #339933;">:</span>Node<span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #003399;">Boolean</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>collission<span style="color: #009900;">&#40;</span>
        a.<span style="color: #006633;">getBoundsX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, a.<span style="color: #006633;">getBoundsY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
        a.<span style="color: #006633;">getBoundsX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> a.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, a.<span style="color: #006633;">getBoundsY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> a.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
        b.<span style="color: #006633;">getX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, b.<span style="color: #006633;">getY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
        b.<span style="color: #006633;">getX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> b.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, b.<span style="color: #006633;">getY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> b.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>This way we can pass just two bounding boxes to hitnode and easily check collision of a node against a list of bounding boxes nodes.<br />
Using the same approach I also wrote this function to test if a Node is inside another Node:</p>
<div class="wp_syntax">
<div class="code">
<pre class="java java" style="font-family:monospace;">function inside <span style="color: #009900;">&#40;</span>ax, ay, bx, by, cx, cy, dx, dy<span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #003399;">Boolean</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ax <span style="color: #339933;">&gt;</span> cx<span style="color: #009900;">&#41;</span> and <span style="color: #009900;">&#40;</span>bx <span style="color: #339933;">&lt;</span> dx<span style="color: #009900;">&#41;</span> and <span style="color: #009900;">&#40;</span>ay <span style="color: #339933;">&gt;</span> cy<span style="color: #009900;">&#41;</span> and <span style="color: #009900;">&#40;</span>by <span style="color: #339933;">&lt;</span> dy<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
function insidenode<span style="color: #009900;">&#40;</span>a<span style="color: #339933;">:</span>Node,b<span style="color: #339933;">:</span>Node<span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #003399;">Boolean</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>inside<span style="color: #009900;">&#40;</span>
        a.<span style="color: #006633;">getBoundsX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, a.<span style="color: #006633;">getBoundsY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
        a.<span style="color: #006633;">getBoundsX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> a.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, a.<span style="color: #006633;">getBoundsY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> a.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
        b.<span style="color: #006633;">getBoundsX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, b.<span style="color: #006633;">getBoundsY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
        b.<span style="color: #006633;">getBoundsX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> b.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, b.<span style="color: #006633;">getBoundsY</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> b.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>Soon I&#8217;ll post game examples showing how to use this method and others collission detection methods.</p>
<p><strong>Downloads:</strong></p>
<ul>
<li>The original video, <a href="http://silveiraneto.net/downloads/javafx_rectangular_collision_detection.ogg">javafx_rectangular_collision_detection.ogg</a></li>
<li>NetBeans 6.1 Project with sources, <a href="http://silveiraneto.net/downloads/javafx_rec_col.tar.gz">javafx_rec_col.tar.gz</a>. Needs <a href="http://javafx.netbeans.org">JavaFX module</a> installed.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2008/10/30/javafx-rectangular-collision-detection/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Anúncio do NetBeans 6.5 Beta</title>
		<link>http://silveiraneto.net/2008/08/13/anuncio-do-netbeans-65-beta/</link>
		<comments>http://silveiraneto.net/2008/08/13/anuncio-do-netbeans-65-beta/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 15:22:19 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[português]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[banco de dados]]></category>
		<category><![CDATA[beta]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[GlassFish]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=1081</guid>
		<description><![CDATA[O Netbeans.org anunciou a disponibilidade do NetBeans IDE 6.5 Beta. Abaixo a tradução do anúncio: O NetBeans IDE 6.5 introduz várias novas funcionalidades, incluindo uma IDE robusta para PHP, deputação de JavaScript para o Firefox e IE, e suporte a Groovy e Grails. Esse lançamento também inclui várias melhorias para o desenvolvimento em Java, Ruby [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a title="Download Now" href="http://download.netbeans.org/netbeans/6.5/beta/"><img class="size-full wp-image-1082 aligncenter" title="netbeans-65-beta" src="http://silveiraneto.net/wp-content/uploads/2008/08/netbeans-65-beta.png" alt="" width="400" height="137" /></a></p>
<p>O <a title="NetBeans" href="http://netbeans.org">Netbeans.org</a> anunciou a disponibilidade do <a title="NetBeans 6.5 Beta" href="http://www.netbeans.org/community/releases/65/">NetBeans IDE 6.5 Beta</a>. Abaixo a tradução do anúncio:</p>
<p>O NetBeans IDE 6.5 introduz várias novas funcionalidades, incluindo uma IDE robusta para PHP, deputação de JavaScript para o Firefox e IE, e suporte a Groovy e Grails. Esse lançamento também inclui várias melhorias para o desenvolvimento em Java, Ruby e Rails, e C/C++. Dentre as melhorias no Java destacam-se: suporte nativo ao Hibernate, importação de projetos do Eclipse, e compilação no salvamento.</p>
<p>Links:</p>
<ul>
<li><a title="Download Now" href="http://download.netbeans.org/netbeans/6.5/beta/">Faça o Download</a></li>
<li><a title="Saiba Mais" href="http://www.netbeans.org/community/releases/65/">Saiba Mais</a></li>
<li><a href="http://www.netbeans.org/kb/index.html">Tutoriais &amp; Documentação</a></li>
</ul>
<p>Outros destaques:</p>
<ul>
<li> PHP
<ul>
<li>Completação de código</li>
<li>Consertos rápidos e checagem semântica</li>
<li>Suporte a FTP</li>
<li>Depuração com Xdebug</li>
<li>Suporte a Web Services populares</li>
</ul>
</li>
<li>Ajax/JavaScript
<ul>
<li>Suporte a depuração no Firefox e IE</li>
<li>Monitoramento cliente de HTTP</li>
<li>Vêm com as bibliotecas mais populares de JavaScript</li>
</ul>
</li>
<li>Java
<ul>
<li>Suporte a Groovy/Grails</li>
<li>Compilação/Deploy no momento do salvamento</li>
<li>Importação e sincronização de projetos do Eclipse</li>
<li>Suporte nativo a Hibernate</li>
<li>Gerador de CRUD JSF agora com Ajax</li>
</ul>
</li>
<li>Banco de Dados
<ul>
<li>Melhorias no editor</li>
</ul>
</li>
<li>C/C++
<ul>
<li>Melhorias na completação de código e destaque de erros</li>
<li>Desenvolvimento remoto</li>
</ul>
</li>
<li>Ruby
<ul>
<li>Suporte aos Testes Ruby</li>
<li>Melhoria no suporte a Rake</li>
</ul>
</li>
<li>GlassFish V3 &#8220;Prelude&#8221;
<ul>
<li>Menor tamanho, inicialização e deployment mais rápido</li>
<li>Suporte a scripting, inclusive jRuby</li>
</ul>
</li>
</ul>
<div id=":vc" class="ArwC7c ckChnd">
<div>
<div lang="x-western">
<p>O NetBeans IDE 6.5 final está planejado para ser lançado em Outubro de 2008. Como sempre, é bem vindo e nós encorajamos seu feedback sobre sua experiência usando a IDE NetBeans. Visite nossas <a href="http://www.netbeans.org/community/lists/top.html">listas de email</a> ou <a href="http://planetnetbeans.org/">faça uma postagem</a> no seu blog.</div>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2008/08/13/anuncio-do-netbeans-65-beta/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Pointers to functions in C++</title>
		<link>http://silveiraneto.net/2008/03/16/pointers-to-functions-in-c/</link>
		<comments>http://silveiraneto.net/2008/03/16/pointers-to-functions-in-c/#comments</comments>
		<pubDate>Sun, 16 Mar 2008 23:11:57 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Cpp]]></category>
		<category><![CDATA[pointers]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/2008/03/16/pointers-to-functions-in-c/</guid>
		<description><![CDATA[I need to implements some codes in C++. Just remembering some concepts like pointers to functions. A simple example: #include &#60;stdlib.h&#62; #include &#60;iostream&#62; using namespace std; double evalFunction(double (*f)(double), double param){ return f(param); } double function(double x){ return x/2; } int main(int argc, char** argv) { cout &#60;&#60; function(5.0) &#60;&#60; endl; cout &#60;&#60; evalFunction(function, 5.0) [...]]]></description>
			<content:encoded><![CDATA[<p>I need to implements some codes in C++. Just remembering some concepts like pointers to functions.</p>
<p>A simple example:</p>
<pre name="code" class="cpp">
#include &lt;stdlib.h&gt;
#include &lt;iostream&gt;

using namespace std;

double evalFunction(double (*f)(double), double param){
    return f(param);
}

double function(double x){
    return x/2;
}

int main(int argc, char** argv) {
    cout &lt;&lt; function(5.0) &lt;&lt; endl;
    cout &lt;&lt; evalFunction(function, 5.0) &lt;&lt; endl;
    return (EXIT_SUCCESS);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2008/03/16/pointers-to-functions-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

