Search code examples
javaprocessingzooming

Receiving the following error when using zoom function in processing: InternalError: java.awt.geom.NoninvertibleTransformException: Determinant is 0


I have set up a zoom function in processing and get the following error when decreasing zoom:

InternalError: java.awt.geom.NoninvertibleTransformException: Determinant is 0

The code works fine in other uses, but fail in my current code:

int zoom = 0;
void draw() {
  // background color
  background(#F0F8FF);

  // plot area  
  fill(#FFFFFF);
  rectMode(CORNERS);
  noStroke();
  rect(plotX1, plotY1, plotX2, plotY2);

//allows zoom
  scale(zoom);
  drawTitleTabs();
  drawAxisLabels();
  drawVolumeLabels();

  // data area color
  fill(#009900);
  drawDataArea(currentColumn);

  drawXTickMarks();
  // rollover color
  stroke(#5679C1);
  noFill();
  strokeWeight(2);
  drawDataHighlight(currentColumn);

  // legend
  textSize(16);
  fill(#000000);
  textAlign(LEFT);
  text("1. Press spacebar to\ntoggle gridlines.\n2. Click on tabs to\nview regions.\n3. Hover over top of\ncolumn to see\naverage temperature.", 1315, 200);
}
 //add zoom using up and down arrow
  if (key == CODED) {
    if (keyCode == UP) {
      zoom+= 0.3;
    } else if (keyCode == DOWN) {
      zoom-= 0.3;
    }
  }

This is just subsection of code, I can post the rest if needed.


Solution

  • The error is cause by scale(zoom), when zoom is 0.0. The matrix operations like scale, rotate and translate define a new matrix and multiply the current matrix by the new matrix. If zoom is 0.0, the the resulting scaling matrix is a matrix where all fields are 0.0, too. This would causes undefined behavior, because of that the error is generated.

    You can prevent the error, by evaluating if zoom > 0.0:

    if (zoom > 0.0) {
        scale(zoom);
    }
    

    or by limiting zoom. Compute the maximum (max) of a small positive value and zoom:

    scale(max(0.01, zoom));
    

    Furthermore zoom has to be a floating point variable:

    int zoom = 0;

    float zoom = 0;