summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Davis <paul@linuxaudiosystems.com>2014-11-24 13:09:09 +0200
committerPaul Davis <paul@linuxaudiosystems.com>2015-04-29 07:22:05 -0400
commit59ce8663f9a20f8bd7a16620b8bccaa23357be36 (patch)
tree40914fea1a3ac744d1da3590914627785d5cfd85
parent6e335ca5d90ddc62b0af9a60a6b156ed74ac8eb1 (diff)
add Rectangle::vertical_fraction() as a convenience method
Conflicts: libs/canvas/rectangle.cc
-rw-r--r--libs/canvas/canvas/rectangle.h11
-rw-r--r--libs/canvas/rectangle.cc20
2 files changed, 31 insertions, 0 deletions
diff --git a/libs/canvas/canvas/rectangle.h b/libs/canvas/canvas/rectangle.h
index 5df10fbc60..b979a24430 100644
--- a/libs/canvas/canvas/rectangle.h
+++ b/libs/canvas/canvas/rectangle.h
@@ -64,6 +64,17 @@ public:
void set_x1 (Coord);
void set_y1 (Coord);
+ /** return @param y as a floating point fraction of the overall
+ * height of the rectangle. @param y is in canvas coordinate space.
+ *
+ * A value of zero indicates that y is at the bottom of the
+ * rectangle; a value of 1 indicates that y is at the top.
+ *
+ * Will return zero if there is no bounding box or if y
+ * is outside the bounding box.
+ */
+ double vertical_fraction (double y) const;
+
enum What {
NOTHING = 0x0,
LEFT = 0x1,
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<Rect> 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();
+}