From 59ce8663f9a20f8bd7a16620b8bccaa23357be36 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Mon, 24 Nov 2014 13:09:09 +0200 Subject: add Rectangle::vertical_fraction() as a convenience method Conflicts: libs/canvas/rectangle.cc --- libs/canvas/rectangle.cc | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'libs/canvas/rectangle.cc') diff --git a/libs/canvas/rectangle.cc b/libs/canvas/rectangle.cc index 07288d5d33..4bb21c42e6 100644 --- a/libs/canvas/rectangle.cc +++ b/libs/canvas/rectangle.cc @@ -266,3 +266,23 @@ Rectangle::set_outline_what (What what) end_visual_change (); } } + +double +Rectangle::vertical_fraction (double y) const +{ + /* y is in canvas coordinates */ + + Duple i (canvas_to_item (Duple (0, y))); + boost::optional r = bounding_box(); + if (!r) { + return 0; /* not really correct, but what else can we do? */ + } + + Rect bbox (r.get()); + + if (i.y < bbox.y0 || i.y >= bbox.y1) { + return 0; + } + + return (i.y - bbox.y0) / bbox.height(); +} -- cgit v1.2.3