#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Zinc; use MeM; my $mw = tkinit; my $heightmw = 260; my $width = 600; my $height = $heightmw - 60; $mw->geometry($width.'x'.$heightmw .'+100+100'); my $canvas = $mw->Zinc(-width => $width, -height => $height, -backcolor => 'black')->pack(-fill=>'both',-expand => 1); my $angle = 0; my $px0 = 0; my $py0 = 0; my $px_new = 240; my $py_new = $height; my %pends; #make a prototype pendulum easily at (0,0) coords $pends{0}{'pendulum'} = $canvas->add('group',1,-visible=> 1); # all lines are curves....of course!! it's relativity :-) my $line = $canvas->add('curve',$pends{0}{'pendulum'}, [0 ,0, 0 ,$py_new], -linewidth => 3, -tags => ['line'], -fillcolor => 'white', -linecolor => 'white', ); my $ball = $canvas->add('arc',$pends{0}{'pendulum'}, [-15,$py_new,15,$py_new+30], -tags => ['ball'], -filled=> 1, -fillcolor => 'orange', ); $canvas->translate($pends{0}{'pendulum'},240,0); $pends{0}{'center_rot'} = [240,0]; #end of first prototype #now clone and position the other pendulums for(1..4){ $pends{$_}{'pendulum'} = $canvas->clone( $pends{0}{'pendulum'} ); $canvas->translate($pends{$_}{'pendulum'}, $_*30, 0); $pends{$_}{'center_rot'} = [240 + $_ * 30 ,0]; } my $motion = 0; my $bframe = $mw->Frame()->pack(-fill =>'both'); my $startbut; my $timer; my $toggle = -1; $startbut = $bframe->Button(-text=>'Start', -command =>sub{ if($motion == 0){ $startbut->configure(-text=>'Stop'); $motion = 1; &start; }else{ $startbut->configure(-text=>'Start'); $motion = 0; $timer->cancel; } } )->pack(-side => 'left'); $bframe->Button(-text=>'Quit', -command =>sub{exit})->pack(-side => 'right'); MainLoop; ###################################################### sub start{ $timer = $canvas->repeat(10,sub{ &swing(.0175); }); } ###################################################### sub swing{ my $degree = shift; $degree = $toggle*$degree; $angle = $angle + $degree; if( $angle > 1.57 ){$toggle *= -1} if( $angle < -1.57 ){$toggle *= -1} for(0..5){ # rotate( item, rads, [center of rotation ]) $canvas->rotate($pends{$_}{'pendulum'}, $degree, @{$pends{$_}{'center_rot'}}); } }