Skip to content

Commit

Permalink
Merge pull request #1 from mpikoula/patch-1
Browse files Browse the repository at this point in the history
Patch 1
  • Loading branch information
mpikoula authored Mar 30, 2018
2 parents 7f7bf65 + 211a2bd commit 515ab9f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 32 deletions.
38 changes: 6 additions & 32 deletions frechetdist/frechetdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,24 @@

__all__ = [ 'frdist' ]

def _eucdist(point1, point2):
"""
Computes the Euclidean distance between
two points in two-dimensional space.
More information:
https://en.wikipedia.org/wiki/Euclidean_distance#Two_dimensions
Parameters
----------
point1 : Input array of two points.
point2 : Input array of two points.
Returns
-------
The Euclidean distance (float64) between points `point1`
and `point2`.
Examples
--------
>>> P=[0,0]
>>> Q=[5,5]
>>> _eucdist(P,Q)
>>> 7.0710678118654755
"""
return math.sqrt(((point1[0] - point2[0])**2)+((point1[1] - point2[1])**2))


def _c(ca,i,j,p,q):

if ca[i,j] > -1:
return ca[i,j]
elif i == 0 and j == 0:
ca[i,j] = _eucdist(p[i],q[j])
ca[i,j] = np.linalg.norm(p[i]-q[j])
elif i > 0 and j == 0:
ca[i,j] = max( _c(ca,i-1,0,p,q), _eucdist(p[i], q[j]) )
ca[i,j] = max( _c(ca,i-1,0,p,q), np.linalg.norm(p[i]-q[j]) )
elif i == 0 and j > 0:
ca[i,j] = max( _c(ca,0,j-1,p,q), _eucdist(p[i],q[j]))
ca[i,j] = max( _c(ca,0,j-1,p,q), np.linalg.norm(p[i]-q[j]) )
elif i > 0 and j > 0:
ca[i,j] = max( \
min( \
_c(ca,i-1,j,p,q), \
_c(ca,i-1,j-1,p,q), \
_c(ca,i,j-1,p,q) \
), \
_eucdist(p[i],q[j]) \
np.linalg.norm(p[i]-q[j]) \
)
else:
ca[i,j] = float('inf')
Expand Down Expand Up @@ -117,6 +90,8 @@ def frdist(p,q):
>>> frdist(P,Q)
>>> 0
"""
p = np.array(p, np.float64)
q = np.array(q, np.float64)

len_p = len(p)
len_q = len(q)
Expand All @@ -131,4 +106,3 @@ def frdist(p,q):

dist = _c(ca,len_p-1,len_q-1,p,q)
return dist

6 changes: 6 additions & 0 deletions frechetdist/tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
'P': [[1,1], [2,1], [2,2]],
'Q': [[1,1], [2,1], [2,2]],
'expected': 0
},

{
'P': [ [-1,0], [0,1], [1,0], [0, -1] ],
'Q': [ [-2,0], [0,2], [2,0], [0, -2] ],
'expected': 1.0
}
]

Expand Down

0 comments on commit 515ab9f

Please sign in to comment.