Gregory Igehy

Dancing at hemisphere coordinate

Simple 2d graph by processing

f:id:gregory-igehy:20150720190809p:plain

// Simple 2d graph by processing

// x/y
float g_min_x = 0.0;
float g_max_x = 1.0;
float g_min_y = 0.0;
float g_max_y = 1.0;

// labels
String g_graph_title  = "Title";
String g_row_label    = "X label";
String g_column_label = "Y label";

// graph
int    g_row_count       = 100;
int    g_column_count    = 100;
float  g_row_sub_grid    = 10.0;
float  g_column_sub_grid = 10.0;
float  g_plot_x1, g_plot_y1;
float  g_plot_x2, g_plot_y2;
float  g_label_x, g_label_y;
PFont  g_plot_font; 

void setup() 
{
  size(480, 480);
      
  // Corners of the plotted time series
  g_plot_x1 = 100; 
  g_plot_x2 = width - 80;
  g_label_x = 50;
  
  g_plot_y1 = 100;
  g_plot_y2 = height - 80;
  g_label_y = height - 25;
  
  g_plot_font = createFont("SansSerif", 20);
  textFont( g_plot_font );

  smooth();
}

void draw()
{
  background(224);
  
  // Show the plot area as a white box  
  fill(255);
  rectMode(CORNERS);
  noStroke();
  rect( g_plot_x1, g_plot_y1, g_plot_x2, g_plot_y2 );

  drawTitle();
  drawAxisLabels();
  drawSubGrid();

  stroke(#5679C1);
  strokeWeight(5);
  noFill();
  drawDataLine();  
}

// value is between 0 and 1.
float getX( float value )
{
  float x = value;
  return x;
}

// value is between 0 and 1.
float getY( float value )
{
  float y = ( value * value );
  return y;
}


void drawTitle() 
{
  fill(0);
  textSize(20);
  textAlign(LEFT);

  String title = g_graph_title;
  text(title, g_plot_x1, g_plot_y1 - 10);
}

void drawAxisLabels() 
{
  fill(0);
  textSize(13);
  textLeading(15);
  
  textAlign(CENTER, CENTER);
  text( g_column_label,
        g_label_x,
        ( g_plot_y1 + g_plot_y2 )/2);
         
  textAlign(CENTER);
  text( g_row_label, 
        ( g_plot_x1 + g_plot_x2)/2,
        g_label_y);
}

void drawDataLine()
{  
  beginShape();
  for (int row = 0; row < g_row_count; row++) 
  { 
      float value  = float( row ) / float( g_row_count );
      float     x  = getX( value );
      float     y  = getY( value );
    
      float draw_x = map( x, g_min_x, g_max_x, g_plot_x1, g_plot_x2 ); 
      float draw_y = map( y, g_min_y, g_max_y, g_plot_y2, g_plot_y1 );
      
      vertex( draw_x, draw_y);
  }
  endShape();
}

void drawSubGrid()
{
   fill(0);
   textSize(10);
   textAlign(CENTER);
  
   // Use thin, gray lines to draw the grid
   stroke(224);
   strokeWeight(1);
   
   for (int row = 0; row < g_row_count; row += int( g_row_sub_grid ) ) 
   {
     float x = map( g_row_sub_grid * row,
                    0.0, g_row_sub_grid * g_row_count,
                    g_plot_x1, g_plot_x2 ); 
     line(x, g_plot_y1, x, g_plot_y2);
   }
   
   for (int column = 0; column < g_column_count; column += int( g_column_sub_grid ) ) 
   {
     float y = map( g_column_sub_grid * column,
                    0.0, g_column_sub_grid * g_column_count,
                    g_plot_y2, g_plot_y1 ); 
     line( g_plot_x1, y, g_plot_x2, y );
   }   
}