Your inner for loop should probably not be reseting "i", you should probably choose a different variable name such as "j".



double lastY = 0;

for(int i=0;i<numPoints;i++){
...