<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Exercise 16-5: Color Tracking with Snake</title>
	<atom:link href="http://www.learningprocessing.com/exercises/chapter-16/exercise-16-5/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learningprocessing.com</link>
	<description>A Beginner's Guide to Programming Images, Animation, and Interaction by Daniel Shiffman</description>
	<lastBuildDate>Fri, 10 Feb 2012 19:58:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Anonymous</title>
		<link>http://www.learningprocessing.com/exercises/chapter-16/exercise-16-5/comment-page-1/#comment-20661</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Mon, 21 Nov 2011 02:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningprocessing.com/?page_id=1294#comment-20661</guid>
		<description>you can reverse the snake&#039;s location by saying

    snake.update(width-1-closestX,closestY);

</description>
		<content:encoded><![CDATA[<p>you can reverse the snake&#8217;s location by saying</p>
<p>    snake.update(width-1-closestX,closestY);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Derder131</title>
		<link>http://www.learningprocessing.com/exercises/chapter-16/exercise-16-5/comment-page-1/#comment-20660</link>
		<dc:creator>Derder131</dc:creator>
		<pubDate>Mon, 21 Nov 2011 00:19:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningprocessing.com/?page_id=1294#comment-20660</guid>
		<description>Okay thanks but now all the snake is all wrong :(</description>
		<content:encoded><![CDATA[<p>Okay thanks but now all the snake is all wrong <img src='http://www.learningprocessing.com/wp/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://www.learningprocessing.com/exercises/chapter-16/exercise-16-5/comment-page-1/#comment-20647</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Mon, 07 Nov 2011 03:20:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningprocessing.com/?page_id=1294#comment-20647</guid>
		<description>pushMatrix();
scale(-1,1);
image(video,0,-video.width);
popMatrix();</description>
		<content:encoded><![CDATA[<p>pushMatrix();<br />
scale(-1,1);<br />
image(video,0,-video.width);<br />
popMatrix();</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Derder131</title>
		<link>http://www.learningprocessing.com/exercises/chapter-16/exercise-16-5/comment-page-1/#comment-20644</link>
		<dc:creator>Derder131</dc:creator>
		<pubDate>Sat, 05 Nov 2011 23:47:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningprocessing.com/?page_id=1294#comment-20644</guid>
		<description>This is really great, but how do i mirror the camera image?</description>
		<content:encoded><![CDATA[<p>This is really great, but how do i mirror the camera image?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Shiffman</title>
		<link>http://www.learningprocessing.com/exercises/chapter-16/exercise-16-5/comment-page-1/#comment-20494</link>
		<dc:creator>Daniel Shiffman</dc:creator>
		<pubDate>Mon, 18 Apr 2011 14:20:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningprocessing.com/?page_id=1294#comment-20494</guid>
		<description>If you want them to always be a new random every frame you can pick the random color right when you draw the ellipse:

color c = color(random(255),random(255),random(255));
fill(c);
ellipse(xpos[i],ypos[i],i,i); 

You might also consider making an array of random colors when the Snake object is made.</description>
		<content:encoded><![CDATA[<p>If you want them to always be a new random every frame you can pick the random color right when you draw the ellipse:</p>
<p>color c = color(random(255),random(255),random(255));<br />
fill(c);<br />
ellipse(xpos[i],ypos[i],i,i); </p>
<p>You might also consider making an array of random colors when the Snake object is made.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stephen White</title>
		<link>http://www.learningprocessing.com/exercises/chapter-16/exercise-16-5/comment-page-1/#comment-20491</link>
		<dc:creator>Stephen White</dc:creator>
		<pubDate>Sun, 17 Apr 2011 16:12:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningprocessing.com/?page_id=1294#comment-20491</guid>
		<description>Thanks for the reply, where abouts when i implement that code to get the ellipses to change color?</description>
		<content:encoded><![CDATA[<p>Thanks for the reply, where abouts when i implement that code to get the ellipses to change color?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Shiffman</title>
		<link>http://www.learningprocessing.com/exercises/chapter-16/exercise-16-5/comment-page-1/#comment-20490</link>
		<dc:creator>Daniel Shiffman</dc:creator>
		<pubDate>Sun, 17 Apr 2011 14:16:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningprocessing.com/?page_id=1294#comment-20490</guid>
		<description>The colors of the camera is the code that is on this very page!  Just click &quot;reveal answer&quot;.  To make a random color in Processing you can say:

color c = color(random(255),random(255),random(255));</description>
		<content:encoded><![CDATA[<p>The colors of the camera is the code that is on this very page!  Just click &#8220;reveal answer&#8221;.  To make a random color in Processing you can say:</p>
<p>color c = color(random(255),random(255),random(255));</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stephen White</title>
		<link>http://www.learningprocessing.com/exercises/chapter-16/exercise-16-5/comment-page-1/#comment-20488</link>
		<dc:creator>Stephen White</dc:creator>
		<pubDate>Thu, 14 Apr 2011 22:44:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningprocessing.com/?page_id=1294#comment-20488</guid>
		<description>Hi.
I downloaded both the snake code and the camera code,

I&#039;m wondering how i would go around changing the colour of the snake, to either randomly change in say RGB colours, or colours of what the camera is picking up. 

Cheers</description>
		<content:encoded><![CDATA[<p>Hi.<br />
I downloaded both the snake code and the camera code,</p>
<p>I&#8217;m wondering how i would go around changing the colour of the snake, to either randomly change in say RGB colours, or colours of what the camera is picking up. </p>
<p>Cheers</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cole</title>
		<link>http://www.learningprocessing.com/exercises/chapter-16/exercise-16-5/comment-page-1/#comment-20485</link>
		<dc:creator>Cole</dc:creator>
		<pubDate>Tue, 12 Apr 2011 07:19:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.learningprocessing.com/?page_id=1294#comment-20485</guid>
		<description>hi.
I was able to connect the provided Color Tracker/Snake example, but now I am trying to connect a Boid sketch. (code below) I believe my issues are with class naming, duplicate voids in each sketch. I was going to post an attempt that was giving me problems but felt like it would be more confusing than helpful. Best to see both in their original code.
Thanks!

--

// Boid Sketch

final int WINDOW_WIDTH  = 1000;
final int WINDOW_HEIGHT =  800;
final int FRAME_RATE = 60;

final int NUM_BOIDS = 40;
final int CENTER_PULL_FACTOR = 80000;
final float BOUNCE_ABSORPTION = 0.75;

final color backColor = color(0, 0, 0);

class Vector
{
  float x, y;
  
  public Vector()
  {
    x = 0;
    y = 0;
  }
  
  public Vector(float initX, float initY)
  {
    x = initX;
    y = initY;
  }
} // end class Vector

class Boid
{
  final int TRAIL_SCALE = 35;
  
  color myColor = color(255, 255, 255);
  int mySize = 2;
  
  float xpos, ypos;
  float vx, vy;
  
  void drawMe()
  {
    stroke(myColor);
    strokeWeight(mySize+3*sqrt(vx*vx+vy*vy));
    
    line(xpos, ypos, xpos-TRAIL_SCALE*vx, ypos-TRAIL_SCALE*vy); 
  }
  
} // end class Boid


// ************************* GLOBAL VARIABLES ************************** 


Boid[] flock = new Boid[NUM_BOIDS];
Vector v1 = new Vector();
Vector v2 = new Vector();
Vector v3 = new Vector();
Vector v4 = new Vector();


void setup() 
{
  size(WINDOW_WIDTH, WINDOW_HEIGHT); 
  background(backColor);
  
  randomSeed(int(random(1,1000)));
  
  for(int i=0; i&lt;NUM_BOIDS; ++i)
  {
    flock[i] = new Boid();
    flock[i].xpos = random(0, WINDOW_WIDTH);
    flock[i].ypos = random(0, WINDOW_HEIGHT);
    
    flock[i].vx = random(-0.5, 0.5);
    flock[i].vy = random(-0.5, 0.5);

  } // end for
  
  frameRate(FRAME_RATE);
  smooth();
}


void draw() 
{   
  background(backColor);
  
  for (int i=0; i&lt;NUM_BOIDS; ++i)
  {
    flock[i].drawMe();
    updateFlock();
  } // end for
}

void updateFlock()
{
  for (int i=0; i&lt;NUM_BOIDS; ++i)
  {
    v1 = rule1(flock[i]);
    v2 = rule2(flock[i]);
    v3 = rule3(flock[i]);
    v4 = rule4(flock[i]);
    
    // add vectors to velocities
    flock[i].vx += v1.x + v2.x + v3.x + v4.x;
    flock[i].vy += v1.y + v2.y + v3.y + v4.y;
    
    limitVelocity(flock[i]);
    
    // update new position with previously calculated velocities
    flock[i].xpos += flock[i].vx;
    flock[i].ypos += flock[i].vy;
    
    if (flock[i].xpos  WINDOW_WIDTH)
	flock[i].vx = -flock[i].vx*BOUNCE_ABSORPTION;
	
    if (flock[i].ypos  WINDOW_HEIGHT)
	flock[i].vy = -flock[i].vy*BOUNCE_ABSORPTION;

  } // end for
}

void limitVelocity(Boid b)
{
  float vlim = 1.5;
  Vector v = new Vector();
  
  float velocity = sqrt(sq(b.vx) + sq(b.vy));
  
  if (velocity &gt; vlim)
  {
    float vx = (b.vx/velocity)/vlim;
    float vy = (b.vy/velocity)/vlim;
    b.vx = vx;
    b.vy = vy;
  }  
  
} // end limitVelocity()


Vector rule1(Boid b)
{
  Vector pc = new Vector();
  
  for (int i=0; i &lt; NUM_BOIDS; ++i)
  {
    if (b != flock[i])
    {
	pc.x += flock[i].xpos;
	pc.y += flock[i].ypos;
    } // end if
  } // end for
	
  pc.x /= (NUM_BOIDS-1);
  pc.y /= (NUM_BOIDS-1);
  
  pc.x = (pc.x - b.xpos) / CENTER_PULL_FACTOR;
  pc.y = (pc.y - b.ypos) / CENTER_PULL_FACTOR;
   
  return pc;
} // end rule1()

Vector rule2(Boid b)
{
  Vector v = new Vector();
  
  for (int i=0; i &lt; NUM_BOIDS; ++i)
  {
    if (b != flock[i])
    {
	if (distance2d(b, flock[i]) &lt; 20)
	{
	  v.x -= flock[i].xpos - b.xpos;
	  v.y -= flock[i].ypos - b.ypos;
	} // end if
    } // end if
  } // end for
  
  return v;
} // end rule2()

Vector rule3(Boid b)
{
  Vector v  = new Vector();
  
  for (int i=0; i &lt; NUM_BOIDS; ++i)
  {
    if (b != flock[i])
    {
	v.x += b.vx;
	v.y += b.vy;
    } // end if
  } // end for
  
  v.x /= (NUM_BOIDS - 1);
  v.y /= (NUM_BOIDS - 1);
  
  v.x = (v.x - b.vx)/8;
  v.y = (v.y - b.vy)/8;
  
  return v;
} // end rule3()


Vector rule4(Boid b)
{
  Vector t = new Vector(0,0);
  //return t;
  
  Vector v = new Vector();
  
  v.x = (mouseX - b.xpos) / CENTER_PULL_FACTOR;
  v.y = (mouseY - b.ypos) / CENTER_PULL_FACTOR;
  
  return v;
  
} // end rule4()


float distance2d(Boid v1, Boid v2)
{
  float x = abs(v1.xpos - v2.xpos);
  float y = abs(v1.ypos - v2.ypos);
  
  return((sqrt(x*x + y*y)));
}</description>
		<content:encoded><![CDATA[<p>hi.<br />
I was able to connect the provided Color Tracker/Snake example, but now I am trying to connect a Boid sketch. (code below) I believe my issues are with class naming, duplicate voids in each sketch. I was going to post an attempt that was giving me problems but felt like it would be more confusing than helpful. Best to see both in their original code.<br />
Thanks!</p>
<p>&#8211;</p>
<p>// Boid Sketch</p>
<p>final int WINDOW_WIDTH  = 1000;<br />
final int WINDOW_HEIGHT =  800;<br />
final int FRAME_RATE = 60;</p>
<p>final int NUM_BOIDS = 40;<br />
final int CENTER_PULL_FACTOR = 80000;<br />
final float BOUNCE_ABSORPTION = 0.75;</p>
<p>final color backColor = color(0, 0, 0);</p>
<p>class Vector<br />
{<br />
  float x, y;</p>
<p>  public Vector()<br />
  {<br />
    x = 0;<br />
    y = 0;<br />
  }</p>
<p>  public Vector(float initX, float initY)<br />
  {<br />
    x = initX;<br />
    y = initY;<br />
  }<br />
} // end class Vector</p>
<p>class Boid<br />
{<br />
  final int TRAIL_SCALE = 35;</p>
<p>  color myColor = color(255, 255, 255);<br />
  int mySize = 2;</p>
<p>  float xpos, ypos;<br />
  float vx, vy;</p>
<p>  void drawMe()<br />
  {<br />
    stroke(myColor);<br />
    strokeWeight(mySize+3*sqrt(vx*vx+vy*vy));</p>
<p>    line(xpos, ypos, xpos-TRAIL_SCALE*vx, ypos-TRAIL_SCALE*vy);<br />
  }</p>
<p>} // end class Boid</p>
<p>// ************************* GLOBAL VARIABLES ************************** </p>
<p>Boid[] flock = new Boid[NUM_BOIDS];<br />
Vector v1 = new Vector();<br />
Vector v2 = new Vector();<br />
Vector v3 = new Vector();<br />
Vector v4 = new Vector();</p>
<p>void setup()<br />
{<br />
  size(WINDOW_WIDTH, WINDOW_HEIGHT);<br />
  background(backColor);</p>
<p>  randomSeed(int(random(1,1000)));</p>
<p>  for(int i=0; i&lt;NUM_BOIDS; ++i)<br />
  {<br />
    flock[i] = new Boid();<br />
    flock[i].xpos = random(0, WINDOW_WIDTH);<br />
    flock[i].ypos = random(0, WINDOW_HEIGHT);</p>
<p>    flock[i].vx = random(-0.5, 0.5);<br />
    flock[i].vy = random(-0.5, 0.5);</p>
<p>  } // end for</p>
<p>  frameRate(FRAME_RATE);<br />
  smooth();<br />
}</p>
<p>void draw()<br />
{<br />
  background(backColor);</p>
<p>  for (int i=0; i&lt;NUM_BOIDS; ++i)<br />
  {<br />
    flock[i].drawMe();<br />
    updateFlock();<br />
  } // end for<br />
}</p>
<p>void updateFlock()<br />
{<br />
  for (int i=0; i&lt;NUM_BOIDS; ++i)<br />
  {<br />
    v1 = rule1(flock[i]);<br />
    v2 = rule2(flock[i]);<br />
    v3 = rule3(flock[i]);<br />
    v4 = rule4(flock[i]);</p>
<p>    // add vectors to velocities<br />
    flock[i].vx += v1.x + v2.x + v3.x + v4.x;<br />
    flock[i].vy += v1.y + v2.y + v3.y + v4.y;</p>
<p>    limitVelocity(flock[i]);</p>
<p>    // update new position with previously calculated velocities<br />
    flock[i].xpos += flock[i].vx;<br />
    flock[i].ypos += flock[i].vy;</p>
<p>    if (flock[i].xpos  WINDOW_WIDTH)<br />
	flock[i].vx = -flock[i].vx*BOUNCE_ABSORPTION;</p>
<p>    if (flock[i].ypos  WINDOW_HEIGHT)<br />
	flock[i].vy = -flock[i].vy*BOUNCE_ABSORPTION;</p>
<p>  } // end for<br />
}</p>
<p>void limitVelocity(Boid b)<br />
{<br />
  float vlim = 1.5;<br />
  Vector v = new Vector();</p>
<p>  float velocity = sqrt(sq(b.vx) + sq(b.vy));</p>
<p>  if (velocity &gt; vlim)<br />
  {<br />
    float vx = (b.vx/velocity)/vlim;<br />
    float vy = (b.vy/velocity)/vlim;<br />
    b.vx = vx;<br />
    b.vy = vy;<br />
  }  </p>
<p>} // end limitVelocity()</p>
<p>Vector rule1(Boid b)<br />
{<br />
  Vector pc = new Vector();</p>
<p>  for (int i=0; i &lt; NUM_BOIDS; ++i)<br />
  {<br />
    if (b != flock[i])<br />
    {<br />
	pc.x += flock[i].xpos;<br />
	pc.y += flock[i].ypos;<br />
    } // end if<br />
  } // end for</p>
<p>  pc.x /= (NUM_BOIDS-1);<br />
  pc.y /= (NUM_BOIDS-1);</p>
<p>  pc.x = (pc.x &#8211; b.xpos) / CENTER_PULL_FACTOR;<br />
  pc.y = (pc.y &#8211; b.ypos) / CENTER_PULL_FACTOR;</p>
<p>  return pc;<br />
} // end rule1()</p>
<p>Vector rule2(Boid b)<br />
{<br />
  Vector v = new Vector();</p>
<p>  for (int i=0; i &lt; NUM_BOIDS; ++i)<br />
  {<br />
    if (b != flock[i])<br />
    {<br />
	if (distance2d(b, flock[i]) &lt; 20)<br />
	{<br />
	  v.x -= flock[i].xpos &#8211; b.xpos;<br />
	  v.y -= flock[i].ypos &#8211; b.ypos;<br />
	} // end if<br />
    } // end if<br />
  } // end for</p>
<p>  return v;<br />
} // end rule2()</p>
<p>Vector rule3(Boid b)<br />
{<br />
  Vector v  = new Vector();</p>
<p>  for (int i=0; i &lt; NUM_BOIDS; ++i)<br />
  {<br />
    if (b != flock[i])<br />
    {<br />
	v.x += b.vx;<br />
	v.y += b.vy;<br />
    } // end if<br />
  } // end for</p>
<p>  v.x /= (NUM_BOIDS &#8211; 1);<br />
  v.y /= (NUM_BOIDS &#8211; 1);</p>
<p>  v.x = (v.x &#8211; b.vx)/8;<br />
  v.y = (v.y &#8211; b.vy)/8;</p>
<p>  return v;<br />
} // end rule3()</p>
<p>Vector rule4(Boid b)<br />
{<br />
  Vector t = new Vector(0,0);<br />
  //return t;</p>
<p>  Vector v = new Vector();</p>
<p>  v.x = (mouseX &#8211; b.xpos) / CENTER_PULL_FACTOR;<br />
  v.y = (mouseY &#8211; b.ypos) / CENTER_PULL_FACTOR;</p>
<p>  return v;</p>
<p>} // end rule4()</p>
<p>float distance2d(Boid v1, Boid v2)<br />
{<br />
  float x = abs(v1.xpos &#8211; v2.xpos);<br />
  float y = abs(v1.ypos &#8211; v2.ypos);</p>
<p>  return((sqrt(x*x + y*y)));<br />
}</p>
]]></content:encoded>
	</item>
</channel>
</rss>

