001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.layer.imagery;
003
004import java.util.function.Function;
005import java.util.stream.IntStream;
006import java.util.stream.Stream;
007
008import org.openstreetmap.gui.jmapviewer.TileXY;
009
010/**
011 * This is a rectangular range of tiles.
012 */
013public class TileRange {
014    protected int minX;
015    protected int maxX;
016    protected int minY;
017    protected int maxY;
018    protected int zoom;
019
020    protected TileRange() {
021    }
022
023    protected TileRange(TileXY t1, TileXY t2, int zoom) {
024        minX = (int) Math.floor(Math.min(t1.getX(), t2.getX()));
025        minY = (int) Math.floor(Math.min(t1.getY(), t2.getY()));
026        maxX = (int) Math.ceil(Math.max(t1.getX(), t2.getX()));
027        maxY = (int) Math.ceil(Math.max(t1.getY(), t2.getY()));
028        this.zoom = zoom;
029    }
030
031    protected double tilesSpanned() {
032        return Math.sqrt(1.0 * this.size());
033    }
034
035    /**
036     * Returns size
037     * @return size
038     */
039    public int size() {
040        int xSpan = maxX - minX + 1;
041        int ySpan = maxY - minY + 1;
042        return xSpan * ySpan;
043    }
044
045    /**
046     * Gets a stream of all tile positions in this set
047     * @return A stream of all positions
048     */
049    public Stream<TilePosition> tilePositions() {
050        if (zoom == 0) {
051            return Stream.empty();
052        } else {
053            return IntStream.rangeClosed(minX, maxX).mapToObj(
054                    x -> IntStream.rangeClosed(minY, maxY).mapToObj(y -> new TilePosition(x, y, zoom))
055                    ).flatMap(Function.identity());
056        }
057    }
058}