0

I am trying to create a graphing calculator applet using Java. I am having trouble plotting graphs that contain discontinuities such as holes and asymptotes. Here is the code I have for graphs with asymptotes so far:

for (int i = 1; i < size; i++) { 
    x1 = round((startX + ((graphdata.get(i - 1)).getX()) * scale)); 
    y1 = round((startY - ((graphdata.get(i - 1)).getY()) * scale)); 
    x2 = round((startX + ((graphdata.get(i)).getX()) * scale)); 
    y2 = round((startY - ((graphdata.get(i)).getY()) * scale)); 
    for (int j = 0; j < VertAsympArray.size(); j++) 
        { 
            if(Math.abs((round(graphdata.get(i).getX(),3)) - (VertAsympArray.get(j))) > epsilon && Math.abs((round(graphdata.get(i-1).getX(),3)) - (VertAsympArray.get(j))) > epsilon ){ 
                System.out.println("Drawing points : x1 : " + round(graphdata.get(i-1).getX(),3) + ", y1 :" + round(graphdata.get(i-1).getY(),3) + "; x2 : " + round(graphdata.get(i).getX(),3) + ", y2 : " + round(graphdata.get(i).getY(),3) ); 
                g.drawLine(x1, y1, x2, y2); 
            } 
            else 
                System.out.println("VertAsympArray : " + VertAsympArray.get(j) + "; Function x value : " + round(graphdata.get(i).getX(),3)); 
        }  
}

Even though I removed the values that make the function the user inputs undefined, for some reason a straight line is drawn connecting the last lowest point on the graph to the next highest point. Here is how a graph looks like for ((x-1)(x+3))/((x+1)(x-6)) around the x = 6 asymptote:

The straight red line is not any asymptote I drew in, it is showing up as part of the function, which is wrong. How do I get rid of it/is there a better way to graph asymptotes?

adrianbanks
  • 81,306
  • 22
  • 176
  • 206
nithinaray
  • 11
  • 3
  • What value does `epsilon` have here, and why are you rounding the `x` values before checking their proximity to the asymptotes? – Dawood ibn Kareem Jan 20 '15 at 06:15
  • double epsilon = 0.01d; the purpose of this was to compare the values that I found to be the asymptotes to the x values stored in the array from the function. I am rounding the x values because the parser, jeval, changes the longer numbers to scientific notation. – nithinaray Jan 20 '15 at 06:29
  • But rounding to 3dp shouldn't affect a comparison against 0.01. – Dawood ibn Kareem Jan 20 '15 at 06:31
  • But even if I round the numbers, it doesn't affect it. The graph still has those lines... is there something wrong with the logic? – nithinaray Jan 20 '15 at 06:42
  • I can't see anything wrong at a quick glance. If I were in your shoes, I'd be stepping through it with a debugger to see exactly what's going on. – Dawood ibn Kareem Jan 20 '15 at 06:44

0 Answers0