瀏覽代碼

g.gui.iclass: replace dist_point_to_segment() removed from matplotlib (#645)

It was disabled for matplotlib 3.1.0+.
Fixes #461.
nilason 4 年之前
父節點
當前提交
281017d2d4
共有 2 個文件被更改,包括 53 次插入1 次删除
  1. 1 1
      gui/wxpython/iscatt/plots.py
  2. 52 0
      gui/wxpython/iscatt/utils.py

+ 1 - 1
gui/wxpython/iscatt/plots.py

@@ -24,6 +24,7 @@ from multiprocessing import Process, Queue
 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
 
@@ -35,7 +36,6 @@ try:
         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

+ 52 - 0
gui/wxpython/iscatt/utils.py

@@ -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)