topical media & game development
#javascript-processing-example-basic-math-sinecosine.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>SineCosine</h2>
<p>Linear movement with sin() and cos().
Numbers between 0 and PI*2 (TWO_PI which is roughly 6.28)
are put into these functions and numbers between -1 and 1 are
returned. These values are then scaled to produce larger movements.</p>
<p><a href="http://processing.org/learning/basics/sinecosine.html"><b>Original Processing.org Example:</b> SineCosine</a><br>
<script type="application/processing">
int i = 45;
int j = 225;
float pos1 = 0;
float pos2 = 0;
float pos3 = 0;
float pos4 = 0;
int sc = 40;
void setup()
{
size(200, 200);
noStroke();
smooth();
}
void draw()
{
background(0);
fill(51);
rect(60, 60, 80, 80);
fill(255);
ellipse(pos1, 36, 32, 32);
fill(153);
ellipse(36, pos2, 32, 32);
fill(255);
ellipse(pos3, 164, 32, 32);
fill(153);
ellipse(164, pos4, 32, 32);
i += 3;
j -= 3;
if(i > 405) {
i = 45;
j = 225;
}
float ang1 = radians(i); // convert degrees to radians
float ang2 = radians(j); // convert degrees to radians
pos1 = width/2 + (sc * cos(ang1));
pos2 = width/2 + (sc * sin(ang1));
pos3 = width/2 + (sc * cos(ang2));
pos4 = width/2 + (sc * sin(ang2));
}
</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>
int i = 45;
int j = 225;
float pos1 = 0;
float pos2 = 0;
float pos3 = 0;
float pos4 = 0;
int sc = 40;
void setup()
{
size(200, 200);
noStroke();
smooth();
}
void draw()
{
background(0);
fill(51);
rect(60, 60, 80, 80);
fill(255);
ellipse(pos1, 36, 32, 32);
fill(153);
ellipse(36, pos2, 32, 32);
fill(255);
ellipse(pos3, 164, 32, 32);
fill(153);
ellipse(164, pos4, 32, 32);
i += 3;
j -= 3;
if(i > 405) {
i = 45;
j = 225;
}
float ang1 = radians(i); // convert degrees to radians
float ang2 = radians(j); // convert degrees to radians
pos1 = width/2 + (sc * cos(ang1));
pos2 = width/2 + (sc * sin(ang1));
pos3 = width/2 + (sc * cos(ang2));
pos4 = width/2 + (sc * sin(ang2));
}</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.