GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — better edge picking · ogdf/ogdf-python@afa7201

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/ogdf_python/matplotlib
    • artist.py
    • rendering.h
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/ogdf_python/matplotlib/artist.py‎

Copy file name to clipboardExpand all lines: src/ogdf_python/matplotlib/artist.py
+19-3Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ def calc_path(self, GA):
8686

8787

8888
class EdgeArtist(PathPatch):
89+
PICK_DISTANCE = 10
90+
8991
def __init__(self, edge, GA, **kwargs):
9092
self.edge = edge
9193
attrs = self.calc_attributes(GA)
@@ -131,7 +133,7 @@ def calc_attributes(self, GA):
131133
edgecolor=color(GA.strokeColor[self.edge]),
132134
linestyle=strokeType(GA.strokeType[self.edge]),
133135
linewidth=GA.strokeWidth[self.edge],
134-
picker=max(GA.strokeWidth[self.edge], 10),
136+
picker=self._should_pick,
135137
)
136138

137139
def calc_arrow_attributes(self, GA):
@@ -148,7 +150,21 @@ def calc_path(self, GA):
148150
(GA.y[self.edge.source()] + GA.y[self.edge.target()]) / 2)
149151
src_arr = ogdf.DPolygon() if ogdf.isArrowEnabled(GA, self.edge.adjSource()) else nullptr
150152
tgt_arr = ogdf.DPolygon() if ogdf.isArrowEnabled(GA, self.edge.adjTarget()) else nullptr
151-
poly = ogdf.drawEdge(self.edge, GA, self.label_pos, src_arr, tgt_arr)
153+
self.poly = ogdf.drawEdge(self.edge, GA, self.label_pos, src_arr, tgt_arr)
154+
self.bbox = self.poly.getBoundingBox()
152155
self.src_arr.get_path().vertices = np.asarray(dPolylineToPathVertices(src_arr) if src_arr else [] + [(0, 0)], float)
153156
self.tgt_arr.get_path().vertices = np.asarray(dPolylineToPathVertices(tgt_arr) if tgt_arr else [] + [(0, 0)], float)
154-
return dPolylineToPath(poly)
157+
return dPolylineToPath(self.poly)
158+
159+
def _should_pick(self, artist, event):
160+
x, y = event.xdata, event.ydata
161+
if not self.bbox.p1().m_x - self.PICK_DISTANCE <= x <= self.bbox.p2().m_x + self.PICK_DISTANCE:
162+
return False, None
163+
if not self.bbox.p1().m_y - self.PICK_DISTANCE <= y <= self.bbox.p2().m_y + self.PICK_DISTANCE:
164+
return False, None
165+
out = ogdf.DPoint()
166+
dist = ogdf.closestPointOnLine(self.poly, ogdf.DPoint(x, y), out)
167+
if dist <= self.PICK_DISTANCE:
168+
return True, {"point": out, "dist": dist}
169+
else:
170+
return False, None
Collapse file

‎src/ogdf_python/matplotlib/rendering.h‎

Copy file name to clipboardExpand all lines: src/ogdf_python/matplotlib/rendering.h
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,46 @@ namespace ogdf {
236236
}
237237
return points;
238238
}
239+
240+
double closestPointOnLine(const DPolyline &line, const DPoint &x, DPoint &out) {
241+
if (line.size() == 0) {
242+
return std::numeric_limits<double>::quiet_NaN();
243+
} else if (line.size() == 1) {
244+
out = line.front();
245+
return out.distance(x);
246+
}
247+
auto it = line.begin();
248+
auto p1 = *it;
249+
it++;
250+
double minDist = std::numeric_limits<double>::infinity();
251+
for (auto p2 = *it; it != line.end(); it++) {
252+
// https://stackoverflow.com/a/10984080/805569
253+
p2 = *it;
254+
auto d = p2 - p1;
255+
auto r = (d * (x - p1)) / d.normSquared();
256+
257+
if (r < 0) {
258+
auto l = (x - p1).normSquared();
259+
if (l < minDist) {
260+
minDist = l;
261+
out = p1;
262+
}
263+
} else if (r > 1) {
264+
auto l = (x - p2).normSquared();
265+
if (l < minDist) {
266+
minDist = l;
267+
out = p2;
268+
}
269+
} else {
270+
auto y = p1 + r * d;
271+
auto l = (x - y).normSquared();
272+
if (l < minDist) {
273+
minDist = l;
274+
out = y;
275+
}
276+
}
277+
p1 = p2;
278+
}
279+
return minDist;
280+
}
239281
};

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.