| 1 | use strict; use warnings; |
| 2 | use Test::More; |
| 3 | use Config; |
| 4 | # Stop f77-linking causing spurious undefined symbols (alpha) |
| 5 | $ENV{'PERL_DL_NONLAZY'}=0 if $Config{'osname'} eq "dec_osf"; |
| 6 | require PGPLOT; |
| 7 | |
| 8 | my $dev = $ENV{PGPLOT_DEV} || '/NULL'; |
| 9 | |
| 10 | $ENV{PGPLOT_XW_WIDTH}=0.3; |
| 11 | |
| 12 | note "Testing Object-Oriented stuff"; |
| 13 | |
| 14 | PGPLOT::pgbegin(9,$dev,1,1); PGPLOT::pgwnad(-100,100,-100,100); |
| 15 | PGPLOT::pgpage(); PGPLOT::pgbox('BC',0,0,'BC',0,0); |
| 16 | |
| 17 | # Define some object classes |
| 18 | |
| 19 | ############################################################## |
| 20 | |
| 21 | package Square; |
| 22 | |
| 23 | # Create a new Square - colour is first argument |
| 24 | |
| 25 | sub new { |
| 26 | my $type = shift; # Ignore as we know we are a Square; |
| 27 | my $self = {}; # $self is ref to anonymous hash |
| 28 | my $colour = shift; |
| 29 | $colour = 2 unless defined($colour); # Default is red |
| 30 | $self->{'Colour'}=$colour; |
| 31 | $self->{'Xvertices'} = [-10, 10, 10,-10, -10]; # Initialise as square |
| 32 | $self->{'Yvertices'} = [-10,-10, 10, 10, -10]; |
| 33 | bless $self; |
| 34 | } |
| 35 | |
| 36 | # Method to plot a Square object at $x,$y |
| 37 | |
| 38 | sub plot { |
| 39 | my $self = shift; |
| 40 | my($x,$y) = @_; |
| 41 | my(@xpts) = @{$self->{'Xvertices'}}; |
| 42 | my(@ypts) = @{$self->{'Yvertices'}}; |
| 43 | |
| 44 | for (@xpts) { $_ = $_ + $x } |
| 45 | for (@ypts) { $_ = $_ + $y } |
| 46 | |
| 47 | PGPLOT::pgsci($self->{'Colour'}); PGPLOT::pgpoly(scalar(@xpts), \@xpts, \@ypts); |
| 48 | PGPLOT::pgsci(1); PGPLOT::pgline(scalar(@xpts), \@xpts, \@ypts); |
| 49 | } |
| 50 | |
| 51 | # Method to expand a Square object |
| 52 | |
| 53 | sub expand { |
| 54 | my $self = shift; |
| 55 | my $fac = shift; |
| 56 | my $xpts = $self->{'Xvertices'}; |
| 57 | my $ypts = $self->{'Yvertices'}; |
| 58 | |
| 59 | for (@$xpts) { $_ = $_ * $fac } |
| 60 | for (@$ypts) { $_ = $_ * $fac } |
| 61 | } |
| 62 | |
| 63 | # Method to rotate a Square object |
| 64 | |
| 65 | sub rotate { |
| 66 | my $self = shift; |
| 67 | my $angle = (shift)*(3.141592564/180); |
| 68 | my $x = $self->{'Xvertices'}; |
| 69 | my $y = $self->{'Yvertices'}; |
| 70 | my ($x2,$y2); |
| 71 | |
| 72 | for(my $i=0; $i<=$#{$x}; $i++) { |
| 73 | $x2 = $$x[$i]*cos($angle) + $$y[$i]*sin($angle); |
| 74 | $y2 = -$$x[$i]*sin($angle) + $$y[$i]*cos($angle); |
| 75 | $$x[$i] = $x2; $$y[$i] = $y2; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | |
| 80 | ############################################################## |
| 81 | |
| 82 | package Triangle; |
| 83 | |
| 84 | # Only difference is "new" method. Otherwise inherit |
| 85 | # all other properties from "Square"; |
| 86 | |
| 87 | our @ISA = qw( Square ); |
| 88 | |
| 89 | # Create a new Triangle |
| 90 | |
| 91 | sub new { |
| 92 | my $type = shift; # Ignore as we know we are a Square; |
| 93 | my $self = {}; # $self is ref to anonymous hash |
| 94 | my $colour = shift; |
| 95 | $colour = 3 unless defined($colour); # Default is green |
| 96 | $self->{'Colour'}=$colour; |
| 97 | $self->{'Xvertices'} = [-10, 10, 0, -10]; # Initialise as square |
| 98 | $self->{'Yvertices'} = [-10,-10, 5, -10]; |
| 99 | bless $self; |
| 100 | } |
| 101 | |
| 102 | ############################################################## |
| 103 | |
| 104 | # Now let's use these objects |
| 105 | |
| 106 | package main; |
| 107 | |
| 108 | note "Testing Square Objects..."; |
| 109 | |
| 110 | my $shape1 = new Square; |
| 111 | |
| 112 | # Plot first shape at 50,50; |
| 113 | |
| 114 | note "Square plot method..."; |
| 115 | |
| 116 | $shape1->plot(50,50); |
| 117 | |
| 118 | note "Square expand and rotate methods..."; |
| 119 | |
| 120 | $shape1->expand(2.3); # Make the shape bigger |
| 121 | $shape1->rotate(20); # Rotate the shape bigger |
| 122 | |
| 123 | $shape1->plot(-20,-50); |
| 124 | |
| 125 | note "Inheriting Square methods in Triangles..."; |
| 126 | |
| 127 | my $shape2 = new Triangle; |
| 128 | |
| 129 | $shape2->plot(-20,50); |
| 130 | |
| 131 | my $shape3 = new Triangle(4); # Blue triangle |
| 132 | $shape3->rotate(-15); $shape3->expand(1.5); |
| 133 | |
| 134 | $shape3->plot(50,-50); |
| 135 | |
| 136 | PGPLOT::pgend(); |
| 137 | |
| 138 | pass; |
| 139 | done_testing; |