view gnv-artifacts/src/main/java/de/intevation/gnv/math/Point2d.java @ 507:45be952a3215

Solved some issues. Removed encoding problems while formatting coordinates (issue137) and use this format as subtitle in charts (issue136). gnv-artifacts/trunk@590 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 21 Jan 2010 14:42:51 +0000
parents 422275fc9927
children b248531fa20b
line wrap: on
line source
package de.intevation.gnv.math;

import java.util.Comparator;

import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Coordinate;

/**
 *  @author Sascha L. Teichmann
 */
public class Point2d
extends      Coordinate
{
    public static final double EPSILON = 1e-3d;

    public static final Comparator X_COMPARATOR = new Comparator() {
        public int compare(Object a, Object b) {
            double xa = ((Coordinate)a).x;
            double xb = ((Coordinate)b).x;
            if (xa < xb) return -1;
            if (xa > xb) return +1;
            return 0;
        }
    };

    public static final Comparator Y_COMPARATOR = new Comparator() {
        public int compare(Object a, Object b) {
            double ya = ((Coordinate)a).y;
            double yb = ((Coordinate)b).y;
            if (ya < yb) return -1;
            if (ya > yb) return +1;
            return 0;
        }
    };

    public static class InverseL1Comparator
    implements          Comparator
    {
        private Point2d ref;

        public InverseL1Comparator(Point2d ref) {
            this.ref = ref;
        }

        public int compare(Object a, Object b) {
            Point2d pa = (Point2d)a;
            Point2d pb = (Point2d)b;
            double da = ref.L1(pa);
            double db = ref.L1(pb);
            if (da < db) return -1;
            if (da > db) return +1;
            return 0;
        }
    } // class InverseL1Comparator

    public int i;
    public int j;

    public Point2d() {
    }

    public Point2d(double x, double y, int i, int j) {
        super(x, y);
        this.i = i;
        this.j = j;
    }

    public Point2d(double x, double y, double z, int i, int j) {
        super(x, y, z);
        this.i = i;
        this.j = j;
    }


    public double L1(Point2d other) {
        return L1(this, other);
    }

    public static double L1(Coordinate a, Coordinate b) {
        return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
    }

    public Envelope envelope() {
        return envelope(EPSILON);
    }

    public Envelope envelope(double epsilon) {
        return new Envelope(
            x-epsilon, x+epsilon,
            y-epsilon, y+epsilon);
    }

    public boolean hasIGap(Point2d other) {
        return Math.abs(i - other.i) > 1;
    }

    public boolean hasJGap(Point2d other) {
        return Math.abs(j - other.j) > 1;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org