App/Chart/Gtk2/Ex/LineClipper.pm untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 59 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 298 CC 28 SCAR 57.9
CC
28
Cov
0%
clipped_draw_lines
552.0
new
12.0
end
12.0
CRAP 328.1
1 # wrong angle on coming back after multiple skipped off screen, Kirshenbaum BHP
2  
3  
4  
5 # x1,y1 x2,inf -> x1,y1
6  
7 # x1,-inf x2,inf -> mean x1,x2
8  
9 # x1,y1 inf,inf -> 45deg to corner
10  
11 # x1,-inf inf,inf -> mean is inf, offscreen
12  
13  
14 # segments drawing with clip, for annotation lines
15  
16  
17  
18  
19 # Copyright 2007, 2008, 2009, 2010 Kevin Ryde
20  
21 # This file is part of Chart.
22 #
23 # Chart is free software; you can redistribute it and/or
24 # modify it under the terms of the GNU General Public License as published
25 # by the Free Software Foundation; either version 3, or (at your option) any
26 # later version.
27 #
28 # Chart is distributed in the hope that it will be useful,
29 # but WITHOUT ANY WARRANTY; without even the implied warranty of
30 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
31 # Public License for more details.
32 #
33 # You should have received a copy of the GNU General Public License along
34 # with Chart. If not, see <http://www.gnu.org/licenses/>.
35  
36 package App::Chart::Gtk2::Ex::LineClipper;
37 use 5.008;
38 use strict;
39 use warnings;
40 use Carp;
41 use Gtk2;
42 use List::Util qw(min max);
43  
44 use constant DEBUG => 0;
45  
46 sub new {
47 my ($class, %self) = @_;
48  
49 if (! exists $self{'drawable'}) {
50 croak "Missing drawable for LineClipper";
51 }
52  
53 # default yes for draw_points
54 if (! exists $self{'draw_points'}) {
55 $self{'draw_points'} = 1;
56 }
57  
58 $self{'points'} = [];
59 return bless \%self, $class;
60 }
61  
62 sub add {
63 my ($self, $gc, $x, $y) = @_;
64 my $points = $self->{'points'};
65  
66 # if the next segment is a new gc and there's more than a single point,
67 # then draw the previous segments with the previous gc
68 if (@$points > 2 && $gc != $self->{'gc'}) {
69 my $x1 = $points->[-2];
70 my $y1 = $points->[-1];
71 $self->end;
72 @$points = ($x1, $y1, $x, $y);
73 } else {
74 push @$points, $x, $y;
75 }
76 $self->{'gc'} = $gc;
77 }
78  
79 sub end {
80 my ($self) = @_;
81  
82 my $points = $self->{'points'};
83 if (! @$points) {
84 # nothing to draw at all
85 return
86 }
87  
88 if (@$points == 2 && ! $self->{'draw_point'}) {
89 # only a single point, and single point drawing not wanted
90 @$points = ();
91 return;
92 }
93  
94 clipped_draw_lines ($self->{'drawable'},
95 $self->{'gc'},
96 max (1, $self->{'line_width'} || 1),
97 $points);
98 @$points = ();
99 }
100  
101 sub clipped_draw_lines {
102 my ($drawable, $gc, $line_width, $points) = @_;
103  
104 my ($width, $height) = $drawable->get_size;
105 my $clip_x_low = 0 - $line_width;
106 my $clip_x_high = $width + $line_width;
107 my $clip_y_low = 0 - $line_width;
108 my $clip_y_high = $height + $line_width;
109  
110 if (@$points == 2) {
111 # single point
112 my $x = $points->[0];
113 my $y = $points->[1];
114 if (DEBUG) { print "point $x,$y $line_width\n"; }
115 if ($x < $clip_x_low || $x > $clip_x_high
116 || $y < $clip_y_low || $y > $clip_y_high) {
117 # outside the drawable, do nothing
118 return;
119 }
120  
121 if ($line_width <= 1) {
122 $drawable->draw_point ($gc, $x, $y);
123 } else {
124 my $offset = int ($line_width / 2); # round down
125 $drawable->draw_arc ($gc, 1,
126 $x - $offset, $y - $offset,
127 $line_width, $line_width,
128 0, 23040); # 360*64 for full circle
129 }
130 return;
131 }
132  
133 if (DEBUG) { print "draw $gc ", scalar(@$points), " coords\n"; }
134  
135 my @draw = ();
136 my $x1 = $points->[0];
137 my $y1 = $points->[1];
138  
139 for (my $i = 2; $i+1 <= $#$points; $i += 2) {
140 my $x2 = $points->[$i];
141 my $y2 = $points->[$i+1];
142  
143 if ( ($x1 <= $clip_x_low && $x2 <= $clip_x_low)
144 || ($x1 >= $clip_x_high && $x2 <= $clip_x_high)) {
145 if (DEBUG) { print " X coords outside drawable, skip: $x1 $x2\n"; }
146 $x1 = $x2;
147 $y1 = $y2;
148 next;
149 }
150  
151 my $end_clipped = 0;
152  
153 if ($x1 <= $clip_x_low && $x2 > $clip_x_low) {
154 $y1 = ($y1 * ($x2 - $clip_x_low) + $y2 * ($clip_x_low - $x1))
155 / ($x2 - $x1);
156 $x1 = $clip_x_low;
157 } elsif ($x2 <= $clip_x_low && $x1 > $clip_x_low) {
158 $y2 = ($y2 * ($x1 - $clip_x_low) + $y1 * ($clip_x_low - $x2))
159 / ($x1 - $x2);
160 $x2 = $clip_x_low;
161 $end_clipped = 1;
162 }
163  
164 if ($x1 <= $clip_x_high && $x2 > $clip_x_high) {
165 $y2 = ($y2 * ($x1 - $clip_x_high) + $y1 * ($clip_x_high - $x2))
166 / ($x1 - $x2);
167 $x2 = $clip_x_high;
168 $end_clipped = 1;
169 } elsif ($x2 <= $clip_x_high && $x1 > $clip_x_high) {
170 $y1 = ($y1 * ($x2 - $clip_x_high) + $y2 * ($clip_x_high - $x1))
171 / ($x2 - $x1);
172 $x1 = $clip_x_high;
173 }
174  
175 if ( ($y1 <= $clip_y_low && $y2 <= $clip_y_low)
176 || ($y1 >= $clip_y_high && $y2 >= $clip_y_high)) {
177 if (DEBUG) { print " Y coords outside drawable, skip: $y1 $y2\n"; }
178 $x1 = $x2;
179 $y1 = $y2;
180 next;
181 }
182  
183 if ($y1 <= $clip_y_low && $y2 > $clip_y_low) {
184 $x1 = ($x1 * ($y2 - $clip_y_low) + $x2 * ($clip_y_low - $y1))
185 / ($y2 - $y1);
186 $y1 = $clip_y_low;
187 } elsif ($y2 <= $clip_y_low && $y1 > $clip_y_low) {
188 $x2 = ($x2 * ($y2 - $clip_y_low) + $x1 * ($clip_y_low - $y1))
189 / ($y2 - $y1);
190 $y2 = $clip_y_low;
191 $end_clipped = 1;
192 }
193  
194 if ($y1 <= $clip_y_high && $y2 > $clip_y_high) {
195 $x2 = ($x2 * ($y2 - $clip_y_high) + $x1 * ($clip_y_high - $y1))
196 / ($y2 - $y1);
197 $y2 = $clip_y_high;
198 $end_clipped = 1;
199 } elsif ($y2 <= $clip_y_high && $y1 > $clip_y_high) {
200 $x1 = ($x1 * ($y2 - $clip_y_high) + $x2 * ($clip_y_high - $y1))
201 / ($y2 - $y1);
202 $y1 = $clip_y_high;
203 }
204  
205 if (! @draw) {
206 push @draw, $x1, $y1;
207 }
208 push @draw, $x2, $y2;
209 if ($end_clipped) {
210 if (DEBUG) { print " end clipped, draw ", join(' ', @draw), "\n"; }
211 $drawable->draw_lines ($gc, @draw);
212 @draw = ();
213 }
214 $x1 = $x2;
215 $y1 = $y2;
216 }
217 if (DEBUG) { print " final draw ", join(' ', @draw), "\n"; }
218 if (@draw) {
219 $drawable->draw_lines ($gc, @draw);
220 }
221 }
222  
223 sub DESTROY {
224 my ($self) = @_;
225 $self->end;
226 }
227  
228  
229  
230 1;
231 __END__