X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fcanvas%2Fpolygon.cc;h=2e818236160d77335aec155aae1245d3de4a2a6c;hb=b880a381523b2cfdb7ebd17c27fff1adf90fa028;hp=1103cedbeea045861cb16ef3e5fa03c09efffd65;hpb=77f5f4c4bf9074d953a1653658c8f96f38ae258c;p=ardour.git diff --git a/libs/canvas/polygon.cc b/libs/canvas/polygon.cc index 1103cedbee..2e81823616 100644 --- a/libs/canvas/polygon.cc +++ b/libs/canvas/polygon.cc @@ -25,10 +25,19 @@ Polygon::Polygon (Group* parent) : Item (parent) , PolyItem (parent) , Fill (parent) + , multiple (0) + , constant (0) + , cached_size (0) { } +Polygon::~Polygon () +{ + delete [] multiple; + delete [] constant; +} + void Polygon::render (Rect const & area, Cairo::RefPtr context) const { @@ -51,3 +60,72 @@ Polygon::render (Rect const & area, Cairo::RefPtr context) const } } +void +Polygon::cache_shape_computation () const +{ + Points::size_type npoints = _points.size(); + + if (npoints == 0) { + return; + } + + Points::size_type i; + Points::size_type j = npoints -1; + + if (cached_size < npoints) { + cached_size = npoints; + delete [] multiple; + multiple = new float[cached_size]; + delete [] constant; + constant = new float[cached_size]; + } + + for (i = 0; i < npoints; i++) { + if (_points[j].y == _points[i].y) { + constant[i] = _points[i].x; + multiple[i] = 0; + } else { + constant[i] = _points[i].x-(_points[i].y*_points[j].x)/(_points[j].y-_points[i].y)+(_points[i].y*_points[i].x)/(_points[j].y-_points[i].y); + multiple[i] = (_points[j].x-_points[i].x)/(_points[j].y-_points[i].y); + } + + j = i; + } +} + +bool +Polygon::covers (Duple const & point) const +{ + Duple p = canvas_to_item (point); + + Points::size_type npoints = _points.size(); + + if (npoints == 0) { + return false; + } + + Points::size_type i; + Points::size_type j = npoints -1; + bool oddNodes = false; + + if (_bounding_box_dirty) { + compute_bounding_box (); + } + + for (i = 0; i < npoints; i++) { + if (((_points[i].y < p.y && _points[j].y >= p.y) || (_points[j].y < p.y && _points[i].y >= p.y))) { + oddNodes ^= (p.y * multiple[i] + constant[i] < p.x); + } + j = i; + } + + return oddNodes; +} + +void +Polygon::compute_bounding_box () const +{ + PolyItem::compute_bounding_box (); + cache_shape_computation (); +} +