<?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>Mon, 19 Jul 2010 21:59:29 +0000</lastBuildDate>
	<language>pt-br</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<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>2</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>0</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>6</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>6</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>6</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>
		<item>
		<title>Gerando permutações</title>
		<link>http://silveiraneto.net/2008/01/19/gerando-permutacoes/</link>
		<comments>http://silveiraneto.net/2008/01/19/gerando-permutacoes/#comments</comments>
		<pubDate>Sat, 19 Jan 2008 14:53:15 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[ia]]></category>
		<category><![CDATA[inteligência artificial]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[listas]]></category>
		<category><![CDATA[Prolog]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Wladimir]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/2008/01/19/gerando-permutacoes/</guid>
		<description><![CDATA[Muitas vezes para resolver uma única instância de um problema é mais rápido ataca-lo com força bruta do que encontrar um algoritmo geral com uma boa ordem de complexidade. Permutações são de grande utilidade nesse tipo de abordagem. Permutações em Prolog: Esse é um código em Prolog que o Wladimir Araujo passou na cadeira de [...]]]></description>
			<content:encoded><![CDATA[<p>Muitas vezes para resolver uma única instância de um problema é mais rápido ataca-lo com força bruta do que encontrar um algoritmo geral com uma boa ordem de complexidade. Permutações são de grande utilidade nesse tipo de abordagem.</p>
<p><span style="font-weight: bold">Permutações em Prolog:</span></p>
<p>Esse é um código em Prolog que o <a href="http://lia.ufc.br/~wladimir/">Wladimir Araujo</a> passou na cadeira de <a href="/tag/inteligencia-aritficial/">IA</a>.</p>
<div class="wp_syntax">
<div class="code">
<pre class="prolog" style="font-family:monospace;">select(X, [X|Xs], Xs).
select(X, [Y|Ys], [Y|Zs]) :- select(X, Ys, Zs).
&nbsp;
permutar([], []).
permutar(Xs, [Z|Zs]) :-
    select(Z, Xs, Ys),
    permutar(Ys, Zs).</pre>
</div>
</div>
<p><span style="font-weight: bold">Permutações em Python:</span><br />
Esse é um código de um certo Michael Davies que eu tirei <a href="http://snippets.dzone.com/posts/show/753">daqui</a>. Ele gera uma lista com todas as permutações de uma lista. Muito bonitinho. <img src='http://silveiraneto.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="wp_syntax">
<div class="code">
<pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> all_perms<span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span>=<span style="color: #ff4500;">1</span>:
        <span style="color: #ff7700;font-weight:bold;">yield</span> <span style="color: #008000;">str</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> perm <span style="color: #ff7700;font-weight:bold;">in</span> all_perms<span style="color: black;">&#40;</span><span style="color: #008000;">str</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;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>perm<span style="color: black;">&#41;</span>+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
                <span style="color: #ff7700;font-weight:bold;">yield</span> perm<span style="color: black;">&#91;</span>:i<span style="color: black;">&#93;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#91;</span>0:<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> + perm<span style="color: black;">&#91;</span>i:<span style="color: black;">&#93;</span></pre>
</div>
</div>
<p>Um exemplo de uso:</p>
<div class="wp_syntax">
<div class="code">
<pre class="python python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">for</span> p <span style="color: #ff7700;font-weight:bold;">in</span> all_perms<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'a'</span>,<span style="color: #483d8b;">'b'</span>,<span style="color: #483d8b;">'c'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">print</span> p
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'a'</span>, <span style="color: #483d8b;">'b'</span>, <span style="color: #483d8b;">'c'</span><span style="color: black;">&#93;</span>
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'b'</span>, <span style="color: #483d8b;">'a'</span>, <span style="color: #483d8b;">'c'</span><span style="color: black;">&#93;</span>
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'b'</span>, <span style="color: #483d8b;">'c'</span>, <span style="color: #483d8b;">'a'</span><span style="color: black;">&#93;</span>
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'a'</span>, <span style="color: #483d8b;">'c'</span>, <span style="color: #483d8b;">'b'</span><span style="color: black;">&#93;</span>
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'c'</span>, <span style="color: #483d8b;">'a'</span>, <span style="color: #483d8b;">'b'</span><span style="color: black;">&#93;</span>
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'c'</span>, <span style="color: #483d8b;">'b'</span>, <span style="color: #483d8b;">'a'</span><span style="color: black;">&#93;</span></pre>
</div>
</div>
<p><strong>Outras implementações:</strong><br />
Em outras linguagens o código para gerar permutações geralmente é muito grande, então eu preferi deixar alguns links.</p>
<ul>
<li><a href="http://www.bearcave.com/random_hacks/permute.html">Permutações em C</a></li>
<li><a href="http://www.merriampark.com/perm.htm">Permutações em Java</a></li>
<li><a href="http://snippets.dzone.com/posts/show/3332">Permutações em Ruby</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2008/01/19/gerando-permutacoes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XII Maratona Brasileira de Programação</title>
		<link>http://silveiraneto.net/2007/09/23/xii-maratona-brasileira-de-programacao/</link>
		<comments>http://silveiraneto.net/2007/09/23/xii-maratona-brasileira-de-programacao/#comments</comments>
		<pubDate>Sun, 23 Sep 2007 15:01:10 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Competição]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Maratona]]></category>
		<category><![CDATA[Programação]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/2007/09/23/xii-maratona-brasileira-de-programacao/</guid>
		<description><![CDATA[Esse sábado eu participei, junto com o Carlos Pontual e o Heraldo Carneiro, da décima segunda edição da Maratona Brasileira de Programação. A sede do Ceará na competição ia ser em Sobral, com o pessoal da Engenharia da Computação da UFC, mas acabou sendo na Unifor. Uma pena, eu queria ter viajado pra conhecer o [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/logomdp.jpg" alt="Logo da Maratona Brasileira de Programação" /></p>
<p>Esse sábado eu participei, junto com o Carlos Pontual e o <a href="http://heraldocarneiro.com/" title="Site do Heraldo">Heraldo Carneiro</a>, da décima segunda edição da <a href="http://maratona.ime.usp.br/" title="Maratona Brasileira de Programação">Maratona Brasileira de Programação</a>.</p>
<p style="text-align: center"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/maratona_brasileira_de_programacao_unifor_ufc_uece_baloes.jpg" alt="Maratona Brasileira de Programação" /></p>
<p>A sede do Ceará na competição ia ser em Sobral, com o pessoal da Engenharia da Computação da UFC, mas acabou sendo na Unifor. Uma pena, eu queria ter viajado pra conhecer o curso novo.</p>
<p>Embora antigamente eu tenha competido na OBI (Olimpíada Brasileira de Informática), eu nunca havia competido na Maratona Brasileira de Computação. Enquanto a OBI é uma competição voltada para alunos do ensino médio e básico a maratona é voltada para alunos do ensino superior da graduação e mestrado. Pelas minhas contas já faziam aí uns 3 anos que eu não competia.</p>
<p>Para quem não conhece esse tipo de competição, funciona assim: uma pessoa ou uma equipe (dependendo da competição) tem um certo tempo para resolver uma série de problemas usando programação. A correção do programa é automatizada. Seu programa é testado através de uma bateria de testes e deve retornar as respostas corretas. É uma ótima forma de melhorar seus conhecimentos sobre grafos, lógica, programação dinâmica, estruturas de dados, programação etc. Também é uma ótima oportunidade para conhecer ou rever o pessoal dos cursos de computação.</p>
<p style="text-align: center"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/heraldo_caneiro_silveira_neto_carlos_pontual.jpg" alt="Heraldo Carneiro, Silveira Neto e Carlos Pontual" height="361" width="479" /></p>
<p>Bem, vamos aos problemas que nós fizemos:</p>
<ul>
<li><strong>Varetas</strong>, problema H, era um problema bem simples. Esse nós fizemos em C e foi aceito de primeira.</li>
</ul>
<ul>
<li><strong>Histórico</strong>, problema E, também um problema não muito complicado. Mas foi por ele que nós nos enrolamos. Nós resolvemos o problema em Java e submetemos, mas a correção deu runtime error para ele. Nós re analisamos o problema, modificamos o programa e mandamos novamente e ganhamos outro runtime error. Como nós estávamos bem confiantes que nossa resposta estava certa nós refizemos o programa em C e submetemos. Dessa vez o programa passou sem problemas. Mais Tarde ficamos sabendo que devido a um erro da correção automática, não havia como um programa em Java ter acertado essa questão. Isso fez que passemos 1 hora e 44 minutos nesse problema.</li>
</ul>
<ul>
<li><strong>Rouba</strong>, problema B, basicamente um problema para simular um jogo de cartas. O Heraldo pegou esse problema e fez ele em Java. Depois de 3 submissões e 3 time limit exceeded da correção automática, nós estávamos certos que nosso programa estava correto. Nós já haviamos feitas varias otimizações de velocidade no programa. Havia agora três alternativas: ou abandonar o problema e tentar outra questão ou continuar a otimizar o programa ou refaze-lo em C. Até tentamos sair do problema, mas ele não saiu da cabeça do Heraldo <img src='http://silveiraneto.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Refaze-lo em C implicaria em implementar uma série de estruturas na unha, o que iria ser muito chato e não havia certeza que isso ia resolver nossa vida.Por fim o Heraldo fez mais um última pequena otimização no programa e ele passou.</li>
</ul>
<p>Nós ainda tentamos sem sucesso resolver os problemas Mário (o problema do armário hehehe) e o Zak.</p>
<p>Algumas estatísticas (parciais) da sede do Ceará:</p>
<table border="1">
<tr>
<td><strong>Problema</strong></td>
<td><strong>Submissões</strong></td>
<td><strong>Aceitos</strong></td>
</tr>
<tr>
<td>Histórico</td>
<td>27</td>
<td>9 (33%)</td>
</tr>
<tr>
<td>Rouba</td>
<td>17</td>
<td>6 (35%)</td>
</tr>
<tr>
<td>Tubos</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td>Volei</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td>Zak</td>
<td>6</td>
<td>2 (33%)</td>
</tr>
<tr>
<td>bolhas</td>
<td>4</td>
<td>0 (0%)</td>
</tr>
<tr>
<td>caixas</td>
<td>23</td>
<td>5 (22%)</td>
</tr>
<tr>
<td>mario</td>
<td>8</td>
<td>2 (25%)</td>
</tr>
<tr>
<td>olimp</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>varetas</td>
<td>17</td>
<td>10 (59%)</td>
</tr>
</table>
<p>Equipes e problemas resolvidos:</p>
<table border="1">
<tr>
<td><strong>Equipe</strong></td>
<td><strong>Resolvidos</strong></td>
<td><strong>Problemas</strong></td>
</tr>
<tr>
<td>UECE &#8211; Camila, Tainara, Leonilia</td>
<td>6</td>
<td>Rouba (7/207), mario (1/172), Histórico (1/41), caixas (1/233), varetas (1/49), Zak (2/273)</td>
</tr>
<tr>
<td>UECE &#8211; Die aphthe schmerzen</td>
<td>5</td>
<td>Rouba (1/161), mario (2/0-), Histórico (1/47), caixas (2/223), varetas (1/35), Zak (4/186)</td>
</tr>
<tr>
<td>AVL Team</td>
<td>5</td>
<td>Rouba (1/135), mario (1/277), Histórico (1/77), caixas (5/296), varetas (1/67)</td>
</tr>
<tr>
<td>Os Entrevistados</td>
<td>4</td>
<td>Rouba (1/90), Tubos (1/-), Histórico (1/79), caixas (2/182), varetas (1/66)</td>
</tr>
<tr>
<td>GOF</td>
<td>4</td>
<td>Rouba (2/73), Histórico (5/151), Caixas (5/289), Varetas (1/39)</td>
</tr>
<tr>
<td>Eupodiatamatando</td>
<td>3</td>
<td>Rouba (4/181), Mário (4/-), Histórico (3/143), Varetas (1/39)</td>
</tr>
<tr>
<td>UECE &#8211; n^n</td>
<td>2</td>
<td>Rouba (1/-), Histórico (1/90), caixas (1/-), varetas (2/122)</td>
</tr>
<tr>
<td>UECE &#8211; n!</td>
<td>2</td>
<td>Histórico (2/206), varetas (1/61)</td>
</tr>
<tr>
<td>unifor2</td>
<td>2</td>
<td>Histórico (2/206), varetas (1/61)</td>
</tr>
<tr>
<td>Mazela.cpp</td>
<td>1</td>
<td>Vôlei (1/-), Histórico (4/-), caixas (7/-), varetas (1/71)</td>
</tr>
<tr>
<td>unifor1</td>
<td>1</td>
<td>Histórico (2/-), Varetas 6</td>
</tr>
</table>
<p><strong>Observações:</strong> A equipe Singularidade de Sobral não estava presente lá, eu não sei se eles competiram. As equipes da UECE tiveram a boa idéia de colocar o nome da faculdade no nome da equipe.</p>
<p>Eu gostei muito dos resultados. Tivemos muitas equipes com bons resultados. Isso demonstra que os esforços, principalmente do Joel Uchôa,  em divulgar e particularizar a competição estão sendo frutíferos. Todas as universidades conseguiram bons resultados. Ano que vem eu espero ver mais universidades competindo.</p>
<p><strong>Sugestões para a organização:</strong></p>
<ul>
<li><strong>Linguagens:</strong> segundo o Joel Uchôa me informou há planos para inserir novas linguagens na competição. Fica minha sugestão para que Python e Ruby sejam incluídas.</li>
<li><strong>Java: </strong>me parece que há um longo histórico de problemas com a correção de programas em Java, sendo inclusive o uso desta desaconselhado por alguns. Eu espero que isso seja melhorado na próxima edição. Eu e minha equipe tivemos sérios problemas por conta disso mas nem por isso tenho planos de usar outra linguagem na próxima edição.</li>
<li><strong>Distribuição: </strong>há uma distribuição GNU/Linux própria para a competição o <a href="http://www.ime.usp.br/~cassio/boca/" title="Maratona Linux">Maratona Linux</a>. Ele tem várias sacadas legais como redes separadas para que nenhuma equipe possa usar a Internet ou enxergar as outras equipes, boot remoto etc. Porém é necessário boot pelo disquete o que tem sido uma fonte constante de problemas. O sistema de janelas <a href="www.windowmaker.info/" title="Window Maker">WindowMaker</a> também é fonte de confusão com usuários iniciantes, se é realmente necessário um sistema minimalista eu recomendaria o Fluxbox, XFCE ou Icewm.</li>
<li><strong>Correção: </strong>eu não gosto do esquema de correção da Maratona. Eu prefiro o da OBI. Na OBI há varias baterias de testes, cuidadosamente preparadas para filtrar cada tipo de erro ou algoritmos. Cada acerto em uma bateria resulta em pontuação. Já na maratona ou se acerta uma questão completamente ou ela está totalmente errada. Isso impede algoritmos mais triviais, algoritmos com complexidade alta (os não polinomiais) e impede também usar técnicas de Inteligência Artificial. Acho que isso interfere muito na forma de se elaborar os problemas e de se resolver os problemas. No universo dos problemas reais, nem tudo pode ser resolvido em tempo e espaço polinomial. Esse é o universo em que vivemos.</li>
</ul>
<p><strong>Fotos:</strong></p>
<p><center><br />
<a href="http://silveiraneto.net/wp-content/uploads/2007/09/p9210020.JPG" title="almoço"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/p9210020.thumbnail.JPG" alt="almoço" /></a> <a href="http://silveiraneto.net/wp-content/uploads/2007/09/p9210021.JPG" title="Almoço"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/p9210021.thumbnail.JPG" alt="Almoço" /></a> <a href="http://silveiraneto.net/wp-content/uploads/2007/09/p9210023.JPG" title="Maratona Brasileira de Programação"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/p9210023.thumbnail.JPG" alt="Maratona Brasileira de Programação" /></a> <a href="http://silveiraneto.net/wp-content/uploads/2007/09/p9210024.JPG" title="Maratona Brasileira de Programação"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/p9210024.thumbnail.JPG" alt="Maratona Brasileira de Programação" /></a>  <a href="http://silveiraneto.net/wp-content/uploads/2007/09/p9220027.JPG" title="Maratona Brasileira de Programação"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/p9220027.thumbnail.JPG" alt="Maratona Brasileira de Programação" /></a> <a href="http://silveiraneto.net/wp-content/uploads/2007/09/p9220028.JPG" title="Maratona Brasileira de Programação Mesa desorganizada"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/p9220028.thumbnail.JPG" alt="Maratona Brasileira de Programação Mesa desorganizada" /></a> <a href="http://silveiraneto.net/wp-content/uploads/2007/09/p9220030.JPG" title="Maratona Brasileira de Programação"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/p9220030.thumbnail.JPG" alt="Maratona Brasileira de Programação" /></a> <a href="http://silveiraneto.net/wp-content/uploads/2007/09/p9220032.JPG" title="Maratona Brasileira de Programação"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/p9220032.thumbnail.JPG" alt="Maratona Brasileira de Programação" /></a> <a href="http://silveiraneto.net/wp-content/uploads/2007/09/p9220033.JPG" title="Maratona Brasileira de Programação"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/p9220033.thumbnail.JPG" alt="Maratona Brasileira de Programação" /></a> <a href="http://silveiraneto.net/wp-content/uploads/2007/09/p9220034.JPG" title="Maratona Brasileira de Programação"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/p9220034.thumbnail.JPG" alt="Maratona Brasileira de Programação" /></a> <a href="http://silveiraneto.net/wp-content/uploads/2007/09/p9220035.JPG" title="Maratona Brasileira de Programação"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/p9220035.thumbnail.JPG" alt="Maratona Brasileira de Programação" /></a> <a href="http://silveiraneto.net/wp-content/uploads/2007/09/p9220040.JPG" title="Maratona Brasileira de Programação Heraldo Carneiro"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/p9220040.thumbnail.JPG" alt="Maratona Brasileira de Programação Heraldo Carneiro" /></a>  <a href="http://silveiraneto.net/wp-content/uploads/2007/09/p9220052.JPG" title="Maratona Brasileira de Programação balões laboratório computadores"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/p9220052.thumbnail.JPG" alt="Maratona Brasileira de Programação balões laboratório computadores" /></a> <a href="http://silveiraneto.net/wp-content/uploads/2007/09/p9220064.JPG" title="Maratona Brasileira de Programação Joel Uchôa"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/p9220064.thumbnail.JPG" alt="Maratona Brasileira de Programação Joel Uchôa" /></a> <a href="http://silveiraneto.net/wp-content/uploads/2007/09/p9220072.JPG" title="Maratona Brasileira de Programação Heraldo Carneiro Silveira Neto Carlos Pontual"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/p9220072.thumbnail.JPG" alt="Maratona Brasileira de Programação Heraldo Carneiro Silveira Neto Carlos Pontual" /></a> <a href="http://silveiraneto.net/wp-content/uploads/2007/09/p9220075.JPG" title="Maratona Brasileira de Programação confraternização"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/p9220075.thumbnail.JPG" alt="Maratona Brasileira de Programação confraternização" /></a> <a href="http://silveiraneto.net/wp-content/uploads/2007/09/p9220078.JPG" title="Maratona Brasileira de Programação confraternização"><img src="http://silveiraneto.net/wp-content/uploads/2007/09/p9220078.thumbnail.JPG" alt="Maratona Brasileira de Programação confraternização" /></a><br />
</center><br />
<strong>bônus:</strong> um vídeo que eu fiz quando já estava bem cansado. <a href="http://www.youtube.com/v/u8y8yQon8lI">Aqui</a>.</p>
<p><small>A Maratona Brasileira de Programação é uma realização da Sociedade Brasileira de Computação, USP, Fundação Carlos Chagas, IBM e diversas universidades e voluntários por todo o Brasi.<br />
</small></p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2007/09/23/xii-maratona-brasileira-de-programacao/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
