2013-04-15 22:10:18 -04:00
|
|
|
/*
|
2019-08-02 22:40:09 -04:00
|
|
|
* Copyright (C) 2012 Carl Hetherington <carl@carlh.net>
|
|
|
|
* Copyright (C) 2013-2015 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2013-04-15 22:10:18 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
#include "canvas/polygon.h"
|
|
|
|
|
|
|
|
using namespace ArdourCanvas;
|
|
|
|
|
2014-06-12 14:53:44 -04:00
|
|
|
Polygon::Polygon (Canvas* c)
|
|
|
|
: PolyItem (c)
|
2013-11-04 11:56:10 -05:00
|
|
|
, multiple (0)
|
|
|
|
, constant (0)
|
|
|
|
, cached_size (0)
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
2014-06-12 14:53:44 -04:00
|
|
|
}
|
2013-04-04 00:32:52 -04:00
|
|
|
|
2014-06-21 11:43:42 -04:00
|
|
|
Polygon::Polygon (Item* parent)
|
|
|
|
: PolyItem (parent)
|
2014-06-12 14:53:44 -04:00
|
|
|
, multiple (0)
|
|
|
|
, constant (0)
|
|
|
|
, cached_size (0)
|
|
|
|
{
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
2013-11-04 11:56:10 -05:00
|
|
|
Polygon::~Polygon ()
|
|
|
|
{
|
|
|
|
delete [] multiple;
|
|
|
|
delete [] constant;
|
|
|
|
}
|
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
void
|
|
|
|
Polygon::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
|
|
|
|
{
|
2020-07-08 20:10:28 -04:00
|
|
|
Points::size_type npoints = _points.size();
|
|
|
|
if (npoints < 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-06 23:15:59 -05:00
|
|
|
if (_outline || _fill) {
|
2020-07-08 20:10:28 -04:00
|
|
|
const double pixel_adjust = (_outline_width == 1.0 ? 0.5 : 0.0);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2020-07-08 20:10:28 -04:00
|
|
|
for (Points::size_type i = 0; i < npoints; i++) {
|
|
|
|
Duple c = item_to_window (Duple (_points[i].x, _points[i].y));
|
|
|
|
if (i == 0) {
|
|
|
|
context->move_to (c.x + pixel_adjust, c.y + pixel_adjust);
|
|
|
|
} else {
|
|
|
|
context->line_to (c.x + pixel_adjust, c.y + pixel_adjust);
|
|
|
|
}
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
2020-07-08 20:10:28 -04:00
|
|
|
context->close_path ();
|
2014-03-06 23:15:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_outline) {
|
|
|
|
setup_outline_context (context);
|
|
|
|
if (_fill) {
|
|
|
|
context->stroke_preserve ();
|
|
|
|
} else {
|
|
|
|
context->stroke ();
|
|
|
|
}
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_fill) {
|
|
|
|
setup_fill_context (context);
|
|
|
|
context->fill ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
void
|
2013-11-04 11:56:10 -05:00
|
|
|
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;
|
2015-10-04 14:51:05 -04:00
|
|
|
multiple[i] = 0;
|
2013-11-04 11:56:10 -05:00
|
|
|
} 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);
|
2015-10-04 14:51:05 -04:00
|
|
|
multiple[i] = (_points[j].x-_points[i].x)/(_points[j].y-_points[i].y);
|
2013-11-04 11:56:10 -05:00
|
|
|
}
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
j = i;
|
2013-11-04 11:56:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
bool
|
2013-11-04 11:56:10 -05:00
|
|
|
Polygon::covers (Duple const & point) const
|
|
|
|
{
|
2014-05-23 22:05:08 -04:00
|
|
|
Duple p = window_to_item (point);
|
2013-11-04 11:56:10 -05:00
|
|
|
|
|
|
|
Points::size_type npoints = _points.size();
|
|
|
|
|
|
|
|
if (npoints == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Points::size_type i;
|
|
|
|
Points::size_type j = npoints -1;
|
|
|
|
bool oddNodes = false;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2022-04-26 23:57:39 -04:00
|
|
|
if (bbox_dirty()) {
|
2021-07-27 15:17:48 -04:00
|
|
|
(void) bounding_box ();
|
2013-11-04 11:56:10 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2013-11-04 11:56:10 -05:00
|
|
|
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))) {
|
2015-10-04 14:51:05 -04:00
|
|
|
oddNodes ^= (p.y * multiple[i] + constant[i] < p.x);
|
2013-11-04 11:56:10 -05:00
|
|
|
}
|
2015-10-04 14:51:05 -04:00
|
|
|
j = i;
|
2013-11-04 11:56:10 -05:00
|
|
|
}
|
2013-12-12 10:03:33 -05:00
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
return oddNodes;
|
|
|
|
}
|
2013-11-04 11:56:10 -05:00
|
|
|
|
|
|
|
void
|
|
|
|
Polygon::compute_bounding_box () const
|
|
|
|
{
|
|
|
|
PolyItem::compute_bounding_box ();
|
|
|
|
cache_shape_computation ();
|
|
|
|
}
|
|
|
|
|