crop()
has parameters that are two Points
in the Cloud
. One of these two Points
is a bottom corner, and the other the top corner of a rectangle, located diagonally across from the first Point
. crop()
will remove all points outside this rectangle from the Cloud
, much like you would crop an image. The crop()
method must deal with two input points on a horizontal or vertical line segment, in which case all points not on the line segment are removed, and it must deal with two equal Points
p1
and p2
, in which case all Points
but p1
are removed from the Cloud.
For example, if the two input Points
are (0.0,0,0)
and (1.0,1.0)
, all Points outside the square delimited by (0.0,0.0)
, (0.0,1.0)
, (1.0,1.0)
, and (0.0,1.0)
are removed, but if the two input Points
are (0.0,0,0)
and (0.0,1.0)
, all Points outside the line segment delimited by (0.0,0.0)
, and (0.0,1.0)
are removed.
So if you have final cloud: [(3.0,1.0), (2.0,2.0), (1.5,1.5), (3.0,0.0)]
you should get cropped cloud: [(3.0,1.0), (3.0,0.0)]
At this point I really want to know a solution cause I'm so frustrated that I can't figure it out... So any help or suggestions about how you guys would solve it would be nice. I came pretty close, but I was just missing one main thing.
I had this:
public void crop(Point p1, Point p2) {
double highX = 0;
double lowX = 0;
double highY = 0;
double lowY = 0;
double x1 = p1.getX();
if (p1.getX() > p2.getX()) {
highX = p1.getX();
lowX = p2.getX();
} else {
highX = p2.getX();
lowX = p1.getX();
}
if (p1.getY() > p2.getY()) {
highY = p1.getY();
lowY = p2.getY();
} else {
highY = p2.getY();
lowY = p1.getY();
}
for (int i = 0; i < points.size(); i++) {
Point P = points.get(i);
if (P.getX() < lowX || P.getX() > highX || P.getY() < lowY
|| P.getY() > highY) {
points.remove(i);
}
if (P.getX() == lowX && P.getX() == highX && P.getY() == lowY
&& P.getY() == highY) {
points.remove(i);
}
}
// vertical
if (p1.getX() == p2.getX()) {
double x = p1.getX();
for (int i = 0; i < points.size(); i++) {
if (points.get(i).getX() == x
&& p1.getY() <= points.get(i).getY()
&& points.get(i).getY() >= p2.getY()) {
continue;
} else {
points.remove(i);
}
}
}
// Horizontal
if (p1.getY() == p2.getY()) {
double y = p2.getY();
for (int i = 0; i < points.size(); i++) {
if (points.get(i).getY() == y) {
if (p1.getX() <= points.get(i).getX()
&& points.get(i).getX() >= p2.getX()) {
continue;
}
} else {
points.remove(i);
}
}
}
// square
if (p1.getX() == p2.getX()) {
for (int i = 0; i < points.size(); i++) {
if (points.get(i).getY() <= p1.getY()
&& points.get(i).getY() >= p2.getY()) {
if (points.get(i).getX() >= p1.getX()
&& points.get(i).getX() <= p2.getX()) {
continue;
}
}
points.remove(i);
}
}
}