Java Rectangle mergeRects(Set prev, Set current)

Here you can find the source of mergeRects(Set prev, Set current)

Description

merge Rects

License

Open Source License

Declaration

private static Set<Rectangle> mergeRects(Set<Rectangle> prev,
            Set<Rectangle> current) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2007 Olivier Chafik, All Rights Reserved
 * Copyright (c) 2008 Timothy Wall, All Rights Reserved
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *///from  w w  w.j  a  v a 2s. co m

import java.awt.Rectangle;

import java.util.HashSet;

import java.util.Set;

public class Main {
    private static Set<Rectangle> mergeRects(Set<Rectangle> prev,
            Set<Rectangle> current) {
        Set<Rectangle> unmerged = new HashSet<Rectangle>(prev);
        if (!prev.isEmpty() && !current.isEmpty()) {
            Rectangle[] pr = prev.toArray(new Rectangle[prev.size()]);
            Rectangle[] cr = current.toArray(new Rectangle[current.size()]);
            int ipr = 0;
            int icr = 0;
            while (ipr < pr.length && icr < cr.length) {
                while (cr[icr].x < pr[ipr].x) {
                    if (++icr == cr.length) {
                        return unmerged;
                    }
                }
                if (cr[icr].x == pr[ipr].x
                        && cr[icr].width == pr[ipr].width) {
                    unmerged.remove(pr[ipr]);
                    cr[icr].y = pr[ipr].y;
                    cr[icr].height = pr[ipr].height + 1;
                    ++icr;
                } else {
                    ++ipr;
                }
            }
        }
        return unmerged;
    }
}

Related

  1. getRectangleProperty(Map p, Object key, Rectangle defaultValue)
  2. getRectLE(byte[] data, int i)
  3. getSubArea(Shape retangulo, int divisorx, int divisory, int offsetx, int offsety)
  4. inflateRectangle(Rectangle r, int x, int y)
  5. interpolate(Rectangle r1, Rectangle r2, float f)
  6. normalizeRect(Rectangle rect)
  7. randomRectangle(int minW, int maxW, int minH, int maxH)
  8. randomSquare(int minW, int maxW)
  9. rectangleResize(Rectangle rect, int width, int height)