aboutsummaryrefslogtreecommitdiffstats
path: root/GD/Arrow.pm
blob: 1030eb80ed670b64142589f6b07c4374f11db189 (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package GD::Arrow;
# $Id: Arrow.pm,v 1.7 2004/10/25 17:22:27 tcaine Exp $

use strict;
use warnings;
use vars qw( $VERSION @ISA );
use GD;

$VERSION = '0.01';
@ISA = qw( GD::Polygon );

sub x1    { shift->{X1}    }
sub y1    { shift->{Y1}    }
sub x2    { shift->{X2}    }
sub y2    { shift->{Y2}    }
sub width { shift->{WIDTH} }

package GD::Arrow::Full;

use strict;
use warnings;
use vars qw( $VERSION @ISA );
use Carp;
use GD;

$VERSION = '0.01';
@ISA = qw( GD::Arrow );

sub new {
    my $class = shift;
    my %arg = @_;
    my ($x1, $y1, $x2, $y2, $width);
    my $self = $class->SUPER::new();

    foreach ( keys %arg ) {
        if    (/^-?X1$/i)    { $self->{X1} = $x1 = $arg{$_} }
        elsif (/^-?Y1$/i)    { $self->{Y1} = $y1 = $arg{$_} }
        elsif (/^-?X2$/i)    { $self->{X2} = $x2 = $arg{$_} }
        elsif (/^-?Y2$/i)    { $self->{Y2} = $y2 = $arg{$_} }
        elsif (/^-?WIDTH$/i) { $self->{WIDTH} = $width = $arg{$_} }
    }

    $self->{WIDTH} = $width = 6 if !defined($self->{WIDTH});

    croak "" . __PACKAGE__ . "->new() requires 4 named parameters"
        if !defined($self->{X1}) || 
           !defined($self->{Y1}) ||
           !defined($self->{X2}) ||
           !defined($self->{Y2});

    my $double_width = $width * 2;
    my $theta = atan2($y1-$y2,$x1-$x2);

    $self->addPt(
        sprintf('%.0f', $x2+$width*sin($theta)),
        sprintf('%.0f', $y2-$width*cos($theta))
    );
    $self->addPt(
        sprintf('%.0f', $x2-$width*sin($theta)),
        sprintf('%.0f', $y2+$width*cos($theta))
    );
    $self->addPt(
        sprintf('%.0f', $x1-$width*sin($theta)-$double_width*cos($theta)),
        sprintf('%.0f', $y1-$double_width*sin($theta)+$width*cos($theta))
    );
    $self->addPt(
        sprintf('%.0f', $x1-$double_width*sin($theta)-$double_width*cos($theta)),
        sprintf('%.0f', $y1-$double_width*sin($theta)+$double_width*cos($theta))
    );
    $self->addPt($x1,$y1);
    $self->addPt(
        sprintf('%.0f', $x1+$double_width*(sin($theta)-cos($theta))),
        sprintf('%.0f', $y1+$double_width*(-sin($theta)-cos($theta)))
    );
    $self->addPt(
        sprintf('%.0f', $x1+$width*sin($theta)-$double_width*cos($theta)),
        sprintf('%.0f', $y1-$double_width*sin($theta)-$width*cos($theta))
    );

    return $self; 
}

package GD::Arrow::LeftHalf;

use strict;
use warnings;
use vars qw( $VERSION @ISA );
use Carp;
use GD;

$VERSION = '0.01';
@ISA = qw( GD::Arrow );

sub new {
    my $class = shift;
    my %arg = @_;
    my ($x1, $y1, $x2, $y2, $width);
    my $self = $class->SUPER::new();

    foreach ( keys %arg ) {
        if    (/^-?X1$/i)    { $self->{X1} = $x1 = $arg{$_} }
        elsif (/^-?Y1$/i)    { $self->{Y1} = $y1 = $arg{$_} }
        elsif (/^-?X2$/i)    { $self->{X2} = $x2 = $arg{$_} }
        elsif (/^-?Y2$/i)    { $self->{Y2} = $y2 = $arg{$_} }
        elsif (/^-?WIDTH$/i) { $self->{WIDTH} = $width = $arg{$_} }
    }

    $self->{WIDTH} = $width = 6 if !defined($self->{WIDTH});

    croak "" . __PACKAGE__ . "->new() requires 4 named parameters"
        if !defined($self->{X1}) || 
           !defined($self->{Y1}) ||
           !defined($self->{X2}) ||
           !defined($self->{Y2});

    my $double_width = $width * 2;
    my $theta = atan2($y1-$y2,$x1-$x2);

    $self->addPt($x2, $y2);
    $self->addPt(
        sprintf('%.0f', $x2+$width*sin($theta)),
        sprintf('%.0f', $y2-$width*cos($theta))
    );
    $self->addPt(
        sprintf('%.0f', $x1+$width*sin($theta)-$double_width*cos($theta)),
        sprintf('%.0f', $y1-$double_width*sin($theta)-$width*cos($theta))
    );
    $self->addPt(
        sprintf('%.0f', $x1+$double_width*(sin($theta)-cos($theta))),
        sprintf('%.0f', $y1+$double_width*(-sin($theta)-cos($theta)))
    );
    $self->addPt($x1,$y1);

    return $self;
}

package GD::Arrow::RightHalf;

use strict;
use warnings;
use vars qw( $VERSION @ISA );
use Carp;
use GD;

$VERSION = '0.01';
@ISA = qw( GD::Arrow );

sub new {
    my $class = shift;
    my %arg = @_;
    my ($x1, $y1, $x2, $y2, $width);
    my $self = $class->SUPER::new();

    foreach ( keys %arg ) {
        if    (/^-?X1$/i)    { $self->{X1} = $x1 = $arg{$_} }
        elsif (/^-?Y1$/i)    { $self->{Y1} = $y1 = $arg{$_} }
        elsif (/^-?X2$/i)    { $self->{X2} = $x2 = $arg{$_} }
        elsif (/^-?Y2$/i)    { $self->{Y2} = $y2 = $arg{$_} }
        elsif (/^-?WIDTH$/i) { $self->{WIDTH} = $width = $arg{$_} }
    }

    $self->{WIDTH} = $width = 6 if !defined($self->{WIDTH});

    croak "" . __PACKAGE__ . "->new() requires 4 named parameters"
        if !defined($self->{X1}) || 
           !defined($self->{Y1}) ||
           !defined($self->{X2}) ||
           !defined($self->{Y2});

    my $double_width = $width * 2;
    my $theta = atan2($y1-$y2,$x1-$x2);

    $self->addPt($x2, $y2);
    $self->addPt(
        sprintf('%.0f', $x2-$width*sin($theta)),
        sprintf('%.0f', $y2+$width*cos($theta))
    );
    $self->addPt(
        sprintf('%.0f', $x1-$width*sin($theta)-$double_width*cos($theta)),
        sprintf('%.0f', $y1-$double_width*sin($theta)+$width*cos($theta))
    );
    $self->addPt(
        sprintf('%.0f', $x1-$double_width*sin($theta)-$double_width*cos($theta)),
        sprintf('%.0f', $y1-$double_width*sin($theta)+$double_width*cos($theta))
    );
    $self->addPt($x1,$y1);

    return $self;
}


1;
__END__

=head1 NAME

GD::Arrow - draw arrows using GD

=head1 SYNOPSIS

  use GD;
  use GD::Arrow;

  my $width = 8;
  my ($x1, $y1) = (100, 10);
  my ($x2, $y2) = (100, 190);
  my ($x3, $y3) = (10, 30);
  my ($x4, $y4) = (190, 75);

  my $arrow = GD::Arrow::Full->new( 
                  -X1    => $x1, 
                  -Y1    => $y1, 
                  -X2    => $x2, 
                  -Y2    => $y2, 
                  -WIDTH => $width,
              );

  my $image = GD::Image->new(200, 200);
  my $white = $image->colorAllocate(255, 255, 255);
  my $black = $image->colorAllocate(0, 0, 0);
  my $blue = $image->colorAllocate(0, 0, 255);
  my $yellow = $image->colorAllocate(255, 255, 0);
  $image->transparent($white);

  $image->filledPolygon($arrow,$blue);
  $image->polygon($arrow,$black);

  my $half_arrow_1 = GD::Arrow::LeftHalf->new( 
                         -X1    => $x3, 
                         -Y1    => $y3, 
                         -X2    => $x4, 
                         -Y2    => $y4, 
                         -WIDTH => $width,
                     );

  my $half_arrow_2 = GD::Arrow::LeftHalf->new( 
                         -X1    => $x4, 
                         -Y1    => $y4, 
                         -X2    => $x3, 
                         -Y2    => $y3, 
                         -WIDTH => $width 
                     );

  $image->filledPolygon($half_arrow_1,$blue);
  $image->polygon($half_arrow_1,$black);

  $image->filledPolygon($half_arrow_2,$yellow);
  $image->polygon($half_arrow_2,$black);

  open IMAGE, "> image.png" or die $!;
  binmode(IMAGE, ":raw");
  print IMAGE $image->png;
  close IMAGE;

  exit(0);

=head1 DESCRIPTION

This is a subclass of GD::Polygon used to draw an arrow between two vertices.

GD::Arrow::Full draws a full arrow between two verticies.

                                  |\
           +----------------------+ \
  (X2, Y2) *                         * (X1, Y1)
           +----------------------+ /
                                  |/

GD::Arrow::RightHalf draws a half arrow between two verticies.

  (X2, Y2) *-------------------------* (X1, Y1)
           +----------------------+ /
                                  |/

GD::Arrow::LeftHalf draws a half arrow between two verticies.

                                  |\
           +----------------------+ \
  (X2, Y2) *-------------------------* (X1, Y1)

=head1 SEE ALSO

GD::Polygon

=head1 CREDITS

The equations used to determine the critical verticies to represent a GD::Arrow was based on Hideki Ono's makefeedmap software.  Makefeedmap can be found at http://www.ono.org/software/makefeedmap/.

=head1 AUTHOR

Todd Caine, E<lt>todd@pobox.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright 2004 by Todd Caine

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut