Subversion Repositories display

Rev

Rev 432 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright (C) 2014 Steve Ratcliffe
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 or
 * version 2 as published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 */

package test.svg;

import uk.me.parabola.imgfmt.Utils;

/**
 * Holds a coordinate for display in an SVG document.
 *
 * @author Steve Ratcliffe
 */

public class XY {
        private static int scale = 10;

        // These are used so that the numbers are not too big. We just save the first values we
        // see and subtract them from each x and y coord.
        private static int latOrigin;
        private static int lonOrigin;

        private final int lat;
        private final int lon;

        XY(int lat, int lon) {
                this.lat = lat;
                this.lon = lon;

                if (latOrigin == 0 && lonOrigin == 0) {
                        latOrigin = lat;
                        lonOrigin = lon;
                }
        }

        public final int getYi() {
                return (latOrigin - lat) / scale;
        }

        public String getY() {
                return String.valueOf(getYi());
        }

        public String getY(int off) {
                return String.valueOf(getYi() + off);
        }

        public final int getXi() {
                return (lon - lonOrigin) / scale;
        }

        public String getX() {
                return String.valueOf(getXi());
        }

        public String getX(int off) {
                return String.valueOf(getXi() + off);
        }

        public double getLatitude() {
                return Utils.toDegrees(lat);
        }

        public double getLongitude() {
                return Utils.toDegrees(lon);
        }

        public String toString() {
                return String.format("(%.4f,%.4f)", getLatitude(), getLongitude());
        }

        public XY offset(XY xy2, int fraction) {
                int dx = xy2.getXi() - getXi();
                int dy = xy2.getYi() - getYi();

                XY p = new XY(lat - dy/fraction, lon + dx/fraction);
                return p;
        }

        /**
         * Set the scale factor that the output co-ordinates are scaled by.
         *
         * This is used to prevent very large x&y coords that might cause problems with inkscape.
         * This is a static setting, so it is only useful for single shot programs and it should
         * be set once at the beginning of the program and left alone after that.
         *
         * @param n Output coordinates will be divided by this number.
         */

        public static void setScale(int n) {
                scale = n;
        }
}