File: test11.p

package info (click to toggle)
libpgplot-perl 1%3A2.21-6
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 316 kB
  • ctags: 31
  • sloc: perl: 679; ansic: 453; makefile: 7
file content (144 lines) | stat: -rwxr-xr-x 3,299 bytes parent folder | download | duplicates (11)
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
#!/usr/local/bin/perl

use PGPLOT;

print "\n\nTesting Object-Oriented stuff...\n\n";

print "PGPLOT module version $PGPLOT::VERSION\n\n";

pgqinf("VERSION",$val,$len);
print "PGPLOT $val library\n\n";

$dev = "?" unless defined $dev; # "?" will prompt for device
pgbegin 9,$dev,1,1; pgwnad -100,100,-100,100;
pgpage; pgbox 'BC',0,0,'BC',0,0;

# Define some object classes

##############################################################   

package Square;

use PGPLOT;  # Square class needs PGPLOT

# Create a new Square - colour is first argument

sub new {
   my $type = shift; # Ignore as we know we are a Square;
   my $self = {};    # $self is ref to anonymous hash
   my $colour = shift;
   $colour = 2 unless defined($colour);            # Default is red
   $self->{'Colour'}=$colour;                      
   $self->{'Xvertices'} = [-10, 10, 10,-10, -10];  # Initialise as square
   $self->{'Yvertices'} = [-10,-10, 10, 10, -10];
   bless $self;
}

# Method to plot a Square object at $x,$y

sub plot {   
   my $self = shift;
   my($x,$y) = @_;
   my(@xpts) = @{$self->{'Xvertices'}};
   my(@ypts) = @{$self->{'Yvertices'}};

   for (@xpts) { $_ = $_ + $x }
   for (@ypts) { $_ = $_ + $y }

   pgsci($self->{'Colour'});  pgpoly(scalar(@xpts), \@xpts, \@ypts);
   pgsci(1);                  pgline(scalar(@xpts), \@xpts, \@ypts);
}

# Method to expand a Square object 

sub expand {   
   my $self = shift;
   my $fac  = shift;
   my $xpts  = $self->{'Xvertices'};
   my $ypts  = $self->{'Yvertices'};

   for (@$xpts) { $_ = $_ * $fac }
   for (@$ypts) { $_ = $_ * $fac }
}

   
# Method to rotate a Square object 

sub rotate {   
   my $self  = shift;
   my $angle = (shift)*(3.141592564/180);
   my $x = $self->{'Xvertices'};
   my $y = $self->{'Yvertices'};
   my ($x2,$y2);

   for($i=0; $i<=$#{$x}; $i++) {
     $x2 =  $$x[$i]*cos($angle) + $$y[$i]*sin($angle);
     $y2 = -$$x[$i]*sin($angle) + $$y[$i]*cos($angle);
     $$x[$i] = $x2; $$y[$i] = $y2;
   }
}


##############################################################   

package Triangle;

# Only difference is "new" method. Otherwise inherit
# all other properties from "Square";

@ISA = qw( Square );

# Create a new Triangle

sub new {
   my $type = shift; # Ignore as we know we are a Square;
   my $self = {};    # $self is ref to anonymous hash
   my $colour = shift;
   $colour = 3 unless defined($colour);       # Default is green
   $self->{'Colour'}=$colour;                      
   $self->{'Xvertices'} = [-10, 10, 0, -10];  # Initialise as square
   $self->{'Yvertices'} = [-10,-10, 5, -10];
   bless $self;
}

##############################################################   


# Now let's use these objects

package main;

print "\nTesting Square Objects...\n";

$shape1 = new Square;

# Plot first shape at 50,50;

print "Square plot method...\n";

$shape1->plot(50,50);

print "Square expand and rotate methods...\n";

$shape1->expand(2.3); # Make the shape bigger
$shape1->rotate(20);  # Rotate the shape bigger

$shape1->plot(-20,-50);

print "Inheriting Square methods in Triangles...\n";

$shape2 = new Triangle;

$shape2->plot(-20,50);

$shape3 = new Triangle(4);  # Blue triangle
$shape3->rotate(-15); $shape3->expand(1.5);

$shape3->plot(50,-50);

print "Fun isn't it?\n";

$len=1; # -w fudge

pgend;