Rendering a Sine Wave

Sine Wave by Daniel Shiffman - Example on

int xspacing = 16;   // How far apart should each horizontal location be spaced
int w;              // Width of entire wave

float theta = 0.0;  // Start angle at 0
float amplitude = 75.0;  // Height of wave
float period = 500.0;  // How many pixels before the wave repeats
float dx;  // Value for incrementing X, a function of period and xspacing
float[] yvalues;  // Using an array to store height values for the wave

void setup() {
  size(640, 360);
  w = width+16;
  dx = (TWO_PI / period) * xspacing;
  yvalues = new float[w/xspacing];

void draw() {

void calcWave() {
  // Increment theta (try different values for 'angular velocity' here
  theta += 0.02;

  // For every x value, calculate a y value with sine function
  float x = theta;
  for (int i = 0; i < yvalues.length; i++) {
    yvalues[i] = sin(x)*amplitude;

void renderWave() {
  // A simple way to draw the wave with an ellipse at each location
  for (int x = 0; x < yvalues.length; x++) {
    ellipse(x*xspacing, height/2+yvalues[x], 16, 16);

The example uses a sine calculation to generate a regular set of Y values for a group of circles that are rendered at each position. 

It has been thoroughly commented, making most of the breakdown I would do pointless.


I find the repetitive motion of a sine wave is an interesting visual to be associated with music and sound. The construction and rendering are also relatively simple to break down and modify. The imagery I imagine for my visualizer consists of three sine waves responding to the different sections of the frequency spectrum. In addition, I think adding a similar commenting method could make my system more accessible to users for modification.