Java calculate all points between two coordinates, and padded to ensure that BFS can search for points

These days work encountered a problem.
Dividing a region do, two points together into a connection, the need for calculating the area of each region is divided into the two regions.
Operators need area BFS eight directions, it is necessary to ensure that even the line must be closed, and after the formed spot area, eight directions are found not to other regions.
So this time line needs to become more "thick" a little, because previous practice is certainly a slash diagonally upward from the past.

 public ArrayList<Integer> getLineValue(Point p1, Point p2) {
        ArrayList<Point> splitLine = new ArrayList<>();
        int dx = p2.x - p1.x;
        int dy = p2.y - p1.y;
        int absX = Math.abs(dx);
        int absY = Math.abs(dy);
        Point preP = p1;

        splitLine.add(p1);
        if (absX > absY) {
            for (int i = 0; i <= absX; i++) {
                Point newP = new Point();
                newP.x = p1.x + Math.round((float) dx * i / (float) absX);
                newP.y = p1.y + Math.round((float) dy * i / (float) absX);
                if (preP.y < newP.y) {
                    for (int j = preP.y; j <= newP.y; j++) {
                        Point crossP = new Point();
                        crossP.x = preP.x;
                        crossP.y = j;
                        splitLine.add(crossP);
                    }
                } else if (preP.y > newP.y) {
                    for (int j = preP.y; j >= newP.y; j--) {
                        Point crossP = new Point();
                        crossP.x = preP.x;
                        crossP.y = j;
                        splitLine.add(crossP);
                    }
                }
                splitLine.add(newP);
                preP = newP;
            }
            splitLine.add(p2);
        } else {
            for (int i = 0; i <= absY; i++) {
                Point newP = new Point();
                newP.x = p1.x + Math.round((float) dx * i / (float) absY);
                newP.y = p1.y + Math.round((float) dy * i / (float) absY);
                if (preP.x < newP.x) {
                    for (int j = preP.x; j <= newP.x; j++) {
                        Point crossP = new Point();
                        crossP.y = preP.y;
                        crossP.x = j;
                        splitLine.add(crossP);
                    }
                } else if (preP.x > newP.x) {
                    for (int j = preP.x; j >= newP.x; j--) {
                        Point crossP = new Point();
                        crossP.y = preP.y;
                        crossP.x = j;
                        splitLine.add(crossP);
                    }
                }
                splitLine.add(newP);
                preP = newP;
            }
            splitLine.add(p2);
        }
        Set<Point> sortSet = new TreeSet<>(new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                //x的升序排列
                if (o1.x == o2.x) {
                    return o1.y - o2.y;
                } else {
                    return o1.x - o2.x;
                }
            }
        });
        //去重,并且按排序
        Set<Point> set = new HashSet<>(splitLine);
        sortSet.addAll(set);
        splitLine= new ArrayList<>(sortSet);
        return splitLine;
    }
Published 248 original articles · won praise 99 · Views 100,000 +

Guess you like

Origin blog.csdn.net/rikkatheworld/article/details/103869751