topical media & game development

talk show tell print

#javascript-processing-example-basic-math-arctangent.htm / htm



  <!DOCTYPE html>
  <html><head>
  <script src="javascript-processing-example-processing.js"></script>
  <script src="javascript-processing-example-init.js"></script>
  <link rel="stylesheet" href="javascript-processing-example-style.css">
  </head><body><h1><a href="http://ejohn.org/blog/processingjs/">Processing.js</a></h1>
  <h2>Arctangent</h2>
  
  <p>Move the mouse to change the direction of the eyes. 
  The atan2() function computes the angle from each eye 
  to the cursor. 
  
  Created 1 September 2002</p>
  
  <p><a href="http://processing.org/learning/basics/arctangent.html"><b>Original Processing.org Example:</b> Arctangent</a><br>
  <script type="application/processing">
  Eye e1, e2, e3, e4, e5;
  
  void setup() 
  {
    size(200, 200);
    smooth();
    noStroke();
    e1 = new Eye( 50,  16,  80);
    e2 = new Eye( 64,  85,  40);  
    e3 = new Eye( 90, 200, 120);
    e4 = new Eye(150,  44,  40); 
    e5 = new Eye(175, 120,  80);
  }
  
  void draw() 
  {
    background(102);
    
    e1.update(mouseX, mouseY);
    e2.update(mouseX, mouseY);
    e3.update(mouseX, mouseY);
    e4.update(mouseX, mouseY);
    e5.update(mouseX, mouseY);
  
    e1.display();
    e2.display();
    e3.display();
    e4.display();
    e5.display();
  }
  
  class Eye 
  {
    int ex, ey;
    int size;
    float angle = 0.0;
    
    Eye(int x, int y, int s) {
      ex = x;
      ey = y;
      size = s;
   }
  
    void update(int mx, int my) {
      angle = atan2(my-ey, mx-ex);
    }
    
    void display() {
      pushMatrix();
      translate(ex, ey);
      fill(255);
      ellipse(0, 0, size, size);
      rotate(angle);
      fill(153);
      ellipse(size/4, 0, size/2, size/2);
      popMatrix();
    }
  }
  </script><canvas width="200" height="200"></canvas></p>
  <div style="overflow: hidden; height: 0px; width: 0px;"></div>
  
  <pre><b>// All Examples Written by <a href="http://reas.com/">Casey Reas</a> and <a href="http://benfry.com/">Ben Fry</a>
  // unless otherwise stated.</b>
  Eye e1, e2, e3, e4, e5;
  
  void setup() 
  {
    size(200, 200);
    smooth();
    noStroke();
    e1 = new Eye( 50,  16,  80);
    e2 = new Eye( 64,  85,  40);  
    e3 = new Eye( 90, 200, 120);
    e4 = new Eye(150,  44,  40); 
    e5 = new Eye(175, 120,  80);
  }
  
  void draw() 
  {
    background(102);
    
    e1.update(mouseX, mouseY);
    e2.update(mouseX, mouseY);
    e3.update(mouseX, mouseY);
    e4.update(mouseX, mouseY);
    e5.update(mouseX, mouseY);
  
    e1.display();
    e2.display();
    e3.display();
    e4.display();
    e5.display();
  }
  
  class Eye 
  {
    int ex, ey;
    int size;
    float angle = 0.0;
    
    Eye(int x, int y, int s) {
      ex = x;
      ey = y;
      size = s;
   }
  
    void update(int mx, int my) {
      angle = atan2(my-ey, mx-ex);
    }
    
    void display() {
      pushMatrix();
      translate(ex, ey);
      fill(255);
      ellipse(0, 0, size, size);
      rotate(angle);
      fill(153);
      ellipse(size/4, 0, size/2, size/2);
      popMatrix();
    }
  }</pre>
  </body></html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.