Ignore:
Timestamp:
2016-08-17T20:14:58+02:00 (8 years ago)
Author:
Don-vip
Message:

fix #13306 - Make map paint code use double coordinates (patch by michael2402) - gsoc-core

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/MapViewState.java

    r10806 r10827  
    44import java.awt.Container;
    55import java.awt.Point;
    6 import java.awt.Rectangle;
    76import java.awt.geom.AffineTransform;
    87import java.awt.geom.Area;
     
    1918import org.openstreetmap.josm.data.coor.EastNorth;
    2019import org.openstreetmap.josm.data.coor.LatLon;
     20import org.openstreetmap.josm.data.osm.Node;
    2121import org.openstreetmap.josm.data.projection.Projecting;
    2222import org.openstreetmap.josm.data.projection.Projection;
     
    3030 */
    3131public final class MapViewState {
     32
     33    /**
     34     * A flag indicating that the point is outside to the top of the map view.
     35     * @since 10826
     36     */
     37    public static final int OUTSIDE_TOP = 1;
     38
     39    /**
     40     * A flag indicating that the point is outside to the bottom of the map view.
     41     * @since 10826
     42     */
     43    public static final int OUTSIDE_BOTTOM = 2;
     44
     45    /**
     46     * A flag indicating that the point is outside to the left of the map view.
     47     * @since 10826
     48     */
     49    public static final int OUTSIDE_LEFT = 3;
     50
     51    /**
     52     * A flag indicating that the point is outside to the right of the map view.
     53     * @since 10826
     54     */
     55    public static final int OUTSIDE_RIGHT = 4;
    3256
    3357    private final Projecting projecting;
     
    158182
    159183    /**
     184     * Gets the {@link MapViewPoint} for the given node. This is faster than {@link #getPointFor(LatLon)} because it uses the node east/north
     185     * cache.
     186     * @param node The node
     187     * @return The position of that node.
     188     * @since 10826
     189     */
     190    public MapViewPoint getPointFor(Node node) {
     191        return getPointFor(node.getEastNorth(getProjection()));
     192    }
     193
     194    /**
    160195     * Gets a rectangle representing the whole view area.
    161196     * @return The rectangle.
     
    169204     * @param rectangle The rectangle to get.
    170205     * @return The view area.
    171      * @since 10458
    172      */
    173     public MapViewRectangle getViewArea(Rectangle rectangle) {
     206     * @since 10826
     207     */
     208    public MapViewRectangle getViewArea(Rectangle2D rectangle) {
    174209        return getForView(rectangle.getMinX(), rectangle.getMinY()).rectTo(getForView(rectangle.getMaxX(), rectangle.getMaxY()));
    175210    }
     
    331366        }
    332367
    333         protected abstract double getInViewX();
    334 
    335         protected abstract double getInViewY();
     368        /**
     369         * Get the x coordinate in view space without creating an intermediate object.
     370         * @return The x coordinate
     371         * @since 10826
     372         */
     373        public abstract double getInViewX();
     374
     375        /**
     376         * Get the y coordinate in view space without creating an intermediate object.
     377         * @return The y coordinate
     378         * @since 10826
     379         */
     380        public abstract double getInViewY();
    336381
    337382        /**
     
    399444            return new MapViewEastNorthPoint(getEastNorth().add(en));
    400445        }
     446
     447        /**
     448         * Check if this point is inside the view bounds.
     449         *
     450         * This is the case iff <code>getOutsideRectangleFlags(getViewArea())</code> returns no flags
     451         * @return true if it is.
     452         * @since 10826
     453         */
     454        public boolean isInView() {
     455            return inRange(getInViewX(), 0, getViewWidth()) && inRange(getInViewY(), 0, getViewHeight());
     456        }
     457
     458        private boolean inRange(double val, int min, double max) {
     459            return val >= min && val < max;
     460        }
     461
     462        /**
     463         * Gets the direction in which this point is outside of the given view rectangle.
     464         * @param rect The rectangle to check agains.
     465         * @return The direction in which it is outside of the view, as OUTSIDE_... flags.
     466         * @since 10826
     467         */
     468        public int getOutsideRectangleFlags(MapViewRectangle rect) {
     469            Rectangle2D bounds = rect.getInView();
     470            int flags = 0;
     471            if (getInViewX() < bounds.getMinX()) {
     472                flags |= OUTSIDE_LEFT;
     473            } else if (getInViewX() > bounds.getMaxX()) {
     474                flags |= OUTSIDE_RIGHT;
     475            }
     476            if (getInViewY() < bounds.getMinY()) {
     477                flags |= OUTSIDE_TOP;
     478            } else if (getInViewY() > bounds.getMaxY()) {
     479                flags |= OUTSIDE_BOTTOM;
     480            }
     481
     482            return flags;
     483        }
     484
     485        /**
     486         * Gets the sum of the x/y view distances between the points. |x1 - x2| + |y1 - y2|
     487         * @param p2 The other point
     488         * @return The norm
     489         * @since 10826
     490         */
     491        public double oneNormInView(MapViewPoint p2) {
     492            return Math.abs(getInViewX() - p2.getInViewX()) + Math.abs(getInViewY()) - p2.getInViewY();
     493        }
     494
     495        /**
     496         * Gets the squared distance between this point and an other point.
     497         * @param p2 The other point
     498         * @return The squared distance.
     499         * @since 10826
     500         */
     501        public double distanceToInViewSq(MapViewPoint p2) {
     502            double dx = getInViewX() - p2.getInViewX();
     503            double dy = getInViewY() - p2.getInViewY();
     504            return dx * dx + dy * dy;
     505        }
     506
     507        /**
     508         * Gets the distance between this point and an other point.
     509         * @param p2 The other point
     510         * @return The distance.
     511         * @since 10826
     512         */
     513        public double distanceToInView(MapViewPoint p2) {
     514            return Math.sqrt(distanceToInViewSq(p2));
     515        }
    401516    }
    402517
     
    411526
    412527        @Override
    413         protected double getInViewX() {
     528        public double getInViewX() {
    414529            return x;
    415530        }
    416531
    417532        @Override
    418         protected double getInViewY() {
     533        public double getInViewY() {
    419534            return y;
    420535        }
     
    435550
    436551        @Override
    437         protected double getInViewX() {
     552        public double getInViewX() {
    438553            return (eastNorth.east() - topLeft.east()) / scale;
    439554        }
    440555
    441556        @Override
    442         protected double getInViewY() {
     557        public double getInViewY() {
    443558            return (topLeft.north() - eastNorth.north()) / scale;
    444559        }
     
    517632            return new Rectangle2D.Double(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
    518633        }
     634
     635        /**
     636         * Check if the rectangle intersects the map view area.
     637         * @return <code>true</code> if it intersects.
     638         * @since 10826
     639         */
     640        public boolean isInView() {
     641            return getInView().intersects(getViewArea().getInView());
     642        }
    519643    }
    520644
Note: See TracChangeset for help on using the changeset viewer.