2 Copyright (C) 2011-2013 Paul Davis
3 Author: Carl Hetherington <cth@carlh.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "canvas/polygon.h"
22 using namespace ArdourCanvas;
24 Polygon::Polygon (Group* parent)
42 Polygon::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
45 setup_outline_context (context);
46 render_path (area, context);
48 if (!_points.empty ()) {
50 Duple p = item_to_window (Duple (_points.front().x, _points.front().y));
51 context->move_to (p.x, p.y);
54 context->stroke_preserve ();
58 setup_fill_context (context);
64 Polygon::cache_shape_computation () const
66 Points::size_type npoints = _points.size();
73 Points::size_type j = npoints -1;
75 if (cached_size < npoints) {
76 cached_size = npoints;
78 multiple = new float[cached_size];
80 constant = new float[cached_size];
83 for (i = 0; i < npoints; i++) {
84 if (_points[j].y == _points[i].y) {
85 constant[i] = _points[i].x;
88 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);
89 multiple[i] = (_points[j].x-_points[i].x)/(_points[j].y-_points[i].y);
97 Polygon::covers (Duple const & point) const
99 Duple p = canvas_to_item (point);
101 Points::size_type npoints = _points.size();
108 Points::size_type j = npoints -1;
109 bool oddNodes = false;
111 if (_bounding_box_dirty) {
112 compute_bounding_box ();
115 for (i = 0; i < npoints; i++) {
116 if (((_points[i].y < p.y && _points[j].y >= p.y) || (_points[j].y < p.y && _points[i].y >= p.y))) {
117 oddNodes ^= (p.y * multiple[i] + constant[i] < p.x);
126 Polygon::compute_bounding_box () const
128 PolyItem::compute_bounding_box ();
129 cache_shape_computation ();