1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
package SVG::Graph::Glyph::bezier;
use base SVG::Graph::Glyph;
use strict;
use Math::Spline;
=head2 draw
Title : draw
Usage :
Function:
Example :
Returns :
Args :
=cut
sub draw{
my ($self,@args) = @_;
my $id = 'n'.sprintf("%07d",int(rand(9999999)));
my $group = $self->svg->group(id=>"bezier$id");
my $xscale = $self->xsize / $self->group->xrange;
my $yscale = $self->ysize / $self->group->yrange;
my @p = map {[$_->x,$_->y]} sort {$a->x <=> $b->x} $self->group->data;
my $c = spline_generate(@p);
my @c;
foreach my $i (1..$#p){
my($x1,$y1) = @{$p[$i-1]};
my($x2,$y2) = @{$p[$i] };
my $e = 0;
my($cx,$cy,$cx_sum,$cy_sum);
for(my $d = 2; $d <= 100 ; $d += 2){
next if $d == 0;
my $delta = ($x2 - $x1) / $d;
my $y1d = spline_evaluate($x1+$delta,$c,@p);
my $y2d = spline_evaluate($x2-$delta,$c,@p);
my($int,$t1,$t2) = Intersection(Segment([$x1,$y1],[$x1+$delta,$y1d]),Segment([$x2-$delta,$y2d],[$x2,$y2]));
next unless ref($int);
next unless $x1 <= $int->[0] and $x2 >= $int->[0];
$e++;
$cx_sum += $int->[0];
$cy_sum += $int->[1];
}
$x1 = (($x1 - $self->group->xmin) * $xscale) + $self->xoffset;
$y1 = ($self->ysize - ($y1 - $self->group->ymin) * $yscale) + $self->yoffset;
$x2 = (($x2 - $self->group->xmin) * $xscale) + $self->xoffset;
$y2 = ($self->ysize - ($y2 - $self->group->ymin) * $yscale) + $self->yoffset;
#warn $cx_sum,"\t",$e;
#next unless $e;
if($e){
$cx = $cx_sum / $e;
$cy = $cy_sum / $e;
$cx = (($cx - $self->group->xmin) * $xscale) + $self->xoffset;
$cy = ($self->ysize - ($cy - $self->group->ymin) * $yscale) + $self->yoffset;
$group->path(d=>"M$x1,$y1 Q$cx,$cy $x2,$y2",
style=>{$self->_style},
);
} else {
$group->line(x1=>$x1,y1=>$y1,x2=>$x2,y2=>$y2,style=>{$self->_style});
}
}
}
=head2 spline_generate
Title : spline_generate
Usage :
Function:
Example :
Returns :
Args :
=cut
sub spline_generate{
my (@points) = @_;
my ($i, $delta, $temp, @factors, @coeffs);
$coeffs[0] = $factors[0] = 0;
# Decomposition phase of the tridiagonal system of equations
for ($i = 1; $i < @points - 1; $i++) {
#ad
next unless ($points[$i+1][0] - $points[$i-1][0]);
$delta = ($points[$i][0] - $points[$i-1][0]) / (($points[$i+1][0] - $points[$i-1][0]));
$temp = $delta * ($coeffs[$i-1] || 0) + 2;
$coeffs[$i] = ($delta - 1) / @points;
#ad
next unless ($points[$i+1][0] - $points[$i][0]);
next unless ($points[$i][0] - $points[$i-1][0]);
$factors[$i] = ($points[$i+1][1] - $points[$i][1]) / (($points[$i+1][0] - $points[$i][0]))
-
($points[$i][1] - $points[$i-1][1]) / (($points[$i][0] - $points[$i-1][0]));
$factors[$i] = ( 6 * $factors[$i] / (($points[$i+1][0] - $points[$i-1][0]))
-
$delta * $factors[$i-1] ) / $temp;
}
# Backsubstitution phase of the tridiagonal system
#
$coeffs[$#points] = 0;
for ($i = @points - 2; $i >= 0; $i--) {
$coeffs[$i] = ($coeffs[$i] || 0) * ($coeffs[$i+1] || 0) + ($factors[$i] || 0);
}
return \@coeffs;
}
=head2 spline_evaluate
Title : spline_evaluate
Usage :
Function:
Example :
Returns :
Args :
=cut
sub spline_evaluate{
my ($x, $coeffs, @points) = @_;
my ($i, $delta, $mult);
# Which section of the spline are we in?
#
for ($i = @points - 2; $i >= 1; $i--) {
last if $x >= $points[$i][0];
}
$delta = $points[$i+1][0] - $points[$i][0];
#ad
return 0 unless $delta;
$mult = ( $coeffs->[$i]/2 ) + ($x - $points[$i][0]) * ($coeffs->[$i+1] - $coeffs->[$i]) / (6 * $delta);
$mult *= $x - $points[$i][0];
$mult += ($points[$i+1][1] - $points[$i][1]) / ($delta);
$mult -= ($coeffs->[$i+1] + 2 * $coeffs->[$i]) * $delta / 6;
return $points[$i][1] + $mult * ($x - $points[$i][0]);
}
=head2 Segment
Title : Segment
Usage :
Function:
Example :
Returns :
Args :
=cut
sub Segment {
[ $_[0], $_[1] ];
}
=head2 VectorSum
Title : VectorSum
Usage :
Function:
Example :
Returns :
Args :
=cut
sub VectorSum {
my ($x, $y) = (0, 0);
for (@_) {
$x += $_->[0];
$y += $_->[1];
}
[ $x, $y ];
}
=head2 ScalarProd
Title : ScalarProd
Usage :
Function:
Example :
Returns :
Args :
=cut
sub ScalarProd {
my $s = shift;
my $v = shift;
[ $v->[0] * $s, $v->[1] * $s ];
}
=head2 Minus
Title : Minus
Usage :
Function:
Example :
Returns :
Args :
=cut
sub Minus {
my $v = shift;
[ - $v->[0], - $v->[1] ];
}
=head2 Intersection
Title : Intersection
Usage :
Function:
Example :
Returns :
Args :
=cut
sub Intersection {
my $l1 = shift;
my $l2 = shift;
my ($bp1, $op1) = @$l1;
my ($bp2, $op2) = @$l2;
my $v1 = VectorSum($op1, Minus($bp1));
my $v2 = VectorSum($op2, Minus($bp2));
my ($x1, $y1) = @$v1;
my ($x2, $y2) = @$v2;
my $DEN = $x1 * $y2 - $x2 * $y1;
# Lines are parallel.
return undef if $DEN == 0;
my ($bx1, $by1) = @$bp1;
my ($bx2, $by2) = @$bp2;
my $t1 = (($bx2 - $bx1) * $y2 - ($by2 - $by1) * $x2) / $DEN;
my $t2 = (($bx2 - $bx1) * $y1 - ($by2 - $by1) * $x1) / $DEN;
my $RESULT = VectorSum($bp1, ScalarProd($t1, $v1));
($RESULT, $t1, $t2);
}
1;
|