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

Added support for points which are sequences (tuple, array, etc) #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ import simplify
simplify(points, tolerance, highQuality)
```

`points`: A sequence (e.g. tuple or array) of dictionaries, containing x, y coordinates: `{'x': int/float, 'y': int/float}`
or a sequence of sequences of at least two numbers (int or float), the first
two elements of each inner sequence are treated as coordinates. Extra elements (e.g. z
coordinate, name, etc) of both point sequences and dicts are ignored but preserved for
points which remain.

`points`: An array of dictionaries, containing x, y coordinates: `{'x': int/float, 'y': int/float}`

Examples
* array of dict - `[{'x': 1, 'y': 1}, {'x': 2, 'y': 3}, {'x': 3, 'y': 5} ]`
* tuple of tuple - `((1,1), (2,3), (3,5), (5,5))`
* tuple of array - `([1,1,'first'], [2,3,'second'], [5,8,'third'])`

`tolerance (optional, 0.1 by default)`: Affects the amount of simplification that occurs (the smaller, the less simplification).

`highestQuality (optional, True by default)`: Flag to exclude the distance pre-processing. Produces higher quality results, but runs slower.
47 changes: 44 additions & 3 deletions simplify.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@

def getSquareDistance_list(p1, p2):
"""
Square distance between two points
"""
dx = p1[0] - p2[0]
dy = p1[1] - p2[1]

return dx * dx + dy * dy


def getSquareSegmentDistance_list(p, p1, p2):
"""
Square distance between point and a segment
"""
x = p1[0]
y = p1[1]

dx = p2[0] - x
dy = p2[1] - y

if dx != 0 or dy != 0:
t = ((p[0] - x) * dx + (p[1] - y) * dy) / (dx * dx + dy * dy)

def getSquareDistance(p1, p2):
if t > 1:
x = p2[0]
y = p2[1]
elif t > 0:
x += dx * t
y += dy * t

dx = p[0] - x
dy = p[1] - y

return dx * dx + dy * dy

def getSquareDistance_dict(p1, p2):
"""
Square distance between two points
"""
Expand All @@ -10,7 +44,7 @@ def getSquareDistance(p1, p2):
return dx * dx + dy * dy


def getSquareSegmentDistance(p, p1, p2):
def getSquareSegmentDistance_dict(p, p1, p2):
"""
Square distance between point and a segment
"""
Expand All @@ -35,8 +69,11 @@ def getSquareSegmentDistance(p, p1, p2):

return dx * dx + dy * dy


def simplifyRadialDistance(points, tolerance):
point_is_dict = isinstance(points[0], dict)

getSquareDistance = getSquareDistance_dict if point_is_dict else getSquareDistance_list

length = len(points)
prev_point = points[0]
new_points = [prev_point]
Expand All @@ -55,6 +92,10 @@ def simplifyRadialDistance(points, tolerance):


def simplifyDouglasPeucker(points, tolerance):
point_is_dict = isinstance(points[0], dict)

getSquareSegmentDistance = getSquareSegmentDistance_dict if point_is_dict else getSquareSegmentDistance_list

length = len(points)
markers = [0] * length # Maybe not the most efficent way?

Expand Down