Croping out points from an array list in Java -


crop: has parameters 2 points in cloud. 1 of these 2 points bottom corner, , other diagonally across top corner of rectangle. crop remove points outside rectangle cloud crop image. crop method must deal 2 input points on horizontal or vertical line segment, in case points not on line segment removed, , must deal 2 equal points p1 , p2, in case points p1 removed cloud.

for example, if 2 input points (0.0,0,0) , (1.0,1.0), points outside square delimited (0.0,0.0), (0.0,1.0), (1.0,1.0), , (0.0,1.0) removed, if 2 input points (0.0,0,0) , (0.0,1.0), points outside line segment delimited (0.0,0.0), , (0.0,1.0) removed.

i'm having hard time approaching logically. if me out thankful.

public void crop(point p1, point p2) {     point left = points.get(0);     point right = points.get(1);     point top = points.get(2);     point bottom = points.get(3);     // point []rectangle2d = {p1,p2};     rectangle2d rect = new rectangle2d.double(p1.getx(), p1.gety(),             p2.getx(), p2.gety());     if (p1.getx() == p2.getx() || p1.gety() == p2.gety()) {         points.add(p1); // checks if equal     }      // checks if in square;     if (left.getx() > p1.getx() && left.gety() > p1.gety()             && left.getx() < p2.getx() && left.gety() < p2.gety()) {         points.add(left);     } else {         points.remove(left);     }     if (right.getx() > p1.getx() && right.gety() > p1.gety()             && right.getx() < p2.getx() && right.gety() < p2.gety()) {         points.add(right);     } else {         points.remove(right);     }     if (top.getx() > p1.getx() && top.gety() > p1.gety()             && top.getx() < p2.getx() && top.gety() < p2.gety()) {         points.add(top);     } else {         points.remove(top);     }     if (bottom.getx() > p1.getx() && bottom.gety() > p1.gety()             && bottom.getx() < p2.getx() && bottom.gety() < p2.gety()) {         points.add(bottom);     } else {         points.remove(bottom);     }      // checks line coordanites      if (p1.getx() == top.getx() && p1.gety() < top.gety()             && top.gety() < bottom.gety() || p1.getx() < top.getx()             && top.getx() < p2.getx() && p1.gety() == top.gety()) {         points.add(top);     }     if (p1.getx() == right.getx() && p1.gety() < right.gety()             && left.gety() < right.gety() || p1.getx() < right.getx()             && right.getx() < p2.getx() && p1.gety() == right.gety()) {         points.add(right);     }     if (p1.getx() == top.getx() && p1.gety() < top.gety()             && left.gety() < top.gety() || p1.getx() < top.getx()             && top.getx() < p2.getx() && p1.gety() == top.gety()) {         points.add(top);     }     if (p1.getx() == bottom.getx() && p1.gety() < bottom.gety()             && left.gety() < bottom.gety() || p1.getx() < bottom.getx()             && bottom.getx() < p2.getx() && p1.gety() == bottom.gety()) {         points.add(bottom);     }  } 

or

point left = points.get(0);     point right = points.get(1);     point top = points.get(2);     point bottom = points.get(3);     // point []rectangle2d = {p1,p2};     if(left.getx() > p1.getx()){         points.remove(left);     }     if(left.gety() > p1.gety()){         points.remove(left);     }     if(left.getx() < p1.getx()){         points.remove(left);     }     if(left.gety() < p1.gety()){         points.remove(left);      }             //cehcks right              if(right.getx() > p1.getx()){                     points.remove(right);                     }             if(right.gety() > p1.gety()){                     points.remove(right);                     }             if(right.getx() < p1.getx()){                     points.remove(right);                     }             if(right.gety() < p1.gety()){                     points.remove(right);                     }             //checks top                      if(top.getx() > p1.getx()){                         points.remove(top);                         }                     if(top.gety() > p1.gety()){                         points.remove(top);                         }                     if(top.getx() < p1.getx()){                         points.remove(top);                         }                     if(top.gety() < p1.gety()){                         points.remove(top);                         }                              //checks bottom                             if(bottom.getx() > p1.getx()){                                 points.remove(bottom);                                 }                             if(bottom.gety() > p1.gety()){                                 points.remove(bottom);                                 }                             if(bottom.getx() < p1.getx()){                                 points.remove(bottom);                                 }                             if(bottom.gety() < p1.gety()){                                 points.remove(bottom);                                 }             //checking lines             if(p1.getx() == left.getx() && p1.gety() < left.gety() && left.gety() < p2.gety() ||                 p1.getx()  < left.getx() && left.getx() < p2.getx() && p1.gety() == left.gety()){                 points.add(left);             }              if(p1.getx() == right.getx() && p1.gety() < right.gety() && right.gety() < p2.gety() ||                     p1.getx()  < right.getx() && right.getx() < p2.getx() && p1.gety() == right.gety()){                     points.add(right);                 }             if(p1.getx() == top.getx() && p1.gety() < top.gety() && top.gety() < p2.gety() ||                     p1.getx()  < top.getx() && top.getx() < top.getx() && p1.gety() == top.gety()){                     points.add(top);                 }             if(p1.getx() == bottom.getx() && p1.gety() < bottom.gety() && bottom.gety() < p2.gety() ||                     p1.getx()  < bottom.getx() && bottom.getx() < bottom.getx() && p1.gety() == bottom.gety()){                     points.add(bottom);                 }              if(p1.getx() == p2.getx() || p1.gety() == p2.gety()){                 points.add(p2);             } 

thanks

try using geometric logic, more understanding try on paper using quadrants , sample points.

assume if parameter (x1,y1), , (x2,y2)

to check point (a,b) whether inside square, condition (a>x1 && b>y1) , (a<x2 && b<y2). if satisfies condition inside square else not.

assume if parameter (x1,y1), , (x2,y2) [and x1==x2 or y1==y2]

to check point (a,b) whether within line, condition (x1==a && y1<b<y2) or (x1<a<x2 && y1==b)

it worked me, find whether point within triangle of given vertices.

hope helpful


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -