0

I am currently developing a game in which the computer makes a path along points in an arraylist according to the travelling salesman problem algorithm. With each iteration I need the previous path to reset. At the moment each new path generated by the iterations is drawing on top of the previous so it all looks really messy. The function path.reset() doesn't seem to work as it should according to the android docs. This is my code, could anyone point out where I'm going wrong??

//this class draws a line 
public void CompDrawLine(List test) {
    // int d = 0; 
    int i=0;
    test.add(test.get(0));
    Point c = test.get(i);

    for (i=0;i<(test.size()-1);i++) {
        cPath.moveTo(c.x,c.y);
        c = test.get(i+1);
        cPath.lineTo(c.x,c.y);
        mCanvas.drawPath(cPath,cPaint); 

        cPath.reset(); 
    } 

    // cPath.reset(); 
    invalidate(); 
}

1 Answers1

0

When calling moveTo in loop, its effect is same to calling reset(). moveTo() sets the beginning of the next contour, but actually you just need to draw one contour. I changed your code, and you can see the difference:

        int i=0;
        test.add(test.get(0));
        Point c = test.get(i);
        path.moveTo(c.x,c.y); // move this line out of the loop
        for (i=0;i<(test.size()-1);i++) {

            c = test.get(i+1);
            path.lineTo(c.x,c.y);
            canvas.drawPath(path,paint); 

//          path.reset(); // comment out reset()
        } 

enter image description here

        int i=0;
        test.add(test.get(0));
        Point c = test.get(i);
        path.moveTo(c.x,c.y);
        for (i=0;i<(test.size()-1);i++) {

            c = test.get(i+1);
            path.lineTo(c.x,c.y);
            canvas.drawPath(path,paint); 

            path.reset(); // uncomment reset()
        } 

enter image description here

yushulx
  • 11,695
  • 8
  • 37
  • 64
  • @AmnaHussain I think I see what you mean. You said the reset() function cannot work, but actually It can. You didn't use it correctly. I just want to explain how reset() works. I guess you want to connect all points with lines. If so, you should use drawLine, not drawPath. – yushulx Mar 12 '15 at 02:18