summaryrefslogtreecommitdiff
path: root/libs/canvas/line.cc
diff options
context:
space:
mode:
authorPaul Davis <paul@linuxaudiosystems.com>2013-12-09 13:39:10 -0500
committerPaul Davis <paul@linuxaudiosystems.com>2013-12-09 13:39:10 -0500
commit77a63c2bf75d4c8fa6948981a246806453bef95c (patch)
treeef06d3754790d750ffbe865e815e84e7e2be87b2 /libs/canvas/line.cc
parent4780c8425267a26ac5324687fd699ade79389d64 (diff)
ArdourCanvas::Line::covers() uses distance_to_segment_squared()
Diffstat (limited to 'libs/canvas/line.cc')
-rw-r--r--libs/canvas/line.cc45
1 files changed, 37 insertions, 8 deletions
diff --git a/libs/canvas/line.cc b/libs/canvas/line.cc
index fcc190a705..61acf89252 100644
--- a/libs/canvas/line.cc
+++ b/libs/canvas/line.cc
@@ -151,18 +151,47 @@ Line::set_y1 (Coord y1)
bool
Line::covers (Duple const & point) const
{
- Duple p = canvas_to_item (point);
+ const Duple p = canvas_to_item (point);
+ static const Distance threshold = 2.0;
- /* compute area of triangle computed by the two line points and the one
- we are being asked about. If zero (within a given tolerance), the
- points are co-linear and the argument is on the line.
+ /* this quick check works for vertical and horizontal lines, which are
+ * common.
+ */
+
+ if (_points[0].x == _points[1].x) {
+ /* line is vertical, just check x coordinate */
+ return fabs (_points[0].x - p.x) <= threshold;
+ }
+
+ if (_points[0].y == _points[1].y) {
+ /* line is horizontal, just check y coordinate */
+ return fabs (_points[0].y - p.y) <= threshold;
+ }
+
+ Duple at;
+ double t;
+ Duple a (_points[0]);
+ Duple b (_points[1]);
+ const Rect visible (_canvas->visible_area());
+
+ /*
+ Clamp the line endpoints to the visible area of the canvas. If we do
+ not do this, we have a line segment extending to COORD_MAX and our
+ math goes wrong.
*/
- double area = fabs (_points[0].x * (_points[0].y - p.y)) +
- (_points[1].x * (p.y - _points[0].y)) +
- (p.x * (_points[0].y - _points[1].y));
+ a.x = min (a.x, visible.x1);
+ a.y = min (a.y, visible.y1);
+ b.x = min (b.x, visible.x1);
+ b.y = min (b.y, visible.y1);
+
+ double d = distance_to_segment_squared (p, a, b, t, at);
+
+ if (t < 0.0 || t > 1.0) {
+ return false;
+ }
- if (area < 0.001) {
+ if (d < threshold) {
return true;
}