java - Trouble with intersection between line segments -
i have path stored points in arraylist , want check if line segments intersecting. unknown reason it's not working! don't message in logcat despite i'm drawing shape intersects. preciate if see have done wrong or have suggestions how improve code.
// intersection control if(touchactionup) { // loop throw points except last 3 (int = 0; < points.size()-3; i++) { line line1 = new line(points.get(i), points.get(i+1)); // begin after line above , check points after (int j = + 2; j < points.size()-1; j++) { line line2 = new line(points.get(j), points.get(j+1)); // call method check intersection if(checkintersection(line1, line2)) { log.i("intersection", "yes!"); } } } }
and method:
// method check intersection between lines private boolean interceptioncontrol(line line1, line line2) { int x1 = line1.pointx1; int x2 = line1.pointx2; int x3 = line2.pointx1; int x4 = line2.pointx2; int y1 = line1.pointy1; int y2 = line1.pointy2; int y3 = line2.pointy1; int y4 = line2.pointy2; // check if lines parallel int denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1); if(denom == 0) { // lines parallel // ?? } double = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denom; double b = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denom; // check intersection if( >= 0.0f && <= 1.0f && b >= 0.0f && b <= 1.0f) { return true; } return false; }
you using int
coordinates , integer division (i.e., 3/2 = 1). might reason when dividing denom
. can fix dividing ((double)denom)
instead of denom
.
Comments
Post a Comment