Skip to content

Commit

Permalink
g.gui.iclass: replace dist_point_to_segment()
Browse files Browse the repository at this point in the history
from matplotlib, which was disabled for matplotlib 3.1.0+.

Fixes #461
  • Loading branch information
nilason committed May 23, 2020
1 parent 71c8a1f commit 84f2208
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gui/wxpython/iscatt/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from copy import deepcopy
from iscatt.core_c import MergeArrays, ApplyColormap
from iscatt.dialogs import ManageBusyCursorMixin
from iscatt.utils import dist_point_to_segment
from core.settings import UserSettings
from gui_core.wrap import Menu, NewId

Expand All @@ -35,7 +36,6 @@
FigureCanvasWxAgg as FigCanvas
from matplotlib.lines import Line2D
from matplotlib.artist import Artist
from matplotlib.mlab import dist_point_to_segment
from matplotlib.patches import Polygon, Ellipse, Rectangle
import matplotlib.image as mi
import matplotlib.colors as mcolors
Expand Down
52 changes: 52 additions & 0 deletions gui/wxpython/iscatt/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
@package iscatt.utils
@brief Misc utilities for iscatt
(C) 2020 by the GRASS Development Team
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
@author Nicklas Larsson <n_larsson yahoo com>
"""

import numpy as np

# Code originate from matplotlib
# https://matplotlib.org/3.0.0/_modules/matplotlib/mlab.html#dist
def dist(x, y):
"""
Return the distance between two points.
"""
d = x - y
return np.sqrt(np.dot(d, d))

# Code originate from matplotlib
# https://matplotlib.org/3.0.0/_modules/matplotlib/mlab.html#dist_point_to_segment
def dist_point_to_segment(p, s0, s1):
"""
Get the distance of a point to a segment.
*p*, *s0*, *s1* are *xy* sequences
This algorithm from
http://geomalgorithms.com/a02-_lines.html
"""
p = np.asarray(p, float)
s0 = np.asarray(s0, float)
s1 = np.asarray(s1, float)
v = s1 - s0
w = p - s0

c1 = np.dot(w, v)
if c1 <= 0:
return dist(p, s0)

c2 = np.dot(v, v)
if c2 <= c1:
return dist(p, s1)

b = c1 / c2
pb = s0 + b * v
return dist(p, pb)

0 comments on commit 84f2208

Please sign in to comment.