1
- # Square distance between two points
1
+
2
+
2
3
def getSquareDistance (p1 , p2 ):
4
+ """
5
+ Square distance between two points
6
+ """
3
7
dx = p1 ['x' ] - p2 ['x' ]
4
8
dy = p1 ['y' ] - p2 ['y' ]
5
-
9
+
6
10
return dx * dx + dy * dy
7
11
8
- # Square distance between point and a segment
12
+
9
13
def getSquareSegmentDistance (p , p1 , p2 ):
14
+ """
15
+ Square distance between point and a segment
16
+ """
10
17
x = p1 ['x' ]
11
18
y = p1 ['y' ]
12
19
@@ -17,17 +24,18 @@ def getSquareSegmentDistance(p, p1, p2):
17
24
t = ((p ['x' ] - x ) * dx + (p ['y' ] - y ) * dy ) / (dx * dx + dy * dy )
18
25
19
26
if t > 1 :
20
- x = p2 ['x' ];
21
- y = p2 ['y' ];
27
+ x = p2 ['x' ]
28
+ y = p2 ['y' ]
22
29
elif t > 0 :
23
- x += dx * t ;
24
- y += dy * t ;
30
+ x += dx * t
31
+ y += dy * t
25
32
26
33
dx = p ['x' ] - x
27
34
dy = p ['y' ] - y
28
35
29
36
return dx * dx + dy * dy
30
37
38
+
31
39
def simplifyRadialDistance (points , tolerance ):
32
40
length = len (points )
33
41
prev_point = points [0 ]
@@ -45,9 +53,10 @@ def simplifyRadialDistance(points, tolerance):
45
53
46
54
return new_points
47
55
56
+
48
57
def simplifyDouglasPeucker (points , tolerance ):
49
58
length = len (points )
50
- markers = [0 ] * length # Maybe not the most efficent way?
59
+ markers = [0 ] * length # Maybe not the most efficent way?
51
60
52
61
first = 0
53
62
last = length - 1
@@ -71,7 +80,7 @@ def simplifyDouglasPeucker(points, tolerance):
71
80
max_sqdist = sqdist
72
81
73
82
if max_sqdist > tolerance :
74
- markers [index ] = 1 ;
83
+ markers [index ] = 1
75
84
76
85
first_stack .append (first )
77
86
last_stack .append (index )
@@ -97,17 +106,13 @@ def simplifyDouglasPeucker(points, tolerance):
97
106
98
107
return new_points
99
108
100
- def simplify (points , tolerance , highestQuality ):
101
- sqtolerance = tolerance * tolerance ;
109
+
110
+ def simplify (points , tolerance = 0.1 , highestQuality = True ):
111
+ sqtolerance = tolerance * tolerance
102
112
103
113
if not highestQuality :
104
114
points = simplifyRadialDistance (points , sqtolerance )
105
115
106
116
points = simplifyDouglasPeucker (points , sqtolerance )
107
117
108
118
return points
109
- #return points[::-1]
110
-
111
-
112
-
113
-
0 commit comments