-
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathCircle.pm
More file actions
217 lines (173 loc) · 5.1 KB
/
Copy pathCircle.pm
File metadata and controls
217 lines (173 loc) · 5.1 KB
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
=head1 NAME
Circle
=head1 SYNOPSIS
use Carp;
use GD;
use WWPlot;
use Fun;
=head1 DESCRIPTION
This module defines a circle which can be inserted as a stamp in a graph (WWPlot) object.
=head2 Command:
$circle_object = new Circle( $center_pos_x, $center_pos_y, $radius, $border_color, $fill_color);
=head2 Examples:
Here is the code used to define the subroutines open_circle
and closed_circle in PGgraphmacros.pl
sub open_circle {
my ($cx,$cy,$color) = @_;
new Circle ($cx, $cy, 4,$color,'nearwhite');
}
sub closed_circle {
my ($cx,$cy, $color) = @_;
$color = 'black' unless defined $color;
new Circle ($cx, $cy, 4,$color, $color);
}
$circle_object2 = closed_circle( $x_position, $y_position, $color );
@circle_objects = $graph -> stamps($circle_object2);
# puts a filled dot at ($x_position, $y_position) on the graph -- using real world coordinates.
=cut
package Circle;
use strict;
#use WWPlot;
#Because of the way problem modules are loaded 'use' is disabled.
#use strict;
@Circle::ISA = qw(WWPlot);
# my %fields =(
# colors => {},
# border_color => 0,
# fill_color => 1,
# radius => 8,
# );
sub new {
my $class = shift;
my $cx = shift;
my $cy = shift;
my $radius = shift; # radius is in pixels, others are in real world coordinates
my $border_color = shift;
my $fill_color = shift;
$radius = 4 unless defined $radius;
$border_color = 'black' unless defined($border_color);
$fill_color = 'black' unless defined($fill_color);
my $self = {
im => new GD::Image(2 * $radius, 2 * $radius),
cx => $cx,
cy => $cy,
radius => $radius,
border_color => $border_color,
fill_color => $fill_color,
};
bless $self, $class;
$self->_initialize_colors;
if (defined($self->{'colors'}{$border_color})) {
$self->{'border_color'} = $self->{'colors'}{$border_color};
} else {
$self->{'border_color'} = 'default_color';
}
if (defined($self->{'colors'}{$fill_color})) {
$self->{'fill_color'} = $self->{'colors'}{$fill_color};
} else {
$self->{'fill_color'} = 'nearwhite';
}
$self->im->transparent($self->{'colors'}{'background_color'});
$self->im->arc($radius, $radius, 2 * $radius, 2 * $radius, 0, 360, $self->{'border_color'});
$self->im->fill($radius, $radius, $self->{'fill_color'});
return $self;
}
sub _initialize_colors {
my $self = shift;
# allocate some colors
$self->{'colors'}{'background_color'} = $self->im->colorAllocate(255, 255, 255);
$self->{'colors'}{'default_color'} = $self->im->colorAllocate(0, 0, 0);
$self->{'colors'}{'white'} = $self->im->colorAllocate(255, 255, 255);
$self->{'colors'}{'black'} = $self->im->colorAllocate(0, 0, 0);
$self->{'colors'}{'red'} = $self->im->colorAllocate(255, 0, 0);
$self->{'colors'}{'green'} = $self->im->colorAllocate(0, 255, 0);
$self->{'colors'}{'blue'} = $self->im->colorAllocate(0, 0, 255);
$self->{'colors'}{'yellow'} = $self->im->colorAllocate(255, 255, 0);
$self->{'colors'}{'orange'} = $self->im->colorAllocate(255, 100, 0);
$self->{'colors'}{'gray'} = $self->im->colorAllocate(180, 180, 180);
$self->{'colors'}{'nearwhite'} = $self->im->colorAllocate(254, 254, 254);
}
##########################
# Access methods -- Get only??? should this be changed?
##########################
sub size {
my $s = shift;
(2 * $s->{radius}, 2 * $s->{radius});
}
sub height {
my $s = shift;
2 * $s->{radius};
}
sub width {
my $s = shift;
2 * $s->{radius};
}
sub radius {
my $s = shift;
$s->{radius};
}
sub x {
my $s = shift;
$s->{cx};
}
sub y {
my $s = shift;
$s->{cy};
}
sub image {
my $s = shift;
$s->{im};
}
##########################
# Access methods -- Get and Set
##########################
sub colors {
my $self = shift;
my $type = ref($self) || die "$self is not an object";
unless (exists $self->{colors}) {
die "Can't find colors field in object of class $type";
}
if (@_) {
return $self->{colors} = shift;
} else {
return $self->{colors};
}
}
sub border_color {
my $self = shift;
my $type = ref($self) || die "$self is not an object";
unless (exists $self->{border_color}) {
die "Can't find border_color field in object of class $type";
}
if (@_) {
return $self->{border_color} = shift;
} else {
return $self->{border_color};
}
}
sub fill_color {
my $self = shift;
my $type = ref($self) || die "$self is not an object";
unless (exists $self->{fill_color}) {
die "Can't find fill_color field in object of class $type";
}
if (@_) {
return $self->{fill_color} = shift;
} else {
return $self->{fill_color};
}
}
sub draw {
my $self = shift;
my $g = shift; # the enclosing graph object
my $x = $self->x;
my $y = $self->y;
my $image = $self->image;
my $height = $self->height;
my $width = $self->width;
$g->im->copy($image, ($g->ii($x)) - int($width / 2), ($g->jj($y)) - int($height / 2), 0, 0, $width, $height);
}
sub DESTROY {
# doing nothing about destruction, hope that isn't dangerous
}
1;