| 1 | package Tk::RotCanvas; | ||
| 2 | |||
| 3 | use vars qw/$VERSION/; | ||
| 4 | $VERSION = 1.2; | ||
| 5 | |||
| 6 | use Tk::widgets qw/Canvas/; | ||
| 7 | use base qw/Tk::Derived Tk::Canvas/; | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use Carp; | ||
| 11 | |||
| 12 | Construct Tk::Widget 'RotCanvas'; | ||
| 13 | |||
| 14 | sub ClassInit { | ||
| 15 | my $class = shift; | ||
| 16 | |||
| 17 | $class->SUPER::ClassInit(@_); | ||
| 18 | } | ||
| 19 | |||
| 20 | sub Populate { | ||
| 21 | my ($self, $args) = @_; | ||
| 22 | |||
| 23 | $self->SUPER::Populate($args); | ||
| 24 | } | ||
| 25 | |||
| 26 | my %_cant_handle = ( | ||
| 27 | bitmap => 1, | ||
| 28 | image => 1, | ||
| 29 | arc => 1, | ||
| 30 | text => 1, | ||
| 31 | window => 1, | ||
| 32 | ); | ||
| 33 | |||
| 34 | my %_rotate_methods = ( | ||
| 35 | line => \&_rotate_line, | ||
| 36 | polygon => \&_rotate_poly, | ||
| 37 | oval => \&_rotate_poly, | ||
| 38 | ); | ||
| 39 | |||
| 40 | use constant PI => 3.14159269; | ||
| 41 | |||
| 42 | # This is the new rotate() method. It takes as input the | ||
| 43 | # id of the object to rotate, and the angle to rotate it with. | ||
| 44 | # It then rotates the object about its center by the given angle | ||
| 45 | |||
| 46 | sub rotate { | ||
| 47 | my ($self, $id, $angle, $x, $y) = @_; | ||
| 48 | |||
| 49 | unless (defined $angle) { | ||
| 50 | croak "rotate: Must supply an angle -"; | ||
| 51 | } | ||
| 52 | |||
| 53 | # Get the current coordinates of the object. | ||
| 54 | my $type = $self->type($id); | ||
| 55 | |||
| 56 | # For now, I don't know how to handle some of these! | ||
| 57 | if (exists $_cant_handle{$type}) { | ||
| 58 | croak "rotate: Can't handle objects of type '$type' yet -"; | ||
| 59 | } | ||
| 60 | |||
| 61 | $_rotate_methods{$type}->($self, $id, $angle, $x, $y); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _rotate_line { | ||
| 65 | my ($self, $id, $angle, $midx, $midy) = @_; | ||
| 66 | |||
| 67 | # Get the old coordinates. | ||
| 68 | my @coords = $self->coords($id); | ||
| 69 | |||
| 70 | # If the center of rotation is not given, get the default. | ||
| 71 | # Get the center of the line. We use this to translate the | ||
| 72 | # above coords back to the origin, and then rotate about | ||
| 73 | # the origin, then translate back. | ||
| 74 | |||
| 75 | unless (defined $midx) { | ||
| 76 | $midx = $coords[0] + 0.5*($coords[2] - $coords[0]); | ||
| 77 | $midy = $coords[1] + 0.5*($coords[3] - $coords[1]); | ||
| 78 | } | ||
| 79 | |||
| 80 | my @new; | ||
| 81 | |||
| 82 | # Precalculate the sin/cos of the angle, since we'll call | ||
| 83 | # them a few times. | ||
| 84 | my $rad = PI*$angle/180; | ||
| 85 | my $sin = sin $rad; | ||
| 86 | my $cos = cos $rad; | ||
| 87 | |||
| 88 | # Calculate the new coordinates of the line. | ||
| 89 | while (my ($x, $y) = splice @coords, 0, 2) { | ||
| 90 | my $x1 = $x - $midx; | ||
| 91 | my $y1 = $y - $midy; | ||
| 92 | |||
| 93 | push @new => $midx + ($x1 * $cos - $y1 * $sin); | ||
| 94 | push @new => $midy + ($x1 * $sin + $y1 * $cos); | ||
| 95 | } | ||
| 96 | |||
| 97 | # Redraw the line. | ||
| 98 | $self->coords($id, @new); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub _rotate_poly { | ||
| 102 | my ($self, $id, $angle, $midx, $midy) = @_; | ||
| 103 | |||
| 104 | # Get the old coordinates. | ||
| 105 | my @coords = $self->coords($id); | ||
| 106 | |||
| 107 | # Get the center of the poly. We use this to translate the | ||
| 108 | # above coords back to the origin, and then rotate about | ||
| 109 | # the origin, then translate back. (old) | ||
| 110 | |||
| 111 | ($midx, $midy) = _get_CM(@coords) unless defined $midx; | ||
| 112 | |||
| 113 | my @new; | ||
| 114 | |||
| 115 | # Precalculate the sin/cos of the angle, since we'll call | ||
| 116 | # them a few times. | ||
| 117 | my $rad = PI*$angle/180; | ||
| 118 | my $sin = sin $rad; | ||
| 119 | my $cos = cos $rad; | ||
| 120 | |||
| 121 | # Calculate the new coordinates of the line. | ||
| 122 | while (my ($x, $y) = splice @coords, 0, 2) { | ||
| 123 | my $x1 = $x - $midx; | ||
| 124 | my $y1 = $y - $midy; | ||
| 125 | |||
| 126 | push @new => $midx + ($x1 * $cos - $y1 * $sin); | ||
| 127 | push @new => $midy + ($x1 * $sin + $y1 * $cos); | ||
| 128 | } | ||
| 129 | |||
| 130 | # Redraw the poly. | ||
| 131 | $self->coords($id, @new); | ||
| 132 | } | ||
| 133 | |||
| 134 | # We have to intercept any calls to createRectangle and | ||
| 135 | # create('rectangle') and call createPolygon instead. | ||
| 136 | |||
| 137 | sub createRectangle { | ||
| 138 | my $self = shift; | ||
| 139 | |||
| 140 | $self->_rect_to_poly(@_); | ||
| 141 | } | ||
| 142 | |||
| 143 | sub create { | ||
| 144 | my $self = shift; | ||
| 145 | |||
| 146 | my $type = shift; | ||
| 147 | |||
| 148 | if ($type eq 'rectangle') { | ||
| 149 | $self->_rect_to_poly(@_); | ||
| 150 | } elsif ($type eq 'oval') { | ||
| 151 | $self->_oval_to_poly(@_); | ||
| 152 | } else { | ||
| 153 | $self->SUPER::create($type, @_); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | sub createOval { | ||
| 158 | my $self = shift; | ||
| 159 | $self->_oval_to_poly(@_); | ||
| 160 | } | ||
| 161 | |||
| 162 | # This sub transforms the rectangle coords to poly coords. | ||
| 163 | sub _rect_to_poly { | ||
| 164 | my $self = shift; | ||
| 165 | |||
| 166 | my ($x1, $y1, $x2, $y2) = splice @_ => 0, 4; | ||
| 167 | |||
| 168 | $self->createPolygon( | ||
| 169 | $x1, $y1, | ||
| 170 | $x2, $y1, | ||
| 171 | $x2, $y2, | ||
| 172 | $x1, $y2, | ||
| 173 | @_, | ||
| 174 | ); | ||
| 175 | } | ||
| 176 | |||
| 177 | sub _oval_to_poly { | ||
| 178 | my $self = shift; | ||
| 179 | |||
| 180 | my ($x1, $y1, $x2, $y2) = splice @_ => 0, 4; | ||
| 181 | |||
| 182 | my $steps = 100; | ||
| 183 | my $xc = ($x2 - $x1) / 2; | ||
| 184 | my $yc = ($y2 - $y1) / 2; | ||
| 185 | my @pointlist; | ||
| 186 | |||
| 187 | for my $i (0..$steps) { | ||
| 188 | my $theta = (PI * 2)* ($i / $steps); | ||
| 189 | my $x = $xc * cos($theta) - $xc + $x2; | ||
| 190 | my $y = $yc * sin($theta) + $yc + $y1; | ||
| 191 | push(@pointlist, $x, $y); | ||
| 192 | } | ||
| 193 | |||
| 194 | push(@_, '-fill', undef) unless grep {/-fill/ } @_; | ||
| 195 | push(@_, '-outline', 'black') unless grep {/-outline/} @_; | ||
| 196 | |||
| 197 | $self->createPolygon(@pointlist, @_); | ||
| 198 | } | ||
| 199 | |||
| 200 | # This sub finds the center of mass of a polygon. | ||
| 201 | # I grabbed the algorithm somewhere from the web. | ||
| 202 | sub _get_CM { | ||
| 203 | my ($x, $y, $area); | ||
| 204 | |||
| 205 | my $i = 0; | ||
| 206 | |||
| 207 | while ($i < $#_) { | ||
| 208 | my $x0 = $_[$i]; | ||
| 209 | my $y0 = $_[$i+1]; | ||
| 210 | |||
| 211 | my ($x1, $y1); | ||
| 212 | if ($i+2 > $#_) { | ||
| 213 | $x1 = $_[0]; | ||
| 214 | $y1 = $_[1]; | ||
| 215 | } else { | ||
| 216 | $x1 = $_[$i+2]; | ||
| 217 | $y1 = $_[$i+3]; | ||
| 218 | } | ||
| 219 | |||
| 220 | $i += 2; | ||
| 221 | |||
| 222 | my $a1 = 0.5*($x0 + $x1); | ||
| 223 | my $a2 = ($x0**2 + $x0*$x1 + $x1**2)/6; | ||
| 224 | my $a3 = ($x0*$y1 + $y0*$x1 + 2*($x1*$y1 + $x0*$y0))/6; | ||
| 225 | my $b0 = $y1 - $y0; | ||
| 226 | |||
| 227 | $area += $a1 * $b0; | ||
| 228 | $x += $a2 * $b0; | ||
| 229 | $y += $a3 * $b0; | ||
| 230 | } | ||
| 231 | |||
| 232 | return split ' ', sprintf "%.0f %0.f" => $x/$area, $y/$area; | ||
| 233 | } | ||
| 234 | |||
| 235 | 1; | ||
| 236 | |||
| 237 | __END__ |