"Fossies" - the Fresh Open Source Software Archive 
Member "radialnet/core/Interpolation.py" (17 Feb 2008, 3875 Bytes) of package /linux/privat/old/radialnet-0.44.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "Interpolation.py" see the
Fossies "Dox" file reference documentation.
1 # vim: set fileencoding=utf-8 :
2
3 # Copyright (C) 2007 Insecure.Com LLC.
4 #
5 # Author: João Paulo de Souza Medeiros <ignotus21@gmail.com>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.__start_point = (0, 0)
32 """Initial point of interpolation"""
33 self.__final_point = (0, 0)
34 """Final point of interpolation"""
35 self.__interpolated_points = []
36 """Interpolated points vector"""
37
38
39 def set_start_point(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.__start_point = (a, b)
49
50
51 def set_final_point(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.__final_point = (a, b)
60
61
62 def get_weighed_points(self, number_of_pass, pass_vector):
63 """
64 Return the vector of coordinates between the initial and final
65 coordinates with the specified size
66 @type number_of_pass: number
67 @param number_of_pass: The number of pass of interpolation
68 @rtype: list
69 @return: A list of tuples with interpolated points
70 """
71 (ai, bi) = self.__start_point
72 (af, bf) = self.__final_point
73
74 a_conversion_factor = float(af - ai) / sum(pass_vector)
75 b_conversion_factor = float(bf - bi) / sum(pass_vector)
76
77 a_pass = 0
78 b_pass = 0
79
80 self.__interpolated_points = range(number_of_pass)
81
82 for i in range(0, number_of_pass):
83
84 a_pass += pass_vector[i] * a_conversion_factor
85 b_pass += pass_vector[i] * b_conversion_factor
86 self.__interpolated_points[i] = (ai + a_pass, bi + b_pass)
87
88 return self.__interpolated_points
89
90
91 def get_points(self, number_of_pass):
92 """
93 Return the vector of coordinates between the initial and final
94 coordinates with the specified size
95 @type number_of_pass: number
96 @param number_of_pass: The number of pass of interpolation
97 @rtype: list
98 @return: A list of tuples with interpolated points
99 """
100 (ai, bi) = self.__start_point
101 (af, bf) = self.__final_point
102
103 a_pass = float(af - ai) / number_of_pass
104 b_pass = float(bf - bi) / number_of_pass
105
106 self.__interpolated_points = range(number_of_pass)
107
108 for i in range(1, number_of_pass + 1):
109 self.__interpolated_points[i - 1] = (ai + a_pass * i,
110 bi + b_pass * i)
111
112 return self.__interpolated_points
113
114
115
116 if __name__ == "__main__":
117
118 # Testing application
119
120 i = Linear2DInterpolator()
121
122 i.set_start_point(0, 0)
123 i.set_final_point(1, 1)
124
125 print len(i.get_points(10)), i.get_points(10)
126