view gnv-artifacts/src/main/java/de/intevation/gnv/math/Point2d.java @ 788:e8a9e84a0328

Corrected JavaDoc errors in Chart Package. gnv-artifacts/trunk@870 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 31 Mar 2010 08:01:56 +0000
parents c4156275c1e1
children 6cff63d0c434
line wrap: on
line source
package de.intevation.gnv.math;

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

import java.util.Comparator;

import org.apache.log4j.Logger;

/**
 *  @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
 */
public class Point2d
extends      Coordinate
{
    private static Logger log = Logger.getLogger(Point2d.class);

    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) {
        super(x, y);
    }

    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;
    }

    public Point2d extrapolate(double t, Point2d other) {
        if (other == null) {
            return null;
        }
        Point2d p = newPoint();
        p.x = t*(other.x - x) + x;
        p.y = t*(other.y - y) + y;
        return p;
    }

    public Point2d newPoint() {
        return new Point2d(0d, 0d);
    }

    public Point2d newPoint(double x, double y) {
        return new Point2d(x, y);
    }

    public void inverseDistanceWeighting(Point2d [] ps) {

        double sum = 0d;

        double [] d = new double[ps.length];

        for (int i = 0; i < ps.length; ++i) {
            Point2d p = ps[i];
            if (p != null) {
                double di = distance(p);
                if (di < 1e-5d) { z = p.z; return; }
                di = 1d/di;
                d[i] = di;
                sum += di;
            }
        }

        if (sum == 0d) {
            return;
        }

        double v = 0d;

        for (int i = 0; i < ps.length; ++i) {
            Point2d p = ps[i];
            if (p != null) {
                v += p.z*d[i];
            }
        }
        z = v/sum;
    }

    public static Point2d average(Point2d [] ps) {

        Point2d p = null;
        int count = 0;

        for (int i = 0; i < ps.length; ++i) {
            Point2d t = ps[i];
            if (t != null) {
                ++count;
                if (p == null) {
                    p = t.newPoint(t.x, t.y);
                }
                else {
                    p.x += t.x;
                    p.y += t.y;
                }
            }
        }

        if (p != null) {
            double s = 1d/count;
            p.x *= s;
            p.y *= s;
        }

        return p;
    }

    public boolean near(Point2d [] ps) {

        for (int i = 0; i < ps.length; ++i) {
            Point2d p = ps[i];
            if (p != null && distance(p) > EPSILON) {
                return false;
            }
        }

        return true;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org