从Java中的数组列表Croping出点 [英] Croping out points from an array list in Java

查看:91
本文介绍了从Java中的数组列表Croping出点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作物:有是在云两点参数。一个这两点是底角,和一个矩形的另一对角横过上角。作物将删除所有点从云这个矩形之外的很像你会裁剪图像。作物方法必须处理​​上的水平或垂直线段两个输入点,在这种情况下,所有点不是线段被去除,并且它必须处理两个相等点P1和P2,在这种情况下,所有点但P1是从云中删除。

crop: has parameters that are two Points in the Cloud. One of these two points is a bottom corner, and the other a diagonally across top corner of a rectangle. 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.

例如,如果两个输入点是:(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 )被除去。

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.

我有一个很难接近逻辑此。如果有人可以帮助我,我会心存感激。

I'm having a hard time approaching this logically. If anyone could help me out with that I would be 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 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 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);
    }

}

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

感谢

推荐答案

尝试使用几何逻辑,为更多的理解尝试使用象限和采样点纸。

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

假设,如果参数(X1,Y1)和(X2,Y2)

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

检查点(A,B)是否在正方形内,
条件(A&GT; X1&放大器;和B&GT; y1)和(A&LT; X2&放大器;和B&LT; Y2)
如果满足条件就没有别的

to check for point (a,b) whether inside the square, condition (a>x1 && b>y1) And (a<x2 && b<y2). If satisfies condition it is inside the Square else not.

假设,如果参数(X1,Y1)和(X2,Y2)[和X1 == X2或Y1 == Y2]

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

检查点(A,B)是否在一行内,
条件(X1 == A和&功放; Y1&LT; B&LT; Y2)OR(X1&LT; A&LT; X2&放大器;&安培; Y1 == B)

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

它的工作对我来说,要找到一个点是否给出顶点的三角形内。

it worked for me, to find whether a point is within a triangle of given vertices.

希望这是有益

这篇关于从Java中的数组列表Croping出点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆