diff options
author | Paul Davis <paul@linuxaudiosystems.com> | 2013-11-04 11:56:10 -0500 |
---|---|---|
committer | Paul Davis <paul@linuxaudiosystems.com> | 2013-11-04 11:56:10 -0500 |
commit | 6473cc7cb431abe71721341c550130e61cf64aa1 (patch) | |
tree | 1f0a2c5783e883d817d4667269abd73d0fdb5afb /libs/canvas/poly_line.cc | |
parent | 08b485db75082a21c3814b0a4517f2b2fc994b77 (diff) |
drop use of bounding box to determine whether an item covers a point; add Item::covers(Duple const&)
Default implementation for Item still uses bounding box, but specializations for Arc (Circle), Polygon, Line and PolyLine have been added
Diffstat (limited to 'libs/canvas/poly_line.cc')
-rw-r--r-- | libs/canvas/poly_line.cc | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/libs/canvas/poly_line.cc b/libs/canvas/poly_line.cc index bdc4af9c10..2441e4e3dc 100644 --- a/libs/canvas/poly_line.cc +++ b/libs/canvas/poly_line.cc @@ -37,3 +37,37 @@ PolyLine::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) cons context->stroke (); } } + +bool +PolyLine::covers (Duple const & point) const +{ + Duple p = canvas_to_item (point); + + const Points::size_type npoints = _points.size(); + + if (npoints < 2) { + return false; + } + + Points::size_type i; + Points::size_type j; + + /* repeat for each line segment */ + + for (i = 1, j = 0; i < npoints; ++i, ++j) { + + /* 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. + */ + + double area = fabs (_points[j].x * (_points[j].y - p.y)) + + (_points[i].x * (p.y - _points[j].y)) + + (p.x * (_points[j].y - _points[i].y)); + if (area < 0.001) { + return true; + } + } + + return false; +} |