Skip to content
This repository was archived by the owner on Jun 6, 2019. It is now read-only.

Commit 09dccd2

Browse files
author
Omar Estrella
committed
Updated readme; code cleanup (pep8)
1 parent 343f1a6 commit 09dccd2

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
### simplify.py
22

3-
simplify.py is a simple port of simplify.js by Vladimir Agafonkin ([https://github.com/mourner/simplify-js](https://github.com/mourner/simplify-js))
3+
simplify.py is a simple port of simplify.js by Vladimir Agafonkin ([https://github.com/mourner/simplify-js](https://github.com/mourner/simplify-js))
4+
5+
### Usage
6+
7+
```
8+
import simplify
9+
simplify(points, tolerance, highQuality)
10+
```
11+
12+
13+
`points`: An array of dictionaries, containing x, y coordinates: `{'x': int/float, 'y': int/float}`
14+
15+
`tolerance (optional, 0.1 by default)`: Affects the amount of simplification that occurs (the smaller, the less simplification).
16+
17+
`highestQuality (options, True by default)`: Flag to exclude the distance pre-processing. Produces higher quality results, but runs slower.

simplify.py

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
# Square distance between two points
1+
2+
23
def getSquareDistance(p1, p2):
4+
"""
5+
Square distance between two points
6+
"""
37
dx = p1['x'] - p2['x']
48
dy = p1['y'] - p2['y']
5-
9+
610
return dx * dx + dy * dy
711

8-
# Square distance between point and a segment
12+
913
def getSquareSegmentDistance(p, p1, p2):
14+
"""
15+
Square distance between point and a segment
16+
"""
1017
x = p1['x']
1118
y = p1['y']
1219

@@ -17,17 +24,18 @@ def getSquareSegmentDistance(p, p1, p2):
1724
t = ((p['x'] - x) * dx + (p['y'] - y) * dy) / (dx * dx + dy * dy)
1825

1926
if t > 1:
20-
x = p2['x'];
21-
y = p2['y'];
27+
x = p2['x']
28+
y = p2['y']
2229
elif t > 0:
23-
x += dx * t;
24-
y += dy * t;
30+
x += dx * t
31+
y += dy * t
2532

2633
dx = p['x'] - x
2734
dy = p['y'] - y
2835

2936
return dx * dx + dy * dy
3037

38+
3139
def simplifyRadialDistance(points, tolerance):
3240
length = len(points)
3341
prev_point = points[0]
@@ -45,9 +53,10 @@ def simplifyRadialDistance(points, tolerance):
4553

4654
return new_points
4755

56+
4857
def simplifyDouglasPeucker(points, tolerance):
4958
length = len(points)
50-
markers = [0] * length # Maybe not the most efficent way?
59+
markers = [0] * length # Maybe not the most efficent way?
5160

5261
first = 0
5362
last = length - 1
@@ -71,7 +80,7 @@ def simplifyDouglasPeucker(points, tolerance):
7180
max_sqdist = sqdist
7281

7382
if max_sqdist > tolerance:
74-
markers[index] = 1;
83+
markers[index] = 1
7584

7685
first_stack.append(first)
7786
last_stack.append(index)
@@ -97,17 +106,13 @@ def simplifyDouglasPeucker(points, tolerance):
97106

98107
return new_points
99108

100-
def simplify(points, tolerance, highestQuality):
101-
sqtolerance = tolerance * tolerance;
109+
110+
def simplify(points, tolerance=0.1, highestQuality=True):
111+
sqtolerance = tolerance * tolerance
102112

103113
if not highestQuality:
104114
points = simplifyRadialDistance(points, sqtolerance)
105115

106116
points = simplifyDouglasPeucker(points, sqtolerance)
107117

108118
return points
109-
#return points[::-1]
110-
111-
112-
113-

0 commit comments

Comments
 (0)