topical media & game development

talk show tell print

#javascript-processing-example-basic-math-polartocartesian.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>PolarToCartesian</h2>
  
  <p>by Daniel Shiffman.  
  
  Convert a polar coordinate (r,theta) to cartesian (x,y):  
  x = rcos(theta)
  y = rsin(theta)</p>
  
  <p><a href="http://processing.org/learning/basics/polartocartesian.html"><b>Original Processing.org Example:</b> PolarToCartesian</a><br>
  <script type="application/processing">
  float r;
  
  // Angle and angular velocity, accleration
  float theta;
  float theta_vel;
  float theta_acc;
  
  void setup() {
    size(200,200);
    frameRate(30);
    smooth();
    
    // Initialize all values
    r = 50.0f;
    theta = 0.0f;
    theta_vel = 0.0f;
    theta_acc = 0.0001f;
  }
  
  void draw() {
    background(0);
    // Translate the origin point to the center of the screen
    translate(width/2,height/2);
    
    // Convert polar to cartesian
    float x = r * cos(theta);
    float y = r * sin(theta);
    
    // Draw the ellipse at the cartesian coordinate
    ellipseMode(CENTER);
    noStroke();
    fill(200);
    ellipse(x,y,16,16);
    
    // Apply acceleration and velocity to angle (r remains static in this example)
    theta_vel += theta_acc;
    theta += theta_vel;
  
  }
  </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>
  float r;
  
  // Angle and angular velocity, accleration
  float theta;
  float theta_vel;
  float theta_acc;
  
  void setup() {
    size(200,200);
    frameRate(30);
    smooth();
    
    // Initialize all values
    r = 50.0f;
    theta = 0.0f;
    theta_vel = 0.0f;
    theta_acc = 0.0001f;
  }
  
  void draw() {
    background(0);
    // Translate the origin point to the center of the screen
    translate(width/2,height/2);
    
    // Convert polar to cartesian
    float x = r * cos(theta);
    float y = r * sin(theta);
    
    // Draw the ellipse at the cartesian coordinate
    ellipseMode(CENTER);
    noStroke();
    fill(200);
    ellipse(x,y,16,16);
    
    // Apply acceleration and velocity to angle (r remains static in this example)
    theta_vel += theta_acc;
    theta += theta_vel;
  
  }</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.