Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

4. Time Series > Ways to Connect Points (Refine)

Ways to Connect Points (Refine)

Connecting the points with a curve is often a better option because it prevents the spikiness of the plot from overwhelming the data itself. The curveVertex( ) function is similar to the vertex( ) function, except that it connects successive points by fitting them to a curve.

The drawDataCurve( ) method, a modification of drawDataLine( ), follows:

void drawDataCurve(int col) {
  beginShape(  );
  for (int row = 0; row < rowCount; row++) {
    if (data.isValid(row, col)) {
      float value = data.getFloat(row, col);
      float x = map(years[row], yearMin, yearMax, plotX1, plotX2);
      float y = map(value, dataMin, dataMax, plotY2, plotY1);curveVertex(x, y);
      // Double the curve points for the start and stop
      if ((row == 0) || (row == rowCount-1)) {
        curveVertex(x, y);
      }
    }
  }
  endShape(  );
}

To draw a curve with curveVertex( ), at least four points are necessary because the first and last coordinates in curveVertex( ) are used to guide the angle at which the curve begins and ends. In this particular example, doubling start and stop points will work fine. In other cases, additional points can be used to maintain continuity between two connected curves.

The results of using a smooth curve can be seen most clearly when comparing the coffee data drawn with vertex( ) and curveVertex( ) in Figure 4-11.

Comparison of the use of vertices (top) and curve vertices (bottom)

Figure 4-11. Comparison of the use of vertices (top) and curve vertices (bottom)