
<?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>daniel shiffman &#187; General</title>
	<atom:link href="http://www.shiffman.net/category/general/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.shiffman.net</link>
	<description></description>
	<lastBuildDate>Wed, 08 Feb 2012 03:00:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Night #5: thread() in Processing</title>
		<link>http://www.shiffman.net/2011/12/25/night-5-thread-in-processing/</link>
		<comments>http://www.shiffman.net/2011/12/25/night-5-thread-in-processing/#comments</comments>
		<pubDate>Sun, 25 Dec 2011 14:52:54 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[processing.org]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://www.shiffman.net/?p=1028</guid>
		<description><![CDATA[I just updated the Thread tutorial on the Processing wiki to include a little known function in Processing called thread(). It&#8217;s undocumented as of now, but will be a part of the 2.0 documentation. Here&#8217;s how it works. (This following &#8230; <a href="http://www.shiffman.net/2011/12/25/night-5-thread-in-processing/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I just updated the <a href="http://wiki.processing.org/w/Threading">Thread tutorial</a> on the Processing wiki to include a little known function in Processing called thread().  It&#8217;s undocumented as of now, but will be a part of the 2.0 documentation.  Here&#8217;s how it works.</p>
<p>(This following is excerpted from the tutorial).</p>
<p>You are likely familiar with the idea of writing a program that follows a specific sequence of steps &#8212; setup() first then draw() over and over and over again! A Thread is also a series of steps with a beginning, a middle, and an end. A Processing sketch is a single thread, often referred to as the &#8220;Animation&#8221; thread. Other threads sequences, however, can run independently of the main &#8220;Processing&#8221; sketch. In fact, you can launch any number of threads at one time and they will all run concurrently.</p>
<p>Processing does this all the time, whenever you write an event callback, such as serialEvent(), or captureEvent(), etc. these functions are triggered by a different thread running behind the scenes, and they alert Processing whenever they have something to report. This is useful whenever you need to perform a task that takes too long and would slow down the main animation&#8217;s frame rate, such as grabbing data from the network (XML, database, etc.) If a separate thread gets stuck or has an error, the entire program won&#8217;t grind to a halt, since the error only stops that individual thread. To create independent, asynchronous threads, you can use the thread() function built into Processing.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  size<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span>,<span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// This will tell someFunction() to execute now as a separate thread</span>
  thread<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;someFunction&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> draw<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> someFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// This function will run as a thread when called via</span>
  <span style="color: #666666; font-style: italic;">// thread(&quot;someFunction&quot;) as it was in setup!</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The thread() function receives a String as an argument. The String should match the name of the function you want to run as a thread.   While using the thread() function is a very simple way of getting an independent thread, it is somewhat limited. Being able to make a thread object is a great deal more powerful, and this can be done by extending java&#8217;s <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html">Thread</a> class.</p>
<p>For more about how to do that, take a look at <a href="http://wiki.processing.org/w/Threading">the full tutorial</a>.</p>
<p>Following is an example draws a loading bar in the &#8220;animation&#8221; thread that reports progress on another thread().  This is a nice demonstration, however, it would not be necessary in a sketch where you wanted to load data in the background and hide this from the user, allowing the draw() loop to simply continue.</p>
<p><a href='http://www.shiffman.net/wp/wp-content/uploads/2011/12/Threads.zip'><img src="http://www.shiffman.net/wp/wp-content/uploads/2011/12/threads.png" alt="" title="threads" width="500" height="298" class="alignnone size-full wp-image-1029" /></a></p>
<p><a href='http://www.shiffman.net/wp/wp-content/uploads/2011/12/Threads.zip'>Threads.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.shiffman.net/2011/12/25/night-5-thread-in-processing/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Night #4: Sorting the Vertices of a Polygon</title>
		<link>http://www.shiffman.net/2011/12/23/night-4-sorting-the-vertices-of-a-polygon/</link>
		<comments>http://www.shiffman.net/2011/12/23/night-4-sorting-the-vertices-of-a-polygon/#comments</comments>
		<pubDate>Sat, 24 Dec 2011 00:27:13 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[arraylist]]></category>
		<category><![CDATA[polygon]]></category>
		<category><![CDATA[processing.org]]></category>
		<category><![CDATA[selection sort]]></category>

		<guid isPermaLink="false">http://www.shiffman.net/?p=1005</guid>
		<description><![CDATA[The following problem came up in my ICM course this year. A student was working on a sketch that involved tiling polygons arbitrarily drawn by a user. Allowing a user to set the vertices of a polygon should be easy &#8230; <a href="http://www.shiffman.net/2011/12/23/night-4-sorting-the-vertices-of-a-polygon/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The following problem came up in my ICM course this year.  A student was working on a sketch that involved tiling polygons arbitrarily drawn by a user.  Allowing a user to set the vertices of a polygon should be easy enough, right?  But what if the user does not happen to draw them in a nice clock-wise (or counter clock-wise) order?</p>
<p>Imagine for a moment, you have an ArrayList of PVectors called &#8220;vertices.&#8221;  When the user clicks, the mouse you could add that mouse location to that ArrayList.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">void</span> mousePressed<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  vertices.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PVector<span style="color: #009900;">&#40;</span>mouseX,mouseY<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You could then draw that list as a polygon using beginShape() and endShape().</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">void</span> draw<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  beginShape<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>PVector v <span style="color: #339933;">:</span> vertices<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    vertex<span style="color: #009900;">&#40;</span>v.<span style="color: #006633;">x</span>, v.<span style="color: #006633;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> 
  endShape<span style="color: #009900;">&#40;</span>CLOSE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>But depending on how the user set the vertices, you might end up with:</p>
<p><img src="http://www.shiffman.net/wp/wp-content/uploads/2011/12/unsorted.png" alt="" title="unsorted" width="500" height="298" class="alignnone size-full wp-image-1006" /></p>
<p>when what you really want is the following:</p>
<p><img src="http://www.shiffman.net/wp/wp-content/uploads/2011/12/sorted.png" alt="" title="sorted" width="500" height="298" class="alignnone size-full wp-image-1007" /></p>
<p>One solution for solving this problem is to always sort all of the vertices according to their relative angle from the center.  Let&#8217;s say you calculate the center of the polygon as the average location of all vertices.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  PVector centroid <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PVector<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>PVector v <span style="color: #339933;">:</span> vertices<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    centroid.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> 
  centroid.<span style="color: #006633;">div</span><span style="color: #009900;">&#40;</span>vertices.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can then make a vector that points from the center to each vertex and get its direction (using PVector&#8217;s heading2D() method).</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>PVector v <span style="color: #339933;">:</span> vertices<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    PVector dir <span style="color: #339933;">=</span> PVector.<span style="color: #006633;">sub</span><span style="color: #009900;">&#40;</span>v, centroid<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">float</span> a <span style="color: #339933;">=</span> dir.<span style="color: #006633;">heading2D</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> PI<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note how we add PI to the angle.  The heading2D() function will return an angle between -PI and PI and it&#8217;ll be easier to sort if we just have an angle between 0 and TWO_PI.  One way to sort an ArrayList is called a <a href="http://en.wikipedia.org/wiki/Selection_sort">Selection Sort</a>.  In the example below, you&#8217;ll find a variation of the selection sort.   The code iterates through the ArrayList, finds the element with the highest angle, removes that element and sticks it at the end of a new ArrayList.  It does this again and again until the original ArrayList is empty.  The result is a new ArrayList in sorted order.</p>
<p>Following is that example which implements all of the above into a class. If you are looking for an exercise, try allowing the user to move or delete existing vertices.   Also, how you would make a system of these polygons so that the user can draw more than one?</p>
<p><script type="application/processing">
// Daniel Shiffman
// Hanukkah 2011
// 8 nights of Processing examples
// http://www.shiffman.net


// A Polygon object
Poly p;

void setup() {
  size(586, 293);
  smooth();
  // An empty one
  p = new Poly();
}

void draw() {
  background(50);
  // Draw the polygon
  p.display();

  textAlign(CENTER);
  fill(255);
  text("click mouse to add vertices", width/2, height-16);
}

void keyPressed() {
  // Clear it when you press the mouse
  if (key == ' ') {
    p.clear();
  } 
  // If you want to see the polygon unsorted, comment
  // out the automatic sortVertices() in the class
  /*else if (key == 's') {
    p.sortVertices();
  }*/
}

// Add a vertex!
void mousePressed() {
  PVector mouse = new PVector(mouseX, mouseY);
  p.addVertex(mouse);
}

// Daniel Shiffman
// Hanukkah 2011
// 8 nights of Processing examples
// http://www.shiffman.net

// A class that generates a polygon sorted
// according to relative angle from center

class Poly {
  // A list of vertices
  ArrayList vertices;
  // The center
  PVector centroid;

  Poly() {
    // Empty at first
    vertices = new ArrayList();
    centroid = new PVector();
  }

  // We can clear the whole thing if necessary
  void clear() {
    vertices.clear();
  }


  // Add a new vertex
  void addVertex(PVector newVertex) {
    vertices.add(newVertex);
    // Whenever we have a new vertex
    // We have to recalculate the center
    // and re-sort the vertices
    updateCentroid();
    // Come out the sorting if you want to see it drawn incorrectly
    sortVertices();
  }

  // The center is the average location of all vertices
  void updateCentroid() {
    centroid = new PVector();
    for (int i = 0; i < vertices.size(); i++) {
      PVector v = (PVector) vertices.get(i);
      centroid.add(v);
    } 
    centroid.div(vertices.size());
  }


  // Sorting the ArrayList
  void sortVertices() {

    // This is something like a selection sort
    // Here, instead of sorting within the ArrayList
    // We'll just build a new one sorted
    ArrayList newVertices = new ArrayList();

    // As long as it's not empty
    while (!vertices.isEmpty ()) {
      // Let's find the one with the highest angle
      float biggestAngle = 0;
      PVector biggestVertex = null;
      // Look through all of them
    for (int i = 0; i < vertices.size(); i++) {
      PVector v = (PVector) vertices.get(i);
        // Make a vector that points from center
        PVector dir = PVector.sub(v, centroid);
        // What is it's heading
        // The heading function will give us values between -PI and PI
        // easier to sort if we have from 0 to TWO_PI
        float a = dir.heading2D() + PI;
        // Did we find it
        if (a > biggestAngle) {
          biggestAngle = a;
          biggestVertex = v;
        }
      }

      // Put the one we found in the new arraylist
      newVertices.add(biggestVertex);
      // Delete it so that the next biggest one 
      // will be found the next time
      vertices.remove(biggestVertex);
    }
    // We've got a new ArrayList
    vertices = newVertices;
  }

  // Draw everything!
  void display() {
    
    // First draw the polygon
    stroke(255);
    fill(255, 127);
    beginShape();
    for (int i = 0; i < vertices.size(); i++) {
      PVector v = (PVector) vertices.get(i);

      vertex(v.x, v.y);
    } 
    endShape(CLOSE);
    
    
    // Then we'll draw some addition information
    // at each vertex to show the sorting
    for (int i = 0; i < vertices.size(); i++) {
      
      // This is overkill, but we want the numbers to
      // appear outside the polygon so we extend a vector
      // from the center
      PVector v = (PVector) vertices.get(i);      
      PVector dir = PVector.sub(v, centroid);
      dir.normalize();
      dir.mult(12);
      
      // Number the vertices
      fill(255);
      stroke(255);
      ellipse(v.x, v.y, 4, 4);
      textAlign(CENTER);
      text(i, v.x+dir.x, v.y+dir.y+6);
    } 

   
    // Once we have two vertices draw the center
    if (vertices.size() > 1  ) {
      fill(255);
      ellipse(centroid.x, centroid.y, 8, 8);
      text("centroid", centroid.x, centroid.y+16);
    }
  }
}
</script></p>
<p>Source: <a href='http://www.shiffman.net/wp/wp-content/uploads/2011/12/PolygonVertexSorting.zip'>PolygonVertexSorting.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.shiffman.net/2011/12/23/night-4-sorting-the-vertices-of-a-polygon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Night #3: Regular Expressions in Processing</title>
		<link>http://www.shiffman.net/2011/12/22/night-3-regular-expressions-in-processing/</link>
		<comments>http://www.shiffman.net/2011/12/22/night-3-regular-expressions-in-processing/#comments</comments>
		<pubDate>Fri, 23 Dec 2011 02:25:34 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[processing.org]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://www.shiffman.net/?p=989</guid>
		<description><![CDATA[Several years ago I became somewhat obsessed with regular expressions while reading Jeffrey Friedl&#8217;s Mastering Regular Expressions. At the time, I wrote a short tutorial about regular expressions for my course Programming from A to Z. The sad truth is &#8230; <a href="http://www.shiffman.net/2011/12/22/night-3-regular-expressions-in-processing/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Several years ago I became somewhat obsessed with regular expressions while reading Jeffrey Friedl&#8217;s <a href="http://regex.info">Mastering Regular Expressions</a>.  At the time, I wrote <a href="http://www.shiffman.net/teaching/a2z/regex/">a short tutorial about regular expressions</a> for my course Programming from A to Z.  The sad truth is that if you&#8217;ve ever done regular expressions in Java, it&#8217;s pretty darn awkward compared to, say, python or perl.  The good news is there are some nice regex helper functions in Processing that can make it a bit easier.  Before we get to that let&#8217;s start with the Java API: </p>
<ul>
<li><a href="http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html">Pattern</a> &#8212; a compiled representation of a regular expression.</li>
<li><a href="http://docs.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html">Matcher</a> &#8212; an engine that performs match operations on a character sequence (or String) by interpreting a Pattern.</li>
</ul>
<p>An example of Pattern and Matcher in Java (which you can write directly into Processing) looks like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> inputtext <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;This is a test of regular expressions.&quot;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// Input text</span>
<span style="color: #003399;">String</span> regex <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #339933;">;</span>              <span style="color: #666666; font-style: italic;">// The regular expression to match</span>
Pattern p <span style="color: #339933;">=</span> Pattern.<span style="color: #006633;">compile</span><span style="color: #009900;">&#40;</span>regex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Making a Pattern object with the regex </span>
Matcher m <span style="color: #339933;">=</span> p.<span style="color: #006633;">matcher</span><span style="color: #009900;">&#40;</span>inputtext<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">// Matching that regex in the input string</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>m.<span style="color: #006633;">find</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>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>m.<span style="color: #006633;">group</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     <span style="color: #666666; font-style: italic;">// Here's the match!</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No match!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">// No match!</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Of course, in most cases, you want to do something more sophisticated where you iterate over many matches.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Regex that matches double words</span>
<span style="color: #666666; font-style: italic;">// Ugh, look at all these double back slashes!!</span>
<span style="color: #003399;">String</span> regex <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\\</span>b(<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\\</span>w+)<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\\</span>b<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\\</span>W+<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\\</span>1&quot;</span><span style="color: #339933;">;</span>   
&nbsp;
Pattern p <span style="color: #339933;">=</span> Pattern.<span style="color: #006633;">compile</span><span style="color: #009900;">&#40;</span>regex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     <span style="color: #666666; font-style: italic;">// Compile Regex</span>
Matcher m <span style="color: #339933;">=</span> p.<span style="color: #006633;">matcher</span><span style="color: #009900;">&#40;</span>content<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>         <span style="color: #666666; font-style: italic;">// Create Matcher</span>
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>m.<span style="color: #006633;">find</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>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>m.<span style="color: #006633;">group</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Processing provides some regex helper functions that wrap all of this Java Pattern/Matcher stuff.  They are <a href="http://processing.org/reference/match_.html">match()</a> and <a href="http://processing.org/reference/matchAll_.html">matchAll()</a>.  </p>
<p>The match() function is used to apply a regular expression to a piece of text, and return matching groups (elements found inside parentheses) as a String array. If there is no match, the function will return null. If no groups are specified in the regular expression, but the sequence matches, an array of length one (with the matched text as the first element of the array) will be returned. </p>
<p>Here&#8217;s an example (this is straight from the reference page).</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> s <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Inside a tag, you will find &lt;tag&gt;content&lt;/tag&gt;.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> m <span style="color: #339933;">=</span> match<span style="color: #009900;">&#40;</span>s, <span style="color: #0000ff;">&quot;&lt;tag&gt;(.*?)&lt;/tag&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
println<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Found '&quot;</span> <span style="color: #339933;">+</span> m<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;' inside the tag.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Prints to the console:</span>
<span style="color: #666666; font-style: italic;">// &quot;Found 'content' inside the tag.&quot;</span></pre></div></div>

<p>The matchAll() function is at first a bit confusing because it returns a two dimensional array.  But if you look right back to how match() works, it&#8217;s pretty simple.  match() assumes you want just one match, and gives you an array, a list of all the groups for that single match.  matchAll() assumes you want all the matches, so it gives you a bunch of those arrays, one for every match.   What&#8217;s an array of an array?  A two dimensional array!  The first dimension is the match itself, and the second dimension is the group for that match, i.e.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> s <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Some tags! &lt;tag&gt;one&lt;/tag&gt; &lt;tag&gt;two&lt;/tag&gt; &lt;tag&gt;three&lt;/tag&gt;.&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Match this regular expression</span>
<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> m <span style="color: #339933;">=</span> matchAll<span style="color: #009900;">&#40;</span>s, <span style="color: #0000ff;">&quot;&lt;tag&gt;(.*?)&lt;/tag&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Loop through all the matches     </span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> m.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// Print out group 1 for each match                </span>
  println<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Found '&quot;</span> <span style="color: #339933;">+</span> m<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;' inside a tag.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This new example uses a regex that matches anything inside an HTML href tag and draws it the screen.</p>
<p><a href='http://www.shiffman.net/wp/wp-content/uploads/2011/12/Regex.zip'><img src="http://www.shiffman.net/wp/wp-content/uploads/2011/12/regex.png" alt="" title="regex" width="500" height="298" class="alignnone size-full wp-image-994" /></a></p>
<p><a href='http://www.shiffman.net/wp/wp-content/uploads/2011/12/Regex.zip'>Regex.zip</a></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.shiffman.net/2011/12/22/night-3-regular-expressions-in-processing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Night #2: Fade Sound In and Out Using Minim</title>
		<link>http://www.shiffman.net/2011/12/21/night-2-fade-sound-in-and-out-using-minim/</link>
		<comments>http://www.shiffman.net/2011/12/21/night-2-fade-sound-in-and-out-using-minim/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 03:37:36 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[minim]]></category>
		<category><![CDATA[pcomp]]></category>
		<category><![CDATA[processing.org]]></category>
		<category><![CDATA[sound]]></category>

		<guid isPermaLink="false">http://www.shiffman.net/?p=978</guid>
		<description><![CDATA[I made this example earlier in the semester after seeing countless projects with the following functionality: turning a sound on when a sensor reading reaches a given level, turning the sound off when a sensor reading falls below a certain &#8230; <a href="http://www.shiffman.net/2011/12/21/night-2-fade-sound-in-and-out-using-minim/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I made this example earlier in the semester after seeing countless projects with the following functionality: turning a sound on when a sensor reading reaches a given level, turning the sound off when a sensor reading falls below a certain level.  Most the projects used Minim for sound playback and pause() and play() to start and stop a sound, along with rewind() to send the sound back to the beginning.  While this does work, I find a more effective strategy is to fade a sound in and out using shiftGain().</p>
<p>Now this is a fairly simple problem if you can pinpoint the moment a sound should fade in.  For example, let&#8217;s say you want it to happen when the mouse is clicked.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">void</span> mousePressed<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  player.<span style="color: #006633;">shiftGain</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">80</span>, <span style="color: #cc66cc;">13</span>, <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The above line of code will fade the sound over 1,000 milliseconds (i.e. 1 second) from a decibel level of -80 (inaudible) to 13 (some vaguely loud level.)</p>
<p>If you put this code in draw(), however, you&#8217;ve got a problem.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">void</span> draw<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sensorVal <span style="color: #339933;">&gt;</span> threshold<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    player.<span style="color: #006633;">shiftGain</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">80</span>, <span style="color: #cc66cc;">13</span>, <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can&#8217;t call shiftGain() over and over again!  You want this to happen just once, the moment the sensor value reaches that threshold.  Introducing a boolean is a quick way to solve this problem.  If you set the boolean to true when the sound is fading and only call shiftGain() when the boolean is set to false, you&#8217;ve now got only one call to the function.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">boolean</span> fade <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> draw<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sensorVal <span style="color: #339933;">&gt;</span> threshold <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>fade<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    player.<span style="color: #006633;">shiftGain</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">80</span>, <span style="color: #cc66cc;">13</span>, <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    fade <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The question remains: when does fade get set back to false?  One likely solution is to reset the boolean when the sensor value falls below the threshold, i.e.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sensorVal <span style="color: #339933;">&gt;</span> threshold <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>fade<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    player.<span style="color: #006633;">shiftGain</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">80</span>, <span style="color: #cc66cc;">13</span>, <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    fade <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sensorVal <span style="color: #339933;">&lt;</span> threshold<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    fade <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Following is an example that does this with a double threshold (fading up above a value and fading down below a value).  In addition, it offers some other improvements (such as having the sound fade from its current gain level). The mouseX location is the stand-in for a sensor value.</p>
<p><a href="http://www.shiffman.net/wp/wp-content/uploads/2011/12/SoundFade.zip"><img src="http://www.shiffman.net/wp/wp-content/uploads/2011/12/soundfade.png" alt="" title="soundfade" width="400" height="239" class="alignnone size-full wp-image-979" /></a></p>
<p>Source: <a href="http://www.shiffman.net/wp/wp-content/uploads/2011/12/SoundFade.zip">SoundFade.zip</a></p>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.shiffman.net/2011/12/21/night-2-fade-sound-in-and-out-using-minim/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Night #1: Zoom and Pan in 2D</title>
		<link>http://www.shiffman.net/2011/12/20/night-1-zoom-and-pan-in-2d/</link>
		<comments>http://www.shiffman.net/2011/12/20/night-1-zoom-and-pan-in-2d/#comments</comments>
		<pubDate>Wed, 21 Dec 2011 03:19:31 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[2D]]></category>
		<category><![CDATA[pan]]></category>
		<category><![CDATA[processing.org]]></category>
		<category><![CDATA[zoom]]></category>

		<guid isPermaLink="false">http://www.shiffman.net/?p=970</guid>
		<description><![CDATA[This came up in my course &#8220;Introduction to Computational Media&#8221; this year. How does one pan and/or zoom in a 2D Processing world? We could certainly introduce P3D into the mix, but there is a nice, elegant way we can &#8230; <a href="http://www.shiffman.net/2011/12/20/night-1-zoom-and-pan-in-2d/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This came up in my course &#8220;Introduction to Computational Media&#8221; this year.  How does one pan and/or zoom in a 2D Processing world?  We could certainly introduce P3D into the mix, but there is a nice, elegant way we can create the effect of panning and zooming and still live in 2D.  Here&#8217;s how it works.</p>
<p>First, you need to make sure you translate to the center of your window.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  translate<span style="color: #009900;">&#40;</span>width<span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span>, height<span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then you can use the <a href="http://processing.org/learning/basics/scale.html">scale()</a> function to scale the world according to a percentage (i.e. 2.0 is 200%, 0.5 is 50%).  We&#8217;ll use a variable called zoom.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000066; font-weight: bold;">float</span> zoom <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1.5</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// 150%</span>
  scale<span style="color: #009900;">&#40;</span>zoom<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then we can translate additionally to pan according to some offset.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000066; font-weight: bold;">float</span> offsetX <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// Some arbitrary offset</span>
  <span style="color: #000066; font-weight: bold;">float</span> offsetY <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  translate<span style="color: #009900;">&#40;</span>offsetX,offsetY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here is an example (running in processing.js) that allows the user to pan around a design by dragging the mouse, and zoom in and out using key presses.</p>
<p><script type="application/processing">
// The scale of our world
float zoom;
// A vector to store the offset from the center
PVector offset;
// The previous offset
PVector poffset;
// A vector for the mouse position
PVector mouse;

void setup() {
  size(586, 293);
  zoom = 1.0;
  offset = new PVector(0, 0);
  poffset = new PVector(0, 0);

  smooth();
}

void draw() {
  background(50);
  pushMatrix();
  // Everything must be drawn relative to center
  translate(width/2, height/2);
  
  // Use scale for 2D "zoom"
  scale(zoom);
  // The offset (note how we scale according to the zoom)
  translate(offset.x, offset.y);
  
  // An arbitrary design so that we have something to see!
  randomSeed(1);
  for (int i = 0; i < 500; i++) {
    stroke(255);
    fill(255,50);
    rectMode(CENTER);
    float h = 100;
    if (random(1) < 0.5) {
      rect(random(-h,h),random(-h,h),12,12);
    } else {
      ellipse(random(-h,h),random(-h,h),12,12);
    } 
  }
  popMatrix();
  
  // Draw some text (not panned or zoomed!)
  fill(255);
  text("a: zoom in\nz: zoom out\ndrag mouse to pan",10,32);
  
  
}

// Zoom in and out when the key is pressed
void keyPressed() {
  if (key == 'a') {
    zoom += 0.1;
  } 
  else if (key == 'z') {
    zoom -= 0.1;
  }
  zoom = constrain(zoom,0,100);
}

// Store the mouse and the previous offset
void mousePressed() {
  mouse = new PVector(mouseX, mouseY);
  poffset.set(offset);
}

// Calculate the new offset based on change in mouse vs. previous offsey
void mouseDragged() {
  offset.x = (mouseX - mouse.x)/zoom + poffset.x;
  offset.y = (mouseY - mouse.y)/zoom + poffset.y;
}

</script></p>
<p>Source: <a href='http://www.shiffman.net/wp/wp-content/uploads/2011/02/PanZoom2D.zip'>PanZoom2D.zip</a></p>
<p></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.shiffman.net/2011/12/20/night-1-zoom-and-pan-in-2d/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Hanukkah: 8 Nights of Processing</title>
		<link>http://www.shiffman.net/2011/12/20/hanukkah-8-nights-of-processing/</link>
		<comments>http://www.shiffman.net/2011/12/20/hanukkah-8-nights-of-processing/#comments</comments>
		<pubDate>Tue, 20 Dec 2011 19:31:05 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.shiffman.net/?p=951</guid>
		<description><![CDATA[Hello world. I have completely failed to remember I have a blog. Well, I suppose it wasn&#8217;t a failure of memory, rather it just fell out of my routine to keep things up-to-date. When you have one of these jobs &#8230; <a href="http://www.shiffman.net/2011/12/20/hanukkah-8-nights-of-processing/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Hello world.</p>
<p>I have completely failed to remember I have a blog.  Well, I suppose it wasn&#8217;t a failure of memory, rather it just fell out of my routine to keep things up-to-date.  When you have one of these jobs at an academic institution, one thing they require you to do every so often is put together a big pile of paper that provides evidence you are contributing member of society.  The way I&#8217;ve always managed to do that in the past is by scanning through all my old posts to find out if I have in fact done anything.  According to this blog, I have not existed since April 2011.  I&#8217;m going to try to remedy that.  The first thing I&#8217;m going to do is post a list of things I&#8217;ve been up to in the last seven or eight months (this is for me and anyone reading should really just stop reading).  The second thing I&#8217;m going to do (starting tonight) is simultaneously attempt to revive this blog and celebrate the festival of lights by posting eight nights of Processing examples.  I&#8217;ve made a few new ones for the upcoming Processing 2.0 release as well as helped out students on a number of different problems over the course of the fall semester.  So hopefully this can be a place where I can document and share some of these examples in the hopes that they will help others.</p>
<p>So first, the part everyone should feel free to ignore. . .</p>
<ul>
<li>April 2011. I visited my good friend <a href="http://www.talespin.com/">Chris Ault</a> at The College of New Jersey and gave a presentation about the Nature of Code and Most Pixels Ever and saw several demos of student work.</li>
<li>June 2011. I attended Seb Lee-Delisle&#8217;s <a href="http://sebleedelisle.com/training/">Creative JS</a> course in NYC.  I made this: <a href="http://www.shiffman.net/creativejs/particles.html">fibonacci particles</a></li>
<li>June 17-20 2011. I participated in the Processing 2.0 summit. Ben Fry, Casey Reas, Andres Colubri, Jer Thorp, Patrick Hebron, and I worked together to define the 2.0 release.  (Thanks to Casey for the photos below.)</li>
<p><img src="http://reas.com/blog/wp-content/uploads/2011/07/itp2.jpg" alt="Processing 2.0" /><br />
<img alt="Processing 2.0" src="http://reas.com/blog/wp-content/uploads/2011/07/itp1.jpg" /></p>
<li>Summer 2011. I participated in Google Summer of Code as a mentor for Processing.</li>
<li>September 2011. Launched the &#8220;Magic Book&#8221; project with Rune Madsen and Miguel Bermudez at ITP (stay tuned for more about this).</li>
<li>October 2011. I attended IndieCade in Los Angeles and participated in a panel discussion with Zach Gage, Robert Hodgin, and Casey Reas, and Daniel Shiffman.  We discussed projects which use code as a generator of form, and explore the various ways in which procedurally generated elements can contribute to the mechanics, dynamics, and/or aesthetics of video games. (<a href="http://www.youtube.com/watch?v=edor4shC-pY">video of the panel</a>).</li>
<li>October 2011. Attended Art &#038;&#038; Code 3D and was proud to be Greg Borenstein&#8217;s helper for his workshop <a href="http://artandcode.com/3d/workshops/1a-using-the-kinect-with-processing">Using the Kinect with Processing</a>.  Also really enjoyed Stephen Howell&#8217;s Scratch + Kinect workshop.</li>
<li>November 2011. Began development on a <a href="http://www.itpcakemix.com/">CakeMix</a> Processing library. Source on <a href="https://github.com/shiffman/CakeMix-Processing">github</a>.
</li>
<li>Fall 2011. Technical edited Greg Borenstein&#8217;s upcoming book: <a href="http://shop.oreilly.com/product/0636920020684.do">Making Things See</a>.</li>
<li>December 2011. Gave a presentation at the Neuberger Museum of Art as part of the <a href="https://drupalsites.purchase.edu/newmedia/index.php?q=node/150">New Media Lecture Series</a></li>
<li>December 2011.  Completing work on Processing 2.0.  New examples, new tutorials. Stay tuned for further updates</li>
<p>.
</ul>
<p>Next up, 8 Processing examples!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shiffman.net/2011/12/20/hanukkah-8-nights-of-processing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>OpenCV Matching Faces Over Time</title>
		<link>http://www.shiffman.net/2011/04/26/opencv-matching-faces-over-time/</link>
		<comments>http://www.shiffman.net/2011/04/26/opencv-matching-faces-over-time/#comments</comments>
		<pubDate>Tue, 26 Apr 2011 15:29:13 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[opencv]]></category>
		<category><![CDATA[processing.org]]></category>

		<guid isPermaLink="false">http://www.shiffman.net/?p=852</guid>
		<description><![CDATA[One of the most common questions I get regarding blob tracking is &#8220;memory.&#8221; How do I know which blob is which over time? Computer vision libraries, for the most part, simply pass you a list of blobs (with x, y, &#8230; <a href="http://www.shiffman.net/2011/04/26/opencv-matching-faces-over-time/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><iframe src="http://player.vimeo.com/video/22873042?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=0" width="398" height="299" frameborder="0"></iframe></p>
<p>One of the most common questions I get regarding blob tracking is &#8220;memory.&#8221;   How do I know which blob is which over time?  Computer vision libraries, for the most part, simply pass you a list of blobs (with x, y, width, and height properties) for any given moment in time.  But the blobs themselves represent only a snapshot of that particular moment and contain no information related to whether the blobs existed before this very moment.   This may seem absurd given that as human beings it&#8217;s so easy for us to watch a rectangle moving across a screen and understand conceptually that it is the same rectangle.  But without additional information (such as color matching, an AR marker, etc.) there&#8217;s no way for an algorithm that analyzes one frame of video to know anything about a previous frame.  And so we need to apply the same intuitions our brain uses (it was there a moment ago, it&#8217;s probably still there now) to our algorithms.  </p>
<p>To illustrate one solution to this problem, I&#8217;ve created an example that tags an OpenCV face &#8220;rectangle&#8221; with an ID number and attempts to track that face over time, matching new faces that OpenCV finds with earlier ones.  This example is somewhat of an oversimplification whose purpose is to demonstrate a particular technique &#8212; a new face is the same as the previous one that was closest to it.  But there are certainly additional and more sophisticated ways that the match could be made.  In addition, it&#8217;s likely useful to add some interpolation to the face&#8217;s movement and size changes so that it appears less jittery.</p>
<p>First, we need to establish our own Face class.  OpenCV just gives us a new array of Rectangle objects every frame so we need our own Face object that persists (in an ArrayList).</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Face <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// A Rectangle</span>
  <span style="color: #003399;">Rectangle</span> r<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Am I available to be matched?</span>
  <span style="color: #000066; font-weight: bold;">boolean</span> available<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Should I be deleted?</span>
  <span style="color: #000066; font-weight: bold;">boolean</span> delete<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// How long should I live if I have disappeared?</span>
  <span style="color: #000066; font-weight: bold;">int</span> timer <span style="color: #339933;">=</span> <span style="color: #cc66cc;">127</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Assign a number to each face</span>
  <span style="color: #000066; font-weight: bold;">int</span> id<span style="color: #339933;">;</span></pre></div></div>

<p>Our main program then needs an ArrayList to keep track of the Face objects that currently exist:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">ArrayList</span> faceList<span style="color: #339933;">;</span></pre></div></div>

<p>Finally, in draw(), OpenCV gives us an array of Rectangle objects, the faces it currently sees.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #003399;">Rectangle</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> faces <span style="color: #339933;">=</span> opencv.<span style="color: #006633;">detect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It&#8217;s our job to match these with any Face objects we have in our ArrayList.  The way I see it, there are three scenarios.</p>
<p>1) We have nothing in our Face ArrayList.  In this case, we add a new Face object for every single Rectangle in the faces array, i.e.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>faceList.<span style="color: #006633;">isEmpty</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>
    <span style="color: #666666; font-style: italic;">// Just make a Face object for every face Rectangle</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> faces.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      faceList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Face<span style="color: #009900;">&#40;</span>faces<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">x</span>,faces<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">y</span>,faces<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">width</span>,faces<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">height</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>2) OpenCV found more faces than we currently have in our list.   In this case, we need to match our current Face objects with an OpenCV Rectangle and then add new Face objects for any remaining Rectangles.</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>faceList.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #339933;">=</span> faces.<span style="color: #006633;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">boolean</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> used <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">boolean</span><span style="color: #009900;">&#91;</span>faces.<span style="color: #006633;">length</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Match existing Face objects with a Rectangle</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Face f <span style="color: #339933;">:</span> faceList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #666666; font-style: italic;">// Find faces[index] that is closest to face f</span>
       <span style="color: #666666; font-style: italic;">// set used[index] to true so that it can't be used twice</span>
       <span style="color: #000066; font-weight: bold;">float</span> record <span style="color: #339933;">=</span> <span style="color: #cc66cc;">50000</span><span style="color: #339933;">;</span>
       <span style="color: #000066; font-weight: bold;">int</span> index <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
       <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> faces.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000066; font-weight: bold;">float</span> d <span style="color: #339933;">=</span> dist<span style="color: #009900;">&#40;</span>faces<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">x</span>,faces<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">y</span>,f.<span style="color: #006633;">r</span>.<span style="color: #006633;">x</span>,f.<span style="color: #006633;">r</span>.<span style="color: #006633;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>d <span style="color: #339933;">&lt;</span> record <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>used<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
           record <span style="color: #339933;">=</span> d<span style="color: #339933;">;</span>
           index <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span> 
       <span style="color: #009900;">&#125;</span>
       <span style="color: #666666; font-style: italic;">// Update Face object location</span>
       used<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
       f.<span style="color: #006633;">update</span><span style="color: #009900;">&#40;</span>faces<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Notice how in the above code boolean variables are used to keep track of which Rectangles have already been matched.  We don't want two Face objects to think they are the same face!</p>
<p>3) Finally, the third scenario is that we have more Face objects than OpenCV has found.  In this case, we need to match our existing Face objects and then mark any leftover ones for deletion.</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// All Face objects start out as available</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Face f <span style="color: #339933;">:</span> faceList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      f.<span style="color: #006633;">available</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> 
    <span style="color: #666666; font-style: italic;">// Match Rectangle with a Face object</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> faces.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// Find face object closest to faces[i] Rectangle</span>
      <span style="color: #666666; font-style: italic;">// set available to false</span>
       <span style="color: #000066; font-weight: bold;">float</span> record <span style="color: #339933;">=</span> <span style="color: #cc66cc;">50000</span><span style="color: #339933;">;</span>
       <span style="color: #000066; font-weight: bold;">int</span> index <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
       <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> faceList.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         Face f <span style="color: #339933;">=</span> faceList.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>j<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #000066; font-weight: bold;">float</span> d <span style="color: #339933;">=</span> dist<span style="color: #009900;">&#40;</span>faces<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">x</span>,faces<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">y</span>,f.<span style="color: #006633;">r</span>.<span style="color: #006633;">x</span>,f.<span style="color: #006633;">r</span>.<span style="color: #006633;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>d <span style="color: #339933;">&lt;</span> record <span style="color: #339933;">&amp;&amp;</span> f.<span style="color: #006633;">available</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
           record <span style="color: #339933;">=</span> d<span style="color: #339933;">;</span>
           index <span style="color: #339933;">=</span> j<span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span> 
       <span style="color: #009900;">&#125;</span>
       <span style="color: #666666; font-style: italic;">// Update Face object location</span>
       Face f <span style="color: #339933;">=</span> faceList.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       f.<span style="color: #006633;">available</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
       f.<span style="color: #006633;">update</span><span style="color: #009900;">&#40;</span>faces<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Full source is here: <a href="http://www.shiffman.net/p5/whichface.zip">whichface.zip</a></p>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.shiffman.net/2011/04/26/opencv-matching-faces-over-time/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Rotate a Vector (Processing.js!)</title>
		<link>http://www.shiffman.net/2011/02/03/rotate-a-vector-processing-js/</link>
		<comments>http://www.shiffman.net/2011/02/03/rotate-a-vector-processing-js/#comments</comments>
		<pubDate>Thu, 03 Feb 2011 17:18:09 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.shiffman.net/?p=721</guid>
		<description><![CDATA[The question of how to rotate a PVector object (the data of the vector itself, I&#8217;m not talking about rotating while drawing) came up in my nature of code course yesterday. To do this, you&#8217;ll need to convert the vector &#8230; <a href="http://www.shiffman.net/2011/02/03/rotate-a-vector-processing-js/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The question of how to rotate a PVector object (the data of the vector itself, I&#8217;m not talking about rotating while drawing) came up in my <a href="http://www.shiffman.net/teaching/nature/">nature of code</a> course yesterday.   To do this, you&#8217;ll need to convert the vector to polar coordinates (radius + angle), adjust the angle, and the convert it back to cartesian to solve for the components (x and y).  A function would look something like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Rotate a vector in 2D</span>
<span style="color: #000066; font-weight: bold;">void</span> rotate2D<span style="color: #009900;">&#40;</span>PVector v, <span style="color: #000066; font-weight: bold;">float</span> theta<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// What's the magnitude?</span>
  <span style="color: #000066; font-weight: bold;">float</span> m <span style="color: #339933;">=</span> v.<span style="color: #006633;">mag</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// What's the angle?</span>
  <span style="color: #000066; font-weight: bold;">float</span> a <span style="color: #339933;">=</span> v.<span style="color: #006633;">heading2D</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Change the angle</span>
  a <span style="color: #339933;">+=</span> theta<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Polar to cartesian for the new xy components</span>
  v.<span style="color: #006633;">x</span> <span style="color: #339933;">=</span> m <span style="color: #339933;">*</span> cos<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  v.<span style="color: #006633;">y</span> <span style="color: #339933;">=</span> m <span style="color: #339933;">*</span> sin<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>(Update thanks to Vilhelm&#8217;s comment below: You can also use a 2D rotation matrix!)</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">void</span> rotate2D<span style="color: #009900;">&#40;</span>PVector v, <span style="color: #000066; font-weight: bold;">float</span> theta<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">float</span> xTemp <span style="color: #339933;">=</span> v.<span style="color: #006633;">x</span><span style="color: #339933;">;</span>
  v.<span style="color: #006633;">x</span> <span style="color: #339933;">=</span> v.<span style="color: #006633;">x</span><span style="color: #339933;">*</span>cos<span style="color: #009900;">&#40;</span>theta<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> v.<span style="color: #006633;">y</span><span style="color: #339933;">*</span>sin<span style="color: #009900;">&#40;</span>theta<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  v.<span style="color: #006633;">y</span> <span style="color: #339933;">=</span> xTemp<span style="color: #339933;">*</span>sin<span style="color: #009900;">&#40;</span>theta<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> v.<span style="color: #006633;">y</span><span style="color: #339933;">*</span>cos<span style="color: #009900;">&#40;</span>theta<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, this is really just a ruse.  A big excuse for me to figure out how to get a Processing example in a wordpress post using processing.js!  I was able to do this fairly quickly with three quick steps:</p>
<p>1) Download <a href="http://processingjs.org/download">processing.js 1.0</a>.<br />
2) Install <a href="http://wordpress.org/extend/plugins/processingjs/">wordpress processing.js plug-in</a>.<br />
3) Update the plug-in to use processing.js 1.0 (it hasn&#8217;t been yet).  <a href="http://wordpress.org/support/topic/plugin-processing-js-updating-to-processingjs-10">Follow these instructions</a>.</p>
<p>And here it is!</p>
<p><script type="application/processing">
// Rotate2D function
// Daniel Shiffman

void setup() {
  size(200,200);
  smooth();
}

void draw() {
  background(100);
  PVector xaxis = new PVector(75,0);
  float theta = map(mouseX,0,width,0,TWO_PI);
  rotate2D(xaxis,theta);
  drawVector(xaxis,width/2,height/2,1.0);
}

// Rotate a vector in 2D
void rotate2D(PVector v, float theta) {
  // What's the magnitude?
  float m = v.mag();
  // What's the angle?
  float a = v.heading2D();
  
  // Change the angle
  a += theta;
  
  // Now use polar to cartesian coordinates to calculate the new xy components
  v.x = m * cos(a);
  v.y = m * sin(a);
}

// Renders a vector object 'v' as an arrow and a location 'loc'
void drawVector(PVector v, float x, float y, float scayl) {
  pushMatrix();
  float arrowsize = 4;
  // Translate to location to render vector
  translate(x,y);
  stroke(255);
  // Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
  rotate(v.heading2D());
  // Calculate length of vector & scale it to be bigger or smaller if necessary
  float len = v.mag()*scayl;
  // Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
  line(0,0,len,0);
  line(len,0,len-arrowsize,+arrowsize/2);
  line(len,0,len-arrowsize,-arrowsize/2);
  popMatrix();
}
</script></p>
<p>And now the source:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Rotate2D function</span>
<span style="color: #666666; font-style: italic;">// Daniel Shiffman</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  size<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span>,<span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  smooth<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> draw<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  background<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  PVector xaxis <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PVector<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">75</span>,<span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">float</span> theta <span style="color: #339933;">=</span> map<span style="color: #009900;">&#40;</span>mouseX,<span style="color: #cc66cc;">0</span>,width,<span style="color: #cc66cc;">0</span>,TWO_PI<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  rotate2D<span style="color: #009900;">&#40;</span>xaxis,theta<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  drawVector<span style="color: #009900;">&#40;</span>xaxis,width<span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span>,height<span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">1.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Rotate a vector in 2D</span>
<span style="color: #000066; font-weight: bold;">void</span> rotate2D<span style="color: #009900;">&#40;</span>PVector v, <span style="color: #000066; font-weight: bold;">float</span> theta<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// What's the magnitude?</span>
  <span style="color: #000066; font-weight: bold;">float</span> m <span style="color: #339933;">=</span> v.<span style="color: #006633;">mag</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// What's the angle?</span>
  <span style="color: #000066; font-weight: bold;">float</span> a <span style="color: #339933;">=</span> v.<span style="color: #006633;">heading2D</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Change the angle</span>
  a <span style="color: #339933;">+=</span> theta<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Polar to cartesian for the new xy components</span>
  v.<span style="color: #006633;">x</span> <span style="color: #339933;">=</span> m <span style="color: #339933;">*</span> cos<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  v.<span style="color: #006633;">y</span> <span style="color: #339933;">=</span> m <span style="color: #339933;">*</span> sin<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Renders a vector object 'v' as an arrow at a location xy</span>
<span style="color: #000066; font-weight: bold;">void</span> drawVector<span style="color: #009900;">&#40;</span>PVector v, <span style="color: #000066; font-weight: bold;">float</span> x, <span style="color: #000066; font-weight: bold;">float</span> y, <span style="color: #000066; font-weight: bold;">float</span> scayl<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  pushMatrix<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">float</span> arrowsize <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// Translate to location to render vector</span>
  translate<span style="color: #009900;">&#40;</span>x,y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  stroke<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// Vector heading to get direction (pointing up is a heading of 0)</span>
  rotate<span style="color: #009900;">&#40;</span>v.<span style="color: #006633;">heading2D</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// Scale it to be bigger or smaller if necessary</span>
  <span style="color: #000066; font-weight: bold;">float</span> len <span style="color: #339933;">=</span> v.<span style="color: #006633;">mag</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>scayl<span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// Draw three lines to make an arrow</span>
  line<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,len,<span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  line<span style="color: #009900;">&#40;</span>len,<span style="color: #cc66cc;">0</span>,len<span style="color: #339933;">-</span>arrowsize,<span style="color: #339933;">+</span>arrowsize<span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  line<span style="color: #009900;">&#40;</span>len,<span style="color: #cc66cc;">0</span>,len<span style="color: #339933;">-</span>arrowsize,<span style="color: #339933;">-</span>arrowsize<span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  popMatrix<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.shiffman.net/2011/02/03/rotate-a-vector-processing-js/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Nature of Code book on Kickstarter</title>
		<link>http://www.shiffman.net/2011/01/21/nature-of-code-book-on-kickstarter/</link>
		<comments>http://www.shiffman.net/2011/01/21/nature-of-code-book-on-kickstarter/#comments</comments>
		<pubDate>Fri, 21 Jan 2011 20:38:45 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[kickstarter]]></category>
		<category><![CDATA[processing.org]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[self-publishing]]></category>

		<guid isPermaLink="false">http://www.shiffman.net/?p=694</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p><iframe frameborder="0" height="380px" src="https://www.kickstarter.com/projects/shiffman/the-nature-of-code-book-project/widget/card.html" width="220px"></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://www.shiffman.net/2011/01/21/nature-of-code-book-on-kickstarter/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>New Kinect Example Average Point Tracking</title>
		<link>http://www.shiffman.net/2011/01/13/new-kinect-example-average-point-tracking/</link>
		<comments>http://www.shiffman.net/2011/01/13/new-kinect-example-average-point-tracking/#comments</comments>
		<pubDate>Thu, 13 Jan 2011 16:42:25 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[kinect]]></category>
		<category><![CDATA[processing.org]]></category>
		<category><![CDATA[tracking]]></category>

		<guid isPermaLink="false">http://www.shiffman.net/?p=654</guid>
		<description><![CDATA[This example demonstrates how to track the average location of a given number of points that meet a specific depth threshold. It&#8217;s the most basic form of hand tracking (though of course it knows nothing about the object being a &#8230; <a href="http://www.shiffman.net/2011/01/13/new-kinect-example-average-point-tracking/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><iframe src="http://player.vimeo.com/video/18750684?title=0&amp;byline=0&amp;portrait=0&amp;color=ff9933" width="435" height="313" frameborder="0"></iframe></p>
<p>This example demonstrates how to track the average location of a given number of points that meet a specific depth threshold. It&#8217;s the most basic form of hand tracking (though of course it knows nothing about the object being a hand.) </p>
<p><a href="http://github.com/shiffman/libfreenect/tree/master/wrappers/java/processing/distribution/openkinect/examples/">Source code</a></p>
<p>Download: <a href="http://github.com/shiffman/libfreenect/raw/master/wrappers/java/processing/distribution/openkinect.zip">openkinect.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.shiffman.net/2011/01/13/new-kinect-example-average-point-tracking/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

