<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Vector2D;
#####################################################
### Copyright (c) 2002 Russell B Cecala. All rights
### reserved.  This program is free software; you can
### redistribute it and/or modify it under the same 
### terms as Perl itself.
#####################################################
use strict;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
use overload
      "-"    =&gt; \&amp;minus,
      "+"    =&gt; \&amp;plus,
      "*"    =&gt; \&amp;mult,
      "bool" =&gt; \&amp;bool;




$VERSION     = 0.01;
@ISA         = qw(Exporter);
@EXPORT      = ();
@EXPORT_OK   = qw(&amp;new);
%EXPORT_TAGS = ( DEFAULT =&gt; [qw(&amp;new &amp;getx &amp;gety &amp;getxy)],
                   Both    =&gt; [qw(&amp;new &amp;getx &amp;gety)]);

sub new  {
	my ($pkg,$x,$y) = @_;
	bless {
		_x =&gt; $x,
		_y =&gt; $y
	}, $pkg;
}

sub getx { my $obj = shift; return $obj-&gt;{_x}; }
sub gety { my $obj = shift; return $obj-&gt;{_y}; }
sub setx { my $obj = shift; my $v = shift; $obj-&gt;{_x} = $v; }
sub sety { my $obj = shift; my $v = shift; $obj-&gt;{_y} = $v; }

sub getxy { 
	my $obj = shift; 
	my @xy = ( $obj-&gt;getx(), $obj-&gt;gety() );
	return @xy;
}

sub plus {
	my $u = shift;
	my $v = shift;
	return new Vector2D ( 
		$u-&gt;getx() + $v-&gt;getx(),
		$u-&gt;gety() + $v-&gt;gety()
	);
}

sub minus {
	my $u = shift;
	my $v = shift;
	return new Vector2D ( 
		$u-&gt;getx() - $v-&gt;getx(),
		$u-&gt;gety() - $v-&gt;gety()
	);
}

sub mult {
	my $v = shift;
	my $c = shift;
	return new Vector2D ( 
		$c * $v-&gt;getx(),
		$c * $v-&gt;gety()
	);
}

sub bool { return defined( shift ); }

sub incr {
	my $u = shift;
	my $v = shift;
	$u-&gt;{_x} += $v-&gt;{_x};
	$u-&gt;{_y} += $v-&gt;{_y};
	return $u;
}

sub decr {
	my $u = shift;
	my $v = shift;
	$u-&gt;{_x} -= $v-&gt;{_x};
	$u-&gt;{_y} -= $v-&gt;{_y};
	return $u;
}

sub scale {
	my $v = shift;
	my $c = shift;
	$v-&gt;{_x} *= $c;
	$v-&gt;{_y} *= $c;
	return $v;
}

sub rotate {
	my $P = shift; #vector
	my $C = shift; #vector
	my $cosphi = shift;
	my $sinphi = shift;
	my $dx = $P-&gt;{_x} - $C-&gt;{_x};
	my $dy = $P-&gt;{_y} - $C-&gt;{_y};
	return new Vector2D ( 
		$C-&gt;{_x} + $dx * $cosphi - $dy * $sinphi,
		$C-&gt;{_y} + $dx * $sinphi + $dy * $cosphi
	);
}

sub print {
	my $v = shift; #vector
	print "( " . $v-&gt;getx() . ", " . $v-&gt;gety() . ")\n";
}


1;
</pre></body></html>