我想从云中删除矩形之外的点,但我似乎无法做到这一点。 我也尝试实现extremes(),但也可以做到这一点。我不知道我做错了什么。
对于extremes():如果Cloud为空,则返回null,并且x和y坐标的数组加倍:否则为最左边,最右边,顶部,底部。例如,如果云包含[(0.0,0.0),(1.0,1.0),(2.0,0.0),(1.0,-1.0)],则结果数组为[0.0,2.0,1.0,-1.0]。
这是极端的代码:
public double[] extremes(){
if (isEmpty()){
return null;
}
double left = 0;
double right = 0;
double top = 0;
double bottom = 0;
double [] extremes = new double[4];
extremes[0] = left;
extremes[1] = right;
extremes[2] = top;
extremes[3] = bottom;
return extremes;
}
对于裁剪:裁剪的参数是云中的两个点。这两个点中的一个是底角,另一个是矩形的顶角。顶角从底角斜对角。裁剪将从云中移除此矩形外的所有点,就像裁剪图像一样。裁剪方法必须处理水平或垂直线段上的两个输入点,在这种情况下,不会删除线段上的所有点,并且它必须处理两个相等的点p1和p2,在这种情况下所有点都是p1(如果云中存在,则从云中删除。
例如,如果两个输入点是(0.0,0,0)和(1.0,1.0),则方形外的所有点由(0.0,0.0),(0.0,1.0),(1.0,1.0)分隔,和(0.0,1.0)被删除,但如果两个输入点是(0.0,0,0)和(0.0,1.0),则线段外的所有点由(0.0,0.0)和(0.0,1.0)分隔)被删除。
以下是crop的代码:
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);
// check if in square
if (left.getX() > p1.getX() || left.getX() < p1.getX() ||
left.getY() > p1.getY() || left.getY() < p1.getY()){
points.add(left);
}else
points.remove(left);
if (right.getX() > p1.getX() || right.getX() < p1.getX() ||
right.getY() > p1.getY() || right.getY() < p1.getY()){
points.add(right);
}else
points.remove(right);
if (top.getX() > p2.getX() || top.getX() < p2.getX() ||
top.getY() > p2.getY() || top.getY() < p2.getY()){
points.add(top);
}else
points.remove(top);
if (bottom.getX() > p2.getX() || bottom.getX() < p2.getX() ||
bottom.getY() > p2.getY() || bottom.getY() < p2.getY()){
points.add(bottom);
}else
points.remove(bottom);
// check coordinate
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);
}
}
此课程的其他代码:
private ArrayList<Point> points = new ArrayList<Point>();;
public boolean isEmpty(){
return size() == 0;
}
public int size(){
return 0;
}
public boolean hasPoint(Point p){
return points.contains(p);
}
public void addPoint(Point p){
// Don't do anything if p in list
if(points.contains(p)){
}else{
points.add(p);
}
}
编辑!