java - Error when iterating arraylist with points of lines to check for intersection -


i'm developing code check intersection between lines. have points of lines stored in arraylist , below have code iterati throw arraylist compare 2 lines @ time. error message line: line line2 = new line(points.get(j), points.get(j+1)); it's j+1 causes error. if have j it's no problem, don't next point!? can't or have been thinking wrong in way? preciated locate error or suggestion alternative solution.

    // intersection control     if(touchactionup) {          (int = 0; < points.size()-3; i++) {             line line1 = new line(points.get(i), points.get(i+1));              (int j = + 2; j < points.size(); j++) {                 line line2 = new line(points.get(j), points.get(j+1));                  // call method check intersection                 if(checkintersection(line1, line2)) {                  }             }         }     } 

yes, problem:

for (int j = + 2; j < points.size(); j++) {     line line2 = new line(points.get(j), points.get(j+1)); 

that keep incrementing until and including j = points.size() - 1 @ point calling points.get(j+1) calling points.get(points.size()) invalid index.

you want smaller lower bound:

for (int j = + 2; j < points.size() - 1; j++) { 

it looks me you're not using list of points correctly though - if you're trying use pairs of points in list, i++ , j++ should i += 2 , j += 2 - otherwise you're going create lines points indexes of (0, 1), (1, 2), (2, 3) etc. maybe that's intention - if these meant lines describing single shape, example... if it's not, i'd encourage create list<line> instead, each line has pair of points. simpler work with.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -