-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathearthworkvolume.py
215 lines (193 loc) · 6.59 KB
/
earthworkvolume.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# earthworkvolume.py
#
# Copyright 2010 Javier Rovegno Campos <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
class point():
"""
>>> a = point(0, 0)
>>> print 'point is %s where x=%s and y=%s'%(a, a.x, a.y)
point is (0.0, 0.0) where x=0.0 and y=0.0
"""
def __init__(self, x, y):
self.x = float(x)
self.y = float(y)
def __repr__(self):
return "(%s, %s)"% (self.x, self.y)
def __str__(self):
return "(%s, %s)"% (self.x, self.y)
class line():
"""
>>> a = point(0, 0)
>>> b = point(3, 3)
>>> l1 = line(a, b)
>>> print '%s, m=%s, n=%s'%(l1, l1.m, l1.n)
y = 1.0 * x + 0.0, m=1.0, n=0.0
>>> d = point(4, 5)
>>> g = point(10, 2)
>>> l2 = line(d, g)
>>> print l1 - l2
y = 1.5 * x + -7.0
>>> l2.y(4.0) == 5.0
True
>>> l2.x(2) == 10.0
True
>>> print line(m=0.5,n=2)
y = 0.5 * x + 2
>>> l1.area(0.0, 1.0), l2.area(0.0, 1.0)
(0.5, 6.75)
"""
def __init__(self, *args, **kargs):
if args:
p1 = args[0]
p2 = args[1]
if p2.x == p1.x:
raise ValueError, "x1 must be different of x2"
self.m = (p2.y - p1.y)/(p2.x - p1.x)
self.n = p2.y - p2.x * self.m
elif kargs:
self.m = kargs['m']
self.n = kargs['n']
else:
raise ValueError, "You must define 2 points or m,n as argument"
def __repr__(self):
return "y = %s * x + %s"% (self.m, self.n)
def __str__(self):
return "y = %s * x + %s"% (self.m, self.n)
def __sub__(self, other):
return line(m=self.m - other.m, n=self.n - other.n)
def y(self, x):
return self.m * x + self.n
def x(self, y):
return (y - self.n) / self.m
def area(self, x1, x2):
return (self.y(x1) + self.y(x2))/2.0 * (x2 - x1)
class polyline():
"""
>>> lx = [0.0,1.0,2.0,3.0]
>>> ly = [0.0,1.0,0.0,-1.0]
>>> f = polyline(lx, ly)
>>> print f.lines
[y = 1.0 * x + 0.0, y = -1.0 * x + 2.0, y = -1.0 * x + 2.0]
>>> print f.points
[(0.0, 0.0), (1.0, 1.0), (2.0, 0.0), (3.0, -1.0)]
>>> f.y(0.0), f.y(2.0), f.y(3.0)
(0.0, 0.0, -1.0)
>>> f.y(0.5), f.y(1.5), f.y(2.5)
(0.5, 0.5, -0.5)
>>> f.y(-0.5)
Traceback (most recent call last):
...
ValueError: x out of range in polyline domain
>>> f.y(3.5)
Traceback (most recent call last):
...
ValueError: x out of range in polyline domain
>>> f.x(0.0), f.x(0.0,1), f.x(-1.0)
(0.0, 2.0, 3.0)
>>> f.x(0.5), f.x(0.5, 1, 2), f.x(-0.5)
(0.5, 1.5, 2.5)
>>> f.xin(0.0, 0), f.xin(0.0, 1), f.xin(1.9999999999999999, 2)
(True, False, True)
>>> f.area(0.0,1.0), f.area(0.0,2.0), f.area(0.0,3.0)
(0.5, 1.0, 0.5)
"""
def __init__(self, lx, ly):
self.lines = []
self.points = []
if not isinstance(lx,list):
raise ValueError, "lx is not a list of x's coordinates of the polyline"
if not isinstance(lx,list) or not isinstance(ly,list):
raise ValueError, "ly is not a list of y's coordinates of the polyline"
if len(lx) != len(ly):
raise ValueError, "length of len(lx) != len(ly)"
for i in range(len(lx)):
self.points.append(point(lx[i], ly[i]))
for j in range(len(self.points) - 1):
self.lines.append(line(self.points[j], self.points[j+1]))
def xin(self, x, j):
return self.points[j].x <= x <= self.points[j+1].x
def yin(self, y, j):
ymin = min(self.points[j].y,self.points[j+1].y)
ymax = max(self.points[j].y,self.points[j+1].y)
return ymin <= y <= ymax
def y(self, x, j=None, n=None):
if j == None:
j = 0
if n == None:
n = len(self.lines)
value = None
while j < n:
if self.xin(x, j):
value = self.lines[j].y(x)
break
j += 1
if value == None:
raise ValueError, "x out of range in polyline domain"
return value
def x(self, y, j=None, n=None):
if j == None:
j = 0
if n == None:
n = len(self.lines)
value = None
while j < n:
if self.yin(y, j):
value = self.lines[j].x(y)
break
j += 1
if value == None:
raise ValueError, "y out of range in polyline domain"
return value
def area(self, x1, x2):
"""
x1 inf limit
x2 sup limit
"""
if x1 < self.points[0].x:
raise ValueError, "x values out of range in polyline domain"
if x2 > self.points[-1].x:
raise ValueError, "x values out of range in polyline domain"
value = 0.0
j = 0
n = len(self.lines)
x1pass = False
while j < n:
if self.xin(x1, j) and self.xin(x2, j):
value += self.lines[j].area(x1, x2)
break
if x1pass and not self.xin(x2, j):
value += self.lines[j].area(self.points[j].x, self.points[j+1].x)
if self.xin(x1, j) and not self.xin(x2, j):
value += self.lines[j].area(x1, self.points[j+1].x)
x1pass = True
if x1pass and self.xin(x2, j):
value += self.lines[j].area(self.points[j].x, x2)
break
j += 1
return value
def addline(self, p1, p2):
# TODO addline at last
pass
def main():
pass
if __name__ == '__main__':
import doctest
doctest.testmod()
main()