ingo@1115: /* ingo@1115: * Copyright (c) 2010 by Intevation GmbH ingo@1115: * ingo@1115: * This program is free software under the LGPL (>=v2.1) ingo@1115: * Read the file LGPL.txt coming with the software for details ingo@1115: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@1115: */ ingo@1115: sascha@360: package de.intevation.gnv.math; sascha@360: sascha@361: import com.vividsolutions.jts.geom.Coordinate; sascha@360: sascha@360: /** sascha@805: * Implements {@link de.intevation.gnv.math.Metrics} sascha@805: * for linear interpolations and distance measurements. sascha@798: * @author Sascha L. Teichmann sascha@360: */ sascha@360: public final class LinearMetrics sascha@360: implements Metrics sascha@360: { sascha@805: /** sascha@805: * Instance to prevent needless creations of instances. sascha@805: */ sascha@360: public static final Metrics INSTANCE = sascha@360: new LinearMetrics(); sascha@360: sascha@805: /** sascha@805: * Implements the corresponding linear interpolator. sascha@805: */ sascha@360: public static final class LinearInterpolator sascha@360: implements Interpolator sascha@360: { sascha@360: private double mx; sascha@360: private double bx; sascha@360: private double my; sascha@360: private double by; sascha@360: sascha@805: /** sascha@805: * Constructor to create a linear interpolator between two sascha@805: * given points. sascha@805: * @param p1 The first point. sascha@805: * @param p2 The second point. sascha@805: */ sascha@361: public LinearInterpolator(Coordinate p1, Coordinate p2) { sascha@360: sascha@360: /* sascha@360: I) p1.x = 0*m + bx sascha@360: II) p2.x = 1*m + bx sascha@360: sascha@360: bx = p1.x sascha@360: sascha@360: p2.x = m + p1.x sascha@360: sascha@360: mx = p2.x - p1.x sascha@360: */ sascha@360: sascha@361: bx = p1.x; sascha@361: mx = p2.x - bx; sascha@360: sascha@361: by = p1.y; sascha@361: my = p2.y - by; sascha@360: } sascha@360: sascha@361: public void interpolate(double t, Coordinate v) { sascha@361: v.x = t*mx + bx; sascha@361: v.y = t*my + by; sascha@360: } sascha@360: } // class LinearInterpolator sascha@360: sascha@360: private LinearMetrics() { sascha@360: } sascha@360: sascha@361: public double distance(Coordinate p1, Coordinate p2) { sascha@360: return p1.distance(p2); sascha@360: } sascha@360: sascha@361: public Interpolator getInterpolator(Coordinate p1, Coordinate p2) { sascha@360: return new LinearInterpolator(p1, p2); sascha@360: } sascha@360: } sascha@836: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :