从Java中的数组列表中裁剪出点

时间:2014-06-27 03:53:23

标签: java arrays for-loop arraylist

crop:具有云中两点的参数。这两个点中的一个是底角,另一个是矩形的对角线。裁剪将从云中移除此矩形外的所有点,就像裁剪图像一样。裁剪方法必须处理水平或垂直线段上的两个输入点,在这种情况下,不在线段上的所有点都被移除,并且它必须处理两个相等的点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)分隔)被删除。

我很难在逻辑上接近这一点。

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 its in the 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 the line coordinates

    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);
    }

}

    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); 
    }

    //checks 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);
    }

2 个答案:

答案 0 :(得分:1)

我想出了这个示例代码,为您提供一些想法。希望我能理解你的问题。

    double[][] legalRange = {{0.0, 0.0}, {0.0, 1.0}, {1.0, 1.0}, {1.0, 0.0}};
    double[] point = { 91.0, 0.0};

    for (int i = 0; i < point.length; i++) {
        boolean resutl = (point[i] >= 0.0) && (point[i] < 1.1);
        if(!resutl) {
            System.out.println("(" + point[0] + "," + point[1] + ") not in the range");
            return; 
        }
    }
    System.out.println("(" + point[0] + "," + point[1] + ") in the range");

输出:

(91.0,0.0) not in the range

答案 1 :(得分:0)

尝试使用几何逻辑,以便更好地理解使用象限和样本点的纸张。

假设参数(x1,y1)和(x2,y2)

检查点(a,b)是否在广场内, 条件(a>x1 && b>y1) And (a<x2 && b<y2)。 如果满足条件,那么它就不在广场内。

假设参数(x1,y1)和(x2,y2)[和x1 == x2或y1 == y2]

检查点(a,b)是否在一行内, 条件(x1==a && y1<b<y2) OR (x1<a<x2 && y1==b)

它对我有用,可以找出一个点是否在给定顶点的三角形内。

希望这有用