1 // -*- C++ -*-
2 
3 /*
4  * Gnome Chemistry Utils
5  * gccv/poly-line.cc
6  *
7  * Copyright (C) 2008-2012 Jean Bréfort <jean.brefort@normalesup.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 3 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
22  * USA
23  */
24 
25 #include "config.h"
26 #include "poly-line.h"
27 #include <cmath>
28 
29 using namespace std;
30 
31 namespace gccv {
32 
PolyLine(Canvas * canvas,list<Point> & points)33 PolyLine::PolyLine (Canvas *canvas, list <Point> &points):
34 	LineItem (canvas)
35 {
36 	SetPoints (points);
37 }
38 
PolyLine(Group * parent,list<Point> & points,ItemClient * client)39 PolyLine::PolyLine (Group *parent, list <Point> &points, ItemClient *client):
40 	LineItem (parent, client)
41 {
42 	SetPoints (points);
43 }
44 
~PolyLine()45 PolyLine::~PolyLine ()
46 {
47 }
48 
SetPoints(list<Point> & points)49 void PolyLine::SetPoints (list <Point> &points)
50 {
51 	Invalidate ();
52 	m_Points = points;
53 	BoundsChanged ();
54 	Invalidate ();
55 }
56 
Distance(double x,double y,Item ** item) const57 double PolyLine::Distance (double x, double y, Item **item) const
58 {
59 	list <Point>::const_iterator i = m_Points.begin (), end = m_Points.end ();
60 	if (i == end)
61 		return G_MAXDOUBLE;
62 	double x0 = (*i).x, y0 = (*i).y, x1, y1;
63 	double lw = GetLineWidth () / 2.;
64 	double result = G_MAXDOUBLE, d, dx, dy, dx1, dy1, xx, yy, length;
65 	// we do not take miter limits into account
66 	if (item)
67 		*item = const_cast <PolyLine *> (this);
68 	for (i++; i != end; i++) {
69 		x1 = (*i).x;
70 		y1 = (*i).y;
71 		dx = x1 - x0;
72 		dy = y1 - y0;
73 		dx1 = x - x0;
74 		dy1 = y - y0;
75 		length = sqrt (dx * dx  + dy * dy);
76 		if (length == 0.)
77 			d = sqrt (dx1 * dx1 + dy1 * dy1);
78 		else {
79 			xx = (dx1 * dx + dy1 * dy) / length;
80 			yy = (dx1 * dy - dy1 * dx) / length;
81 			if (xx < 0.) {
82 				if (fabs (yy) < lw)
83 					d = fabs (xx);
84 				else {
85 					yy = fabs (yy) - lw;
86 					d = sqrt (xx * xx + yy * yy);
87 				}
88 			} else if (xx > length) {
89 				xx -= length;
90 				if (fabs (yy) < lw)
91 					d = fabs (xx);
92 				else {
93 					yy = fabs (yy) - lw;
94 					d = sqrt (xx * xx + yy * yy);
95 				}
96 			} else {
97 				if (fabs (yy) <= lw)
98 					return 0;
99 				else
100 					d = fabs (yy) - lw;
101 			}
102 		}
103 		if (d < result)
104 			result = d;
105 		x0 = x1;
106 		y0 = y1;
107 	}
108 	return result;
109 }
110 
Draw(cairo_t * cr,G_GNUC_UNUSED bool is_vector) const111 void PolyLine::Draw (cairo_t *cr, G_GNUC_UNUSED bool is_vector) const
112 {
113 	GOColor color = GetEffectiveLineColor ();
114 	if (color != 0) {
115 		cairo_set_line_width (cr, GetLineWidth ());
116 		list <Point>::const_iterator i = m_Points.begin (), end = m_Points.end ();
117 		if (i == end)
118 			return;
119 		cairo_move_to (cr, (*i).x, (*i).y);
120 		for (i++; i != end; i++)
121 			cairo_line_to (cr, (*i).x, (*i).y);
122 		cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
123 		cairo_set_line_join (cr, CAIRO_LINE_JOIN_MITER);
124 		cairo_set_miter_limit (cr, 10.);
125 		cairo_set_source_rgba (cr, GO_COLOR_TO_CAIRO (color));
126 		cairo_stroke (cr);
127 	}
128 }
129 
UpdateBounds()130 void PolyLine::UpdateBounds ()
131 {
132 	// This code might be off by a little thing because of miter limits.
133 	list <Point>::const_iterator i = m_Points.begin (), end = m_Points.end ();
134 	if (i == end)
135 		return;
136 	m_x0 = m_x1 = (*i).x;
137 	m_y0 = m_y1 = (*i).y;
138 	for (i++; i != end; i++) {
139 		if ((*i).x < m_x0)
140 			m_x0 = (*i).x;
141 		else if ((*i).x > m_x1)
142 			m_x1 = (*i).x;
143 		if ((*i).y < m_y0)
144 			m_y0 = (*i).y;
145 		else if ((*i).y > m_y1)
146 			m_y1 = (*i).y;
147 	}
148 	double lw = GetLineWidth () / 2.;
149 	m_x0 -= lw;
150 	m_x1 += lw;
151 	m_y0 -= lw;
152 	m_y1 += lw;
153 	Item::UpdateBounds ();
154 }
155 
Move(double x,double y)156 void PolyLine::Move (double x, double y)
157 {
158 	Invalidate ();
159 	list <Point>::iterator i, end = m_Points.end ();
160 	for (i = m_Points.begin (); i != end; i++) {
161 		(*i).x += x;
162 		(*i).y += y;
163 	}
164 	BoundsChanged ();
165 	Invalidate ();
166 }
167 
168 }
169