All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.yandex.qatools.ashot.coordinates.Coords Maven / Gradle / Ivy

The newest version!
package ru.yandex.qatools.ashot.coordinates;

import com.google.gson.Gson;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Pavel Zorin
 */

public class Coords extends Rectangle {

    public static Set intersection(Collection coordsPool1, Collection coordsPool2) {
        Set intersectedCoords = new HashSet<>();
        for (Coords coords1 : coordsPool1) {
            for (Coords coords2 : coordsPool2) {
                Coords intersection = coords1.intersection(coords2);
                if (!intersection.isEmpty()) {
                    intersectedCoords.add(intersection);
                }
            }
        }
        return intersectedCoords;
    }

    public static Set setReferenceCoords(Coords reference, Set coordsSet) {
        Set referencedCoords = new HashSet<>();
        for (Coords coords : coordsSet) {
            referencedCoords.add(new Coords(
                            coords.x - reference.x,
                            coords.y - reference.y,
                            coords.width,
                            coords.height)
            );
        }
        return referencedCoords;
    }

    public static Coords unity(Collection coordsCollection) {
        Coords unity = coordsCollection.iterator().next();
        for (Coords coords : coordsCollection) {
            unity = unity.union(coords);
        }
        return unity;
    }

    public static Coords ofImage(BufferedImage image) {
        return new Coords(image.getWidth(), image.getHeight());
    }

    public Coords(Rectangle rectangle) {
        super(rectangle);
    }

    public Coords(int x, int y, int width, int height) {
        super(x, y, width, height);
    }

    public Coords(int width, int height) {
        super(width, height);
    }

    public void reduceBy(int pixels) {
        if (pixels < getWidth() / 2 && pixels < getHeight() / 2) {
            this.x += pixels;
            this.y += pixels;
            this.width -= pixels;
            this.height -= pixels;
        }
    }


    @SuppressWarnings("NullableProblems")
    @Override
    public Coords union(Rectangle r) {
        return new Coords(super.union(r));
    }

    @SuppressWarnings("NullableProblems")
    @Override
    public Coords intersection(Rectangle r) {
        return new Coords(super.intersection(r));
    }

    @Override
    public String toString() {
        return new Gson().toJson(this);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy