| 1 | # Copyright (C) 2007 Adriano Monteiro Marques <py.adriano@gmail.com> |
|---|
| 2 | # |
|---|
| 3 | # Author: Joao Paulo de Souza Medeiros <ignotus21@gmail.com> |
|---|
| 4 | # |
|---|
| 5 | # This program is free software; you can redistribute it and/or modify |
|---|
| 6 | # it under the terms of the GNU General Public License as published by |
|---|
| 7 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | # (at your option) any later version. |
|---|
| 9 | # |
|---|
| 10 | # This program is distributed in the hope that it will be useful, |
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | # GNU General Public License for more details. |
|---|
| 14 | # |
|---|
| 15 | # You should have received a copy of the GNU General Public License |
|---|
| 16 | # along with this program; if not, write to the Free Software |
|---|
| 17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | |
|---|
| 19 | from Numeric import arange |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | class Linear2DInterpolator: |
|---|
| 23 | """ |
|---|
| 24 | Implements a bidimesional linear interpolator. |
|---|
| 25 | """ |
|---|
| 26 | |
|---|
| 27 | def __init__(self): |
|---|
| 28 | """ |
|---|
| 29 | Constructor method of Linear2DInterpolator class |
|---|
| 30 | """ |
|---|
| 31 | self.__initialPoint = (0, 0) |
|---|
| 32 | """Initial point of interpolation""" |
|---|
| 33 | self.__finalPoint = (0, 0) |
|---|
| 34 | """Final point of interpolation""" |
|---|
| 35 | self.__interpolatedPoints = [] |
|---|
| 36 | """Interpolated points vector""" |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | def setInitialPoint(self, a, b): |
|---|
| 40 | """ |
|---|
| 41 | Set initial coordinate |
|---|
| 42 | Set final coordinate |
|---|
| 43 | @type a: number |
|---|
| 44 | @param a: The first component of final point |
|---|
| 45 | @type b: number |
|---|
| 46 | @param b: The second component of final point |
|---|
| 47 | """ |
|---|
| 48 | self.__initialPoint = (a, b) |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | def setFinalPoint(self, a, b): |
|---|
| 52 | """ |
|---|
| 53 | Set final coordinate |
|---|
| 54 | @type a: number |
|---|
| 55 | @param a: The first component of final point |
|---|
| 56 | @type b: number |
|---|
| 57 | @param b: The second component of final point |
|---|
| 58 | """ |
|---|
| 59 | self.__finalPoint = (a, b) |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | def interpolate(self, a): |
|---|
| 63 | """ |
|---|
| 64 | Return the primary point with the interpolated point |
|---|
| 65 | @type a: number |
|---|
| 66 | @param a: The first component of point to be interpolated |
|---|
| 67 | @rtype: tuple |
|---|
| 68 | @return: Tuple with interpolated points |
|---|
| 69 | """ |
|---|
| 70 | (ai, bi) = self.__initialPoint |
|---|
| 71 | (af, bf) = self.__finalPoint |
|---|
| 72 | |
|---|
| 73 | try: |
|---|
| 74 | result = (a, bi + ((a - ai) * (bf - bi) / (af - ai))) |
|---|
| 75 | except ZeroDivisionError: |
|---|
| 76 | result = (a, bf) |
|---|
| 77 | |
|---|
| 78 | return result |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | def getInterpolatedPoints(self, numberOfPass): |
|---|
| 82 | """ |
|---|
| 83 | Return the vector of coordinates between the initial and final |
|---|
| 84 | coordinates with the specified size |
|---|
| 85 | @type numberOfPass: number |
|---|
| 86 | @param numberOfPass: The number of pass of interpolation |
|---|
| 87 | @rtype: list |
|---|
| 88 | @return: A list of tuples with interpolated points |
|---|
| 89 | """ |
|---|
| 90 | (ai, bi) = self.__initialPoint |
|---|
| 91 | (af, bf) = self.__finalPoint |
|---|
| 92 | |
|---|
| 93 | aPass = float(af - ai) / numberOfPass |
|---|
| 94 | |
|---|
| 95 | self.__interpolatedPoints = range(numberOfPass) |
|---|
| 96 | |
|---|
| 97 | for i in range(numberOfPass): |
|---|
| 98 | self.__interpolatedPoints[i] = self.interpolate(ai + (i + 1) * aPass) |
|---|
| 99 | |
|---|
| 100 | return self.__interpolatedPoints |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | if __name__ == "__main__": |
|---|
| 105 | |
|---|
| 106 | # Testing application |
|---|
| 107 | |
|---|
| 108 | i = Linear2DInterpolator() |
|---|
| 109 | i.setInitialPoint(0, 0) |
|---|
| 110 | i.setFinalPoint(1, 1) |
|---|
| 111 | print len(i.getInterpolatedPoints(10)), i.getInterpolatedPoints(10) |
|---|
| 112 | |
|---|