001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data;
003
004import org.openstreetmap.josm.data.coor.EastNorth;
005
006/**
007 * This is a simple data class for "rectangular" areas of the world, given in
008 * east/north min/max values.
009 *
010 * @author imi
011 */
012public class ProjectionBounds {
013    /**
014     * The minimum and maximum coordinates.
015     */
016    public double minEast, minNorth, maxEast, maxNorth;
017
018    /**
019     * Construct bounds out of two points.
020     */
021    public ProjectionBounds(EastNorth min, EastNorth max) {
022        this.minEast = min.east();
023        this.minNorth = min.north();
024        this.maxEast = max.east();
025        this.maxNorth = max.north();
026    }
027    public ProjectionBounds(EastNorth p) {
028        this.minEast = this.maxEast = p.east();
029        this.minNorth = this.maxNorth = p.north();
030    }
031    public ProjectionBounds(EastNorth center, double east, double north) {
032        this.minEast = center.east()-east/2.0;
033        this.minNorth = center.north()-north/2.0;
034        this.maxEast = center.east()+east/2.0;
035        this.maxNorth = center.north()+north/2.0;
036    }
037    public ProjectionBounds(double minEast, double minNorth, double maxEast, double maxNorth) {
038        this.minEast = minEast;
039        this.minNorth = minNorth;
040        this.maxEast = maxEast;
041        this.maxNorth = maxNorth;
042    }
043    public void extend(EastNorth e)
044    {
045        if (e.east() < minEast) {
046            minEast = e.east();
047        }
048        if (e.east() > maxEast) {
049            maxEast = e.east();
050        }
051        if (e.north() < minNorth) {
052            minNorth = e.north();
053        }
054        if (e.north() > maxNorth) {
055            maxNorth = e.north();
056        }
057    }
058    public EastNorth getCenter()
059    {
060        return new EastNorth((minEast + maxEast) / 2.0, (minNorth + maxNorth) / 2.0);
061    }
062
063    @Override public String toString() {
064        return "ProjectionBounds["+minEast+","+minNorth+","+maxEast+","+maxNorth+"]";
065    }
066
067    /**
068     * The two bounds intersect? Compared to java Shape.intersects, if does not use
069     * the interior but the closure. (">=" instead of ">")
070     */
071    public boolean intersects(ProjectionBounds b) {
072        return b.maxEast >= minEast &&
073        b.maxNorth >= minNorth &&
074        b.minEast <= maxEast &&
075        b.minNorth <= maxNorth;
076    }
077
078    public EastNorth getMin() {
079        return new EastNorth(minEast, minNorth);
080    }
081
082    public EastNorth getMax() {
083        return new EastNorth(maxEast, maxNorth);
084    }
085
086}