<?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">
	<channel>
      <title>Tagged with atan2() - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=atan2%28%29</link>
      <pubDate>Sun, 08 Aug 2021 19:59:41 +0000</pubDate>
         <description>Tagged with atan2() - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/taggedatan2%28%29/feed.rss" rel="self" type="application/rss+xml" />
   <item>
      <title>get the vertex to scale properly in rotating shape</title>
      <link>https://forum.processing.org/two/discussion/27779/get-the-vertex-to-scale-properly-in-rotating-shape</link>
      <pubDate>Mon, 16 Apr 2018 10:15:53 +0000</pubDate>
      <dc:creator>Per</dc:creator>
      <guid isPermaLink="false">27779@/two/discussions</guid>
      <description><![CDATA[<p>I got this rectangle shape drawn by vertex. Im creating the rectangle simply by offsetting one of the sides with the width of the rectangle. To get the edges of the rectangle to always be in 90 degrees according to the sides Im first calculating the theta and are then using the theta to calculate how much the side shall shrink or grow.</p>

<p>Im getting side of the rectangle correct in and are always 90 degrees but the scaling of the width is not correct and cant really figure out how to solve that.</p>

<p>Any hint?</p>

<pre><code>void setup() {
  size(500, 500, P2D); 
  background(255);
  //noCursor();
}

void draw() {
  background(255);
  gradientRectangle(); 
}

void gradientRectangle() {
  int rectWidth = 25; 
  float x1 = mouseX;
  float y1 = mouseY;
  float x2 = 250;
  float y2 = 250;
  color from = color(255, 0, 0);
  color to = color(0);

  // calculate the theta
  float adjecent = x2-x1;
  float opposite = y2-y1;

  float tanValue = opposite / adjecent;
  float theta = atan(tanValue); // in radians
  float angle = degrees(theta); // in degrees
  println("theta: "+ theta + " tanValue " + tanValue + " angle: " + angle);

  // calculate the change of the side of the second triangle
  float opposite2 = tan(theta) * rectWidth;
  println("opposite2: " + opposite2);

  // draw the shape
  beginShape();
  noStroke();  
  strokeWeight(5);

  fill(from);   
  vertex(x2, y2);  // övre till höger
  fill(to);
  vertex(x1, y1); // övre vänstra
  fill(to);
  vertex(x1+opposite2, y1-rectWidth); // undre vänstra
  fill(from);
  vertex(x2+opposite2, y2-rectWidth); // undre till höger
  endShape();
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Intersection between objects in a array</title>
      <link>https://forum.processing.org/two/discussion/27713/intersection-between-objects-in-a-array</link>
      <pubDate>Sat, 07 Apr 2018 10:15:10 +0000</pubDate>
      <dc:creator>FelixIsFun</dc:creator>
      <guid isPermaLink="false">27713@/two/discussions</guid>
      <description><![CDATA[<p>So i'm now working on a little game to learn how intersection and atan2 works. And I need some help with the intersection between objects in a array. Why I need your help? Because I don't want the objects overlap eachother. So can you people please help me?</p>

<p>Heres the draw and setup</p>

<pre><code>Player p;
int x = 100;
int y = 100;
int grids = 100;
int enemyCount = 1;
int bulletCount = 1;

float dEnemy;

boolean wait;
boolean isLeft, isRight, isUp, isDown;

ArrayList shootBullet = new ArrayList();
ArrayList enemies = new ArrayList();

void setup() {
  //fullScreen(P3D);
  size(750, 500, P3D);
  p = new Player();

  for(int i = 0; i &lt; 2; i++) {
    enemies.add(new enemy());
  }
}

void draw() {
  background(100);
  stroke(255);

  for(int i = 0; i &lt; 41; i++) {
    line(0 + (i * 100), 0, 0 + (i * 100), 4000);
  }

  for(int i = 0; i &lt; 41; i++) {
    line(0, 0 + (i * 100), 4000, 0 + (i * 100));
  }


  if(mousePressed &amp;&amp; wait == false) {
    for(int i = 0; i &lt; bulletCount; i++) {
      wait = true;
      shootBullet.add(new shoot());
    }
  } else {
    if(mousePressed == false) {
      wait = false;
    }
  }

  for(int i = 0; i &lt; shootBullet.size(); i++) {    
    shoot s = (shoot) shootBullet.get(i);
    s.display();
  }

  for(int i = 0; i &lt; enemies.size(); i++) {
    enemy e = (enemy) enemies.get(i);
    e.display();
  }

  p.display();
  p.movement();
}

void keyPressed() {
  setMove(keyCode, true);
}

void keyReleased() {
  setMove(keyCode, false);
}

boolean setMove(int k, boolean b) {
  switch (k) {
    case UP:
    return isUp = b;

    case DOWN:
    return isDown = b;

    case LEFT:
    return isLeft = b;

    case RIGHT:
    return isRight = b;

    default:
    return b;
  }
}
</code></pre>

<p>Heres my Enemy class (The one that I want to check intersection between the enemies so they stop moving when they hit Eachother)</p>

<pre><code>class enemy {
  PVector location;
  PVector velocity;
  float a;
  float speed = random(1, 10);
  float r = 50;
  float dPlayer;

  enemy() {
    location = new PVector(random(0, 4000), random(0, 4000));
    velocity = new PVector();
  }

  void display() {
    a = atan2(p.location.y - location.y, p.location.x - location.x);
    dPlayer = dist(location.x, location.y, p.location.x, p.location.y);

    velocity.x = cos(a);
    velocity.y = sin(a);

    velocity.x *= speed;
    velocity.y *= speed;

    if(dPlayer &gt;= p.r) {
      location.x = location.x + velocity.x;
      location.y = location.y + velocity.y;
    }

    ellipse(location.x, location.y, r, r);
  }
}
</code></pre>

<p>Heres my Player class</p>

<pre><code>class Player {
  PVector location;
  PVector velocity;
  float frict = 0.96;
  int r = 50;

  Player() {
    location = new PVector(2000, 2000);
    velocity = new PVector(0, 0);
  }

  void display() {
    //pushMatrix();
    colorMode(RGB);
    fill(255);
    camera(location.x, location.y, (height/2.0) / tan(PI*30.0 / 180.0), location.x, location.y, 0, 0, 1, 0); 
    ellipse(location.x, location.y, r, 50r);
    //popMatrix();
  }

  void movement() {
    location.x = location.x + velocity.x; 
    location.y = location.y + velocity.y;
    if (isLeft) velocity.x = velocity.x - frict/2;
    if (isRight) velocity.x = velocity.x + frict/2;
    if (isDown) velocity.y = velocity.y + frict/2;
    if (isUp) velocity.y  = velocity.y - frict/2;
    velocity.y = velocity.y * frict;
    velocity.x = velocity.x * frict;
    velocity.x = constrain(velocity.x, -5, 5);
    velocity.y = constrain(velocity.y, -5, 5);
  }
}
</code></pre>

<p>Heres my Shoot class</p>

<pre><code>class shoot {
  PVector velocity;
  PVector location;

  shoot() {
    velocity = new PVector();
    location = new PVector();
    location.x = p.location.x;
    location.y = p.location.y;
    float a = atan2(mouseY - height/2, mouseX - width/2);
    velocity.x = cos(a);
    velocity.y = sin(a);
    velocity.x *= 10;
    velocity.y *= 10;
  }

  void display() {
    println(mouseY - height/2);
    println(mouseX - height/2);
    location.x += velocity.x;
    location.y += velocity.y;
    ellipse(location.x, location.y, 10, 10);
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>How to define an origin for atan2()</title>
      <link>https://forum.processing.org/two/discussion/27380/how-to-define-an-origin-for-atan2</link>
      <pubDate>Mon, 26 Mar 2018 18:29:09 +0000</pubDate>
      <dc:creator>Battlesquid</dc:creator>
      <guid isPermaLink="false">27380@/two/discussions</guid>
      <description><![CDATA[<p>The p5js page for atan2 states that it works in the following way:</p>

<blockquote class="Quote">
  <p>Calculates the angle (in radians) from a specified point to the coordinate origin as measured from the positive x-axis.</p>
</blockquote>

<p>However, I would like to calculate the angle between a set of points other than the origin. Is there any way to do this, and if so, can I get a pointer towards the right track? Thanks in advance! :)</p>
]]></description>
   </item>
   <item>
      <title>Movement with keyboard and mouse</title>
      <link>https://forum.processing.org/two/discussion/26951/movement-with-keyboard-and-mouse</link>
      <pubDate>Wed, 21 Mar 2018 03:02:00 +0000</pubDate>
      <dc:creator>hotdogwe</dc:creator>
      <guid isPermaLink="false">26951@/two/discussions</guid>
      <description><![CDATA[<p>For a project, we get to make a game with three levels. I am trying to do a Hotline Miami kind of game. I am having trouble with player rotation. The info online helped but when trying to copy it into my code it just increases the circumference of the rotation each time.</p>

<p>Here is my code</p>

<pre><code>PImage biker;

int x = 100;
int y = 100;

void setup()
{
  size(1500,1000);
}


void draw()
{
  background(0);
  biker = loadImage("biker.png");

  bikerMovement();
  bikerThrowKnife();
}


void bikerMovement()
{

  rotate(atan2(mouseY-y,mouseX-x));
  image(biker, x, y);



}

void bikerThrowKnife()
{





}


void keyPressed()
{

  if(key == 'w')
  {

    y-=7;

  }
  else if(key == 'w' &amp;&amp; key == 'a')
  {

    x-=7;
    y-=7;
  }



  if(key == 's')
  {

    y+=7;

  }
  if(key == 'a')
  {

    x-=7;

  }
  if(key == 'd')
  {

    x+=7;

  }

}
</code></pre>
]]></description>
   </item>
   <item>
      <title>image rotation following the mouse</title>
      <link>https://forum.processing.org/two/discussion/24353/image-rotation-following-the-mouse</link>
      <pubDate>Tue, 03 Oct 2017 04:53:50 +0000</pubDate>
      <dc:creator>datandre</dc:creator>
      <guid isPermaLink="false">24353@/two/discussions</guid>
      <description><![CDATA[<p>hi,
i’ve searched online but i couldn’t find anything good for me.</p>

<p>There’s a PImage which has to follow the coordinates of the mouse, without moving.
<img src="http://mathworld.wolfram.com/images/gifs/rotat.gif" alt="98174C5B-88C9-4648-91E9-78EAE4E80C21" title="98174C5B-88C9-4648-91E9-78EAE4E80C21" />
This gif is pretty much what i’d like to do</p>
]]></description>
   </item>
   <item>
      <title>Following a mouse around a circle</title>
      <link>https://forum.processing.org/two/discussion/21951/following-a-mouse-around-a-circle</link>
      <pubDate>Wed, 12 Apr 2017 12:55:37 +0000</pubDate>
      <dc:creator>schotsl</dc:creator>
      <guid isPermaLink="false">21951@/two/discussions</guid>
      <description><![CDATA[<p>Hello, I wanted to have a triangle move around a circle to follow the mouse but it had to have a delay so it takes time for the triangle to reach the mouse I got that working but when you move the mouse over to te left top side while the triangle is left down side the triangle moves around the circle trough the right side which takes way longer, although I do understand why this happens I have no Idea how to solve this</p>

<p>Could anyone help me out? also, I've attached the code below</p>

<pre><code>int x, y;
float current_ship;
float target_ship;

void setup() { 
  fullScreen(); 
  ellipseMode(CENTER); 
} 


void draw() { 
  background(255); 
  ellipse(width/2, height/2, 200, 200); 

  target_ship = atan2(mouseY - height/2, mouseX - width/2);

  if(target_ship &gt; current_ship) current_ship += 0.05;
  if(target_ship &lt; current_ship) current_ship -= 0.05;

  x = int(width/2+165*cos(current_ship)); 
  y = int(height/2+165*sin(current_ship)); 

  pushMatrix(); 
  translate(x, y); 
  rotate(atan2( mouseY - height/2, mouseX - width/2)-PI/2); 
  triangle(-30, -10, 0, +50, +30, -10); 
  popMatrix(); 
} 
</code></pre>
]]></description>
   </item>
   <item>
      <title>Midpoint Circle Algorithm, Modifying it to use arcs</title>
      <link>https://forum.processing.org/two/discussion/22375/midpoint-circle-algorithm-modifying-it-to-use-arcs</link>
      <pubDate>Wed, 03 May 2017 23:11:58 +0000</pubDate>
      <dc:creator>EvanJbesser</dc:creator>
      <guid isPermaLink="false">22375@/two/discussions</guid>
      <description><![CDATA[<p>Hello, I have been having quite an annoying problem figuring out how to convert Processing's use of rotations into standard 0-360 degree counter-clockwise unit circle. \
I have JUST about figured it out however once the rotation goes from -180 (standard 180 degrees) abruptly to 180 degrees (standard 181 degrees) the &gt; algorithm doesn't fulfill the condition and creates a dot at 180 degrees, however it is supposed to create an arc that extends 90 degrees. Every other octant performs perfectly except for the 135-180 degree octant. I'm using the code of "Midpoint Circle Algorithm" on the Wikipedia page.
code as follows.</p>

<p>the added code for arcs is below</p>

<p>`// Calculate the angle the current point makes with the circle center
  int angle = (int) degrees(atan2(y, x)); //int(degrees(atan2(mouseY-(height/2),mouseX-(width/2))));</p>

<p>// draw the circle points as long as they lie in the range specified
  if (x &lt; y) {</p>

<pre><code>// draw point in range 0 to 45 degrees
if ( -90 + angle &gt;= startAngle &amp;&amp;  -90 + angle &lt;= endAngle) {
  point(centerX + y, centerY - x);
}

//// draw point in range 45 to 90 degrees
if (-angle &gt;= startAngle &amp;&amp; -angle &lt;= endAngle) {
  point(centerX + x, centerY - y);
}

//// draw point in range 90 to 135 degrees
if (-180 + angle &gt;= startAngle &amp;&amp; -180 + angle &lt;= endAngle) {
  point(centerX - x, centerY - y);
}

//// draw point in range 135 to 180 degrees
if ((-90 + -angle &gt;= (startAngle) &amp;&amp; -90 + -angle &lt;= (endAngle))) {
  point(centerX - y, centerY - x);
}

// draw point in range 180 to 225 degrees
if (90 + (angle) &gt;= (startAngle) &amp;&amp; 90 + (angle) &lt;= (endAngle)) {
  point(centerX - y, centerY + x);

  System.out.println("angle " + (90 + (angle)));
}

// draw point in range 225 to 270 degrees
if (180 + -angle &gt;= startAngle &amp;&amp; 180 + -angle &lt;= endAngle) {
  point(centerX - x, centerY + y);
}

// draw point in range 270 to 315 degrees
if (angle &gt;= startAngle &amp;&amp; angle &lt;= endAngle) {
  point(centerX + x, centerY + y);
}

// draw point in range 315 to 360 degrees
if (-angle + 90 &gt;= startAngle &amp;&amp; -angle + 90 &lt;= endAngle) {
  point(centerX + y, centerY + x);
}
</code></pre>

<p>}
}
`</p>
]]></description>
   </item>
   <item>
      <title>Rotate with scale gives offset results</title>
      <link>https://forum.processing.org/two/discussion/21876/rotate-with-scale-gives-offset-results</link>
      <pubDate>Fri, 07 Apr 2017 10:18:15 +0000</pubDate>
      <dc:creator>BaconPants</dc:creator>
      <guid isPermaLink="false">21876@/two/discussions</guid>
      <description><![CDATA[<p>When the window is sized correctly to 1280 x 800, the line matches up perfectly with the mouse.
Otherwise, the line is located below or above the mouse depending on the size of the window.</p>

<p>I have tried placing the rotate command before the scale command, and that seems to do the trick, although the dimensions of the rectangle are wrong whenever the window is stretched horizontally or vertically.</p>

<p>If anyone could shed some light on why the line is offset, I would greatly appreciate it.</p>

<pre><code>void draw() {
  background(255);
  translate(width/2, height/2);

  float scaleX = width / 1280.0;
  float scaleY = height / 800.0;

  scale(scaleX, scaleY);
  rotate(atan2((mouseY - height/2), mouseX - width/2));
  rect(-40, -20, 80, 40);
  line(0, 0, 500, 0);
}
</code></pre>

<p><a rel="nofollow" href="https://drive.google.com/file/d/0B2bQl0-53drGRW5Xa3k1emJjMFU/view?usp=sharing">Example 1</a>
<a rel="nofollow" href="https://drive.google.com/file/d/0B2bQl0-53drGdk1tZi1SdDFjVTQ/view?usp=sharing">Example 2</a></p>
]]></description>
   </item>
   <item>
      <title>How to make a cube face a 3D coordinate (x,y,z) ?</title>
      <link>https://forum.processing.org/two/discussion/20133/how-to-make-a-cube-face-a-3d-coordinate-x-y-z</link>
      <pubDate>Sat, 07 Jan 2017 19:23:30 +0000</pubDate>
      <dc:creator>Stephcraft</dc:creator>
      <guid isPermaLink="false">20133@/two/discussions</guid>
      <description><![CDATA[<p>Hi,</p>

<p>I have a problem with a sketch in witch I need to make some cubes face a certain coordinate in P3D
it works for the Z axis just like 2D with atan2() but when it comes to the X and Y axis it doesn't work :/</p>

<p>here is the code I have for now:</p>

<hr />

<pre><code>import peasy.*;

PeasyCam cam;


float x;
float y;
float z;

void setup() {
  size(500,500,P3D);
  cam = new PeasyCam(this, 100);
}

void draw() {
  background(50);

  //the angles
  float angleZ = atan2(-x,y); //-x,y
  float angleX = atan2(0,z);
  float angleY = atan2(-z,x);

  //Cube that need to face the target
  pushMatrix();
  rotateZ(angleZ);
  rotateX(angleX);
  //rotateY(angleY);
  translate(0,0,0);
  box(100);
  line(0,0,0,0,1000,0);
  popMatrix();

  //Cube that need to face the target
  pushMatrix();
  translate(1000,1000,1000);
  rotateZ(atan2(-x+1000,y-1000));
  //rotateX(atan2(y+100,-z-100));
  box(100);
  line(0,0,0,0,1000,0);
  popMatrix();

  //to control the little cube (target)
  if(keyPressed) {
   if(keyCode==UP) {
      y+=4;
    }
    if(keyCode==DOWN) {
      y-=4;
    }
    if(keyCode==LEFT) {
      x+=4;
    }
    if(keyCode==RIGHT) {
      x-=4;
    }
    if(key==' ') {
      z+=4;
    }
    if(keyCode==SHIFT) {
      z-=4;
    }
  }

  //little cube (taget)
  pushMatrix();
  translate(x,y,z);
  box(50);
  popMatrix();
}
</code></pre>

<hr />

<p>I used the peasyCam so its easier to see and control</p>

<p>float x,y,z are the targets to face, you can control them with Arrows Space and Shift</p>

<p>thanks !</p>
]]></description>
   </item>
   <item>
      <title>Orientate an image</title>
      <link>https://forum.processing.org/two/discussion/19450/orientate-an-image</link>
      <pubDate>Thu, 01 Dec 2016 15:35:34 +0000</pubDate>
      <dc:creator>FATGEEK</dc:creator>
      <guid isPermaLink="false">19450@/two/discussions</guid>
      <description><![CDATA[<p>Hi, I want to orientate an image toward a point. In fact that's an arrow, and I want it to point my mouse. How can I do it ?</p>
]]></description>
   </item>
   <item>
      <title>How to display a conic section sliced by a plane</title>
      <link>https://forum.processing.org/two/discussion/17717/how-to-display-a-conic-section-sliced-by-a-plane</link>
      <pubDate>Mon, 01 Aug 2016 18:28:06 +0000</pubDate>
      <dc:creator>OLAM</dc:creator>
      <guid isPermaLink="false">17717@/two/discussions</guid>
      <description><![CDATA[<p>How would one display the sliced section of cone as if it were a moving flashlight shinning on a floor?</p>
]]></description>
   </item>
   <item>
      <title>Find a point inside a bezier curve</title>
      <link>https://forum.processing.org/two/discussion/16542/find-a-point-inside-a-bezier-curve</link>
      <pubDate>Wed, 11 May 2016 10:12:52 +0000</pubDate>
      <dc:creator>Dazzid</dc:creator>
      <guid isPermaLink="false">16542@/two/discussions</guid>
      <description><![CDATA[<p>Hi Right now I'm drawing a triangle within a line to define the orientation of the line. However, now I need to do the same to a curve using bezier, but don't find the way to pick that point in the curve, for example in the 33% of the curve.
Here my code:</p>

<pre><code>        point1x = pos_1.x+(pos_2.x-pos_1.x)/4*3;
        point1y = pos_1.y+(pos_2.y-pos_1.y)/4*3;
        var toy = point1y;
        var fromy = pos_1.y;
        var tox = point1x;
        var fromx = pos_1.x;

        var angle = Math.atan2(toy-fromy,tox-fromx);

        var pointBx = tox-(headlen+30)*Math.cos(angle+Math.PI/6);
        var pointBy = toy-(headlen+30)*Math.sin(angle+Math.PI/6);

        var point2x = tox-headlen*Math.cos(angle-Math.PI/6);
        var point2y = toy-headlen*Math.sin(angle-Math.PI/6);
        var point3x = tox-headlen*Math.cos(angle+Math.PI/6);
        var point3y = toy-headlen*Math.sin(angle+Math.PI/6);

        noFill();
        bezier(pos_1.x, pos_1.y, pointBx,  pointBy, point3x, point3y, pos_2.x, pos_2.y);
        stroke(myColor);
        strokeWeight(weight);
        line(pos_1.x, pos_1.y, pos_2.x, pos_2.y);

        fill(myColor);
        triangle(point1x, point1y, point2x, point2y, point3x, point3y);
</code></pre>
]]></description>
   </item>
   <item>
      <title>possible to check if two arc's intersect?</title>
      <link>https://forum.processing.org/two/discussion/16495/possible-to-check-if-two-arc-s-intersect</link>
      <pubDate>Sun, 08 May 2016 19:22:27 +0000</pubDate>
      <dc:creator>ottenm</dc:creator>
      <guid isPermaLink="false">16495@/two/discussions</guid>
      <description><![CDATA[<p>Subject says it all.  I'm drawing a number of arc's like this:</p>

<pre><code>arc(x1, y1, w1, h1, start1, stop1, PIE);
arc(x2, y2, w2, h2, start2, stop2, PIE);
</code></pre>

<p>... and I'd like to find out before I draw them if the arcs intersect/overlap/share pixels (using these terms synonymously).  It's not enough to check the two ellipses because the ellipses can overlap long before the arcs overlap.</p>

<p>Would prefer some nice elegant math, but I do have one duct-tape idea:<br />
Draw both arcs to some kind of off screen buffer using semi-transparent fill colors, then scan the buffer for pixels colored to reflect an overlap.  Big buffer, so probably check the ellipses first, then just check pixels within the ellipses somehow.</p>

<p>Very grateful for any suggestions!</p>
]]></description>
   </item>
   <item>
      <title>Error PGraphics 3D using Syphon</title>
      <link>https://forum.processing.org/two/discussion/16190/error-pgraphics-3d-using-syphon</link>
      <pubDate>Fri, 22 Apr 2016 17:02:16 +0000</pubDate>
      <dc:creator>Gualio</dc:creator>
      <guid isPermaLink="false">16190@/two/discussions</guid>
      <description><![CDATA[<p>I'm using Syphon ibrary in processing 2.2.1 and i get always the same error :</p>

<p>"ClassCastException: processing.core.PGraphics3D cannot be cast to processing.opengl.PGraphicsOpenGL"</p>

<p>Any help please????</p>

<pre><code>/* OpenProcessing Tweak of *@*<a href="http://www.openprocessing.org/sketch/6742*@*" target="_blank" rel="nofollow">http://www.openprocessing.org/sketch/6742*@*</a> */
/* !do not delete the line above, required for linking your tweak if you upload again */

      ////////////////////////////
      //                        //
      //        doodle 3        //
      //                        //
      ////////////////////////////

      // (c) Martin Schneider 2009
      import codeanticode.syphon.*;
      PGraphics canvas;
    SyphonServer server;

    int n = 1000;
    int maxage = 20;
    int rdodge = 30;
    int opacity = 40;
    float speed = .2;
    float zoom = .01;
    boolean crayons, soft, dodge = true;

    float[][] a = new float[n][2];
    int[] age = new int[n];
    float w, h, s;
    int t, c;


    void setup() {
      size(500, 500,P3D);
      canvas = createGraphics(500, 500, P3D);
        // Create syhpon server to send frames out.
    server = new SyphonServer(this, "Processing Syphon");
      w = width/2;
      h = height/2;
      colorMode(HSB, TWO_PI, 2, 1);
      smooth();
      reset();

    }


    void draw() {
      canvas.beginDraw();
      // create new particles
      int np = n / maxage;
      for(int i=0; i&lt;np &amp; c&lt;n; i++, c++) newp(c);
      // draw particle traces
      for(int i=0; i&lt;c; i++) {
        age[i]++;
        float[] p = a[i];
        if (age[i] &gt; maxage) newp(i);
        else {
          float[] f = f(p[0], p[1]);     
          // opacity based on speed (soft mode) or age (hard mode) 
          int m = maxage/2;
          float o = soft ? mag(f[0], f[1]) * 2 * opacity : (m - abs(m - age[i])) * opacity/m;
          // hue based on direction
          float h =  atan2(f[0], f[1]) + PI;   
          canvas.stroke(h, crayons ? 1 : 0, crayons ? 1 : 0, o); 
          // draw line while updating position
          canvas.line(p[0], p[1], p[0] += s*f[0],  p[1] += s*f[1]);
        }
      }
      canvas.endDraw();
      image(canvas, 0, 0);
      server.sendImage(canvas);
    }


    // noise based flow field
    float[] f(float x, float y) {;
      return new float[] {
        noise(t, x * zoom, y * zoom)-.5,
        noise(t+1, x * zoom, y * zoom) - .5
      };
    }


    void newp(int p) {
      if(dodge) { 
        // particle inside a circle around the mouse position
        float r = random(rdodge), ang = random(TWO_PI);
        a[p] = new float[] { mouseX + r * cos(ang), mouseY + r *sin(ang) };  
      } else {  
        // particle anywhere on screen
        a[p] = new float[] { random(width), random(height) };
      }
      age[p] = 0;
    }


    void reset() {
      canvas.background(crayons ? 0 : #ffffff);
      s = speed / zoom;
      c = 0;
    }


    void keyPressed() { 
      switch(key) {
        case 's' : soft = !soft; break;
        case 'd' : dodge = !dodge; break;
        case 'f' : t++; break;
        case 'c' : crayons = !crayons; break;
        case '+' : zoom /= 1.1; break;
        case '-' : zoom *= 1.1; break;
        case ' ' : break;
        default: return;
      } 
      reset();
    }
</code></pre>
]]></description>
   </item>
   <item>
      <title>Using heading() to generate angles</title>
      <link>https://forum.processing.org/two/discussion/14302/using-heading-to-generate-angles</link>
      <pubDate>Wed, 06 Jan 2016 15:45:51 +0000</pubDate>
      <dc:creator>Robbert</dc:creator>
      <guid isPermaLink="false">14302@/two/discussions</guid>
      <description><![CDATA[<p>I'm trying to use heading to position a row of squares with an angle in direction to the mouse.
Just as a test for using vector's and angles. I wrote a function for the squares, and generate an angle with mouseX and mouseY. Though P5 calculates the angles from the 0,0 point of the canvas, so i tried to create the vector in a matrix, but this doesn't seem to change much. So the question is; is there a way to generate the angle with mouseX/mouseY relative to the position of the object that has been created. or do I have to use if statements to check whether the X/Y of the mouse is &lt;/&gt; relative to the objects position.</p>

<p>Code:</p>

<pre><code>var rectArray = [];

var mouseVector;

function setup() {
  createCanvas(windowWidth,windowHeight);

  for (i = 0;i &lt; 100; i++) {
  rectArray.push(new Square(10*i,40));
  }
}

function draw() {

  for (i = 0; i &lt; rectArray.length; i++) {
    rectArray[i].rotation();
    rectArray[i].update();
    rectArray[i].display();
  }

}

function Square(x,y) {
  this.x = x;
  this.y = y;
  this.size = 20;
  this.color = color(200);
  this.target = 0;
  this.rotationValue = 0;

  this.rotation = function() {
    push();
    translate(this.x,this.y);
    mouseVector = createVector(mouseX,mouseY);
    this.target = mouseVector.heading();
    this.rotationValue = this.target;
    pop();
  }

  this.update = function() {
    //this.x = this.x + 1;
  }

  this.display = function() {
    push();
    translate(this.x,this.y);
    rectMode(CENTER);
    rotate(this.rotationValue);
    fill(this.color);
    rect(0,0,this.size,this.size);
    pop();
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Changing colours of triangles with mousex and mousey</title>
      <link>https://forum.processing.org/two/discussion/13808/changing-colours-of-triangles-with-mousex-and-mousey</link>
      <pubDate>Sun, 06 Dec 2015 18:09:02 +0000</pubDate>
      <dc:creator>R_B</dc:creator>
      <guid isPermaLink="false">13808@/two/discussions</guid>
      <description><![CDATA[<p>I'm trying to make a simple sketch where depending on mouse location, the triangle changes colour.</p>

<p>I can do this for quarter squares by saying 
if (mouseX &lt; 300 &amp; mouseY &lt; 300) {
rect ...  }</p>

<p>but am unable to work out how to define the triangle from two corners to the centre - shown in my sketch as coloured red.</p>

<pre><code>void draw() {
  background(145,180,70);

  if (mouseX &lt; 300 &amp; mouseY &lt; 300) {
    triangle(0, 0, 0,600, 300,300); // Left
    fill(190,75,90);
    }

    if (mouseX &gt; 300 &amp; mouseY &gt; 300){
    triangle(300, 300, 600,600, 600,0); // Left
    fill(190,75,90); // Right
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Hover over pie chart segment and show the array value associated</title>
      <link>https://forum.processing.org/two/discussion/13609/hover-over-pie-chart-segment-and-show-the-array-value-associated</link>
      <pubDate>Mon, 23 Nov 2015 08:56:53 +0000</pubDate>
      <dc:creator>[Deleted User]</dc:creator>
      <guid isPermaLink="false">13609@/two/discussions</guid>
      <description><![CDATA[<p>I created a pie chart and the sizes of the segments are based off of values in an array. I can't seem to figure out how I can hover over the specific segment and show the corresponding array value.</p>

<p>Can anyone point me in the right direction?</p>
]]></description>
   </item>
   <item>
      <title>How do you constrain an angle</title>
      <link>https://forum.processing.org/two/discussion/12887/how-do-you-constrain-an-angle</link>
      <pubDate>Thu, 08 Oct 2015 02:00:35 +0000</pubDate>
      <dc:creator>gperez</dc:creator>
      <guid isPermaLink="false">12887@/two/discussions</guid>
      <description><![CDATA[<p>Hi. I'm trying to constrain an angle. , measured in radians. But radians takes negative values so constrain() method will not work.</p>

<p>So, i tried converting radians to angles, constrain degrees and convert to radians again. But the result is the same.</p>

<p><code>a = radians(constrain(degrees(a), 90, 270));</code></p>

<p>Any clue?</p>
]]></description>
   </item>
   <item>
      <title>Move a triangle to point while pointing at it</title>
      <link>https://forum.processing.org/two/discussion/11962/move-a-triangle-to-point-while-pointing-at-it</link>
      <pubDate>Tue, 04 Aug 2015 16:03:13 +0000</pubDate>
      <dc:creator>moesel</dc:creator>
      <guid isPermaLink="false">11962@/two/discussions</guid>
      <description><![CDATA[<p>Hello,</p>

<p>I have tried to make a programm where a red cirlce (the leader as I call it) moves to the mouse position. Additionally to that several small traingles are supposed to follow the leader to a certain point next to the leader, which functions as I wanted but I tried to make the triangles point in the direction where they are heading. But i didnt manage to achieve that so i wanted to ask if someone can help me with that. Here is the follower class:</p>

<pre><code>    class Follower
    {

      PVector followerSupposedPos = new PVector();            //Position of the follower which it should reach
      PVector followerPos         = new PVector();            // actual position of the follower
      PVector distance            = new PVector();            //distance (magnitude will be changed) between the followerPos and  followerSupposedPos
      PVector preFollower         = new PVector();            //position of the follower next to it(left)
      PVector leaderPos           = new PVector();            // position of the leader
      PVector realDistance        = new PVector();            //the real distance between the followerPos and  followerSupposedPos

      boolean numberOne;

      float speed;

      float angle;

      Follower(float speed_)
      {
        speed = speed_;
      }

      void run(PVector leaderPos_, boolean numberOne_, PVector preFollower_)
      {
        preFollower = preFollower_;
        numberOne   = numberOne_;
        leaderPos   = leaderPos_;

        if (numberOne_ == true)
        {
          followerSupposedPos.x = leaderPos.x - 100;
          followerSupposedPos.y = leaderPos.y + 20;
          fill(200, 100, 50);
        } else if (numberOne_ == false)
        {
          followerSupposedPos.x = preFollower.x + 13;
          followerSupposedPos.y = preFollower.y;
        }

        //points(); //the direction the follower points

        display();

        move();

      }


      PVector impart()  // needed for the preFollower position
      {
        return followerSupposedPos;
      }

      void display()
      {      
        fill(255, 0, 0, 100);
        ellipse(followerPos.x, followerPos.y, 5, 5);
        fill(200, 150, 10);
        triangle(followerPos.x, followerPos.y, followerPos.x - 5, 
        followerPos.y + 10, followerPos.x+ 5, followerPos.y + 10);
      }

      void move()
      {
        PVector distancePrint = new PVector(distance.x * 1000, distance.y * 1000);

        realDistance.x = followerSupposedPos.x - followerPos.x;
        realDistance.y = followerSupposedPos.y - followerPos.y;

        distance.x = realDistance.x * 0.007;
        distance.y = realDistance.y * 0.007;


        if (realDistance.mag() &gt;= 4)
        {
          distance.setMag(5);

          followerPos.x += distance.x + speed;
          followerPos.y += distance.y + speed;
        } else
        {
          distance.setMag(0);
        }

        println(realDistance.mag());
      }

      void points()
      {
        angle = atan2(realDistance.y, realDistance.x);
      }

    }
</code></pre>

<p>and a programm where I managed to make a triangle point at the mouse without moving (didnt work in the programm above) :</p>

<pre><code>float angle;
PVector d = new PVector();


void draw()
{  
  background(0);

  translate(width/2, height/2);

  d.x = mouseX - width/2;
  d.y = mouseY - height/2;

  angle = atan2(d.y, d.x);



  triangle_(angle);
}

void triangle_(float ang)
{
  pushMatrix();

  rotate(ang);
  triangle(0, 0, -50, -20, - 50, 20);
  popMatrix();
}
</code></pre>

<p>does somebody know how I can connect these two programms or a another way to achieve what i what?</p>
]]></description>
   </item>
   <item>
      <title>Display relative bearing of joystick</title>
      <link>https://forum.processing.org/two/discussion/11898/display-relative-bearing-of-joystick</link>
      <pubDate>Thu, 30 Jul 2015 12:33:09 +0000</pubDate>
      <dc:creator>harrzack</dc:creator>
      <guid isPermaLink="false">11898@/two/discussions</guid>
      <description><![CDATA[<p>I'm creating a simple radar simulation and need to display the relative bearing of a joystick cursor.  I'm using the GameControlPlus library, and getting excellent results with a Logitech Extreme 3D stick.  I'm just not up to speed on the trig to do this...</p>

<p>I'd like to be able to display the relative degree position of the joystick cursor, such at at the top of the screen it would show 0 degrees,  when in the 3 o'clock position it would be 90 degree, and 6 o'clock would show 180 degrees and a 9 o'clock it would show 270... and so on.  I guess the max would be 359 deg after which the bearing would become 0 again.
This is relative to the center of the screen, so the Y portion (width) should have no effect on the reading.</p>

<p>I suspect this is fairly trivial to do, but as a Processing noob, my best efforts have failed!</p>
]]></description>
   </item>
   <item>
      <title>Calculate direction change</title>
      <link>https://forum.processing.org/two/discussion/11030/calculate-direction-change</link>
      <pubDate>Wed, 27 May 2015 14:52:50 +0000</pubDate>
      <dc:creator>clankill3r</dc:creator>
      <guid isPermaLink="false">11030@/two/discussions</guid>
      <description><![CDATA[<p>I want to calculate values based on the change in direction.
If it keeps going the same it should be 1.
If it goes back (so 180 degrees change) then it should be 0.
And then everything in-between.</p>

<p>As last, the calculations should be fast! 
I tried some things with the cross and dot product but not sure if that is the right way to go.</p>

<p>Hope someone can help.</p>

<p><img src="http://forum.processing.org/two/uploads/imageupload/690/PWFP8QEKLXU3.png" alt="Screen Shot 2015-05-27 at 4.45.11 PM" title="Screen Shot 2015-05-27 at 4.45.11 PM" /></p>

<p>Here some code to get started, with my attempts removed.</p>

<pre><code>PVector a, b, c;

void setup() {
  size(600, 600);

  a = new PVector(50, 50);
  b = new PVector(width/2, height/2);
  c = new PVector();

}

void draw() {
  c.set(mouseX, mouseY);

  background(0);
  beginShape();
  stroke(255);
  noFill();
  vertex(a.x, a.y);
  vertex(b.x, b.y);
  vertex(c.x, c.y);
  endShape();

  // magic...


}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Compare a list of vecs against a angle</title>
      <link>https://forum.processing.org/two/discussion/11104/compare-a-list-of-vecs-against-a-angle</link>
      <pubDate>Mon, 01 Jun 2015 17:44:02 +0000</pubDate>
      <dc:creator>clankill3r</dc:creator>
      <guid isPermaLink="false">11104@/two/discussions</guid>
      <description><![CDATA[<p>I have a line from the center to the mouse. That line has a certain angle.
For the rest I have random points on the screen.
I want to order those points in such a way that they are compared to the angle from the center to the mouse.</p>

<p><img src="http://forum.processing.org/two/uploads/imageupload/099/1NH4F4F6KY3Y.png" alt="Screen Shot 2015-06-01 at 7.40.22 PM" title="Screen Shot 2015-06-01 at 7.40.22 PM" /></p>

<p>So 0 stays 0. 11 becomes 1, 6 becomes 2, 10 becomes 3 etc.
In other words, if the line would move clockwise then they get numbered in that order.</p>

<p>I have no idea how to make a Comparator for that. I hope someone can help.</p>

<p>O yeah, if you want to run you need eclipse or IntelliJ cause Processing will give a scope problem.</p>

<pre><code>import processing.core.PApplet;
import processing.core.PVector;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;


public class CompareToAngle extends PApplet {

     public static void main(String[] args) {
         PApplet.main("CompareToAngle", args);
     }


    public void settings() {
        size(1024, 1024);

    }

    public void setup() {

    }


    public void draw() {
        background(255);

        PVector a = new PVector(width/2, height/2);
        PVector b = new PVector(mouseX, mouseY);

        line(a.x, a.y, b.x, b.y);

        // compare against this angle!!
        // compare against this angle!!
        // compare against this angle!!
        // compare against this angle!!
        // compare against this angle!!
        float angle = atan2(b.y-a.y, b.x-a.x);

        fill(0);
        text(angle, b.x, b.y);

        ArrayList&lt;PVector&gt; vecs = new ArrayList&lt;PVector&gt;();

        randomSeed(1);
        for (int i = 0; i &lt; 15; i++ ) {
            PVector v = new PVector(random(50, width-50), random(50, height-50));
            vecs.add(v);
        }

        Collections.sort(vecs, new Comparator&lt;PVector&gt;() {

            <a href="/two/profile/Override">@Override</a>
            public int compare(PVector o1, PVector o2) {

                float angleO1 = atan2(b.y-o1.y, b.x-o1.x);
                float angleO2 = atan2(b.y-o2.y, b.x-o2.x);

                if (angleO1 &gt; angleO2) {
                    if (angleO1 &gt; angle) {
                        return 1;
                    }
                    if (angleO1 &lt; angle) {
                        return -1;
                    }
                }
                else if (angleO1 &lt; angleO2) {
                    if (angleO2 &gt; angle) {
                        return 1;
                    }
                    if (angleO2 &lt; angle) {
                        return -1;
                    }

                }

                return 0;
            }
        });

        int c = 0;
        for (PVector v : vecs) {
            ellipse(v.x, v.y, 5, 5);
            text(c++, v.x+20, v.y);
        }





    }

}
</code></pre>
]]></description>
   </item>
   <item>
      <title>atan2() -&gt; I don't really get what this is doing / difference from atan()</title>
      <link>https://forum.processing.org/two/discussion/9859/atan2-i-don-t-really-get-what-this-is-doing-difference-from-atan</link>
      <pubDate>Sat, 14 Mar 2015 13:08:33 +0000</pubDate>
      <dc:creator>sjon</dc:creator>
      <guid isPermaLink="false">9859@/two/discussions</guid>
      <description><![CDATA[<p>From the docs :</p>

<p>atan2()</p>

<p><em>Calculates the angle (in radians) from a specified point to the coordinate origin as measured from the positive x-axis. Values are returned as a float in the range from PI to -PI. The atan2() function is most often used for orienting geometry to the position of the cursor. Note: The y-coordinate of the point is the first parameter, and the x-coordinate is the second parameter, due the the structure of calculating the tangent</em></p>

<p>atan()</p>

<p><em>The inverse of tan(), returns the arc tangent of a value. This function expects the values in the range of -Infinity to Infinity (exclusive) and values are returned in the range -PI/2 to PI/2</em></p>

<hr />

<p>I'm not too sure what the real difference is here though. I get that atan2() is generally used instead of atan() for movement, and I've seen it work better, but I don't really understand why this is.</p>

<p>Can anyone explain further please?</p>

<p>thanks</p>
]]></description>
   </item>
   </channel>
</rss>