<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package loc;

#
#  create new location object.
#  $loc1 = loc-&gt;new()           create with default (0,0)
#  $loc2 = loc-&gt;new(x,y)        create with (x,y)
#  $loc3 = $loc1-&gt;new()         clone $loc1
#
sub new
{
    my $r_loc;
    my $pck = shift;    # first parameter is class name
    if (ref($pck))
    # called as instance method $locobject-&gt;new()
    {
	$r_loc = { "x" =&gt; $pck-&gt;{"x"},
		   "y" =&gt; $pck-&gt;{"y"}
		 };
    }
    else
    # called as class method loc-&gt;new()
    {
	if (@_)
	{
	    my $p1 = shift;
	    if (ref($p1) eq "loc")
	    {
		$r_loc = { "x" =&gt; $p1-&gt;{"x"},
			   "y" =&gt; $p1-&gt;{"y"}
			 };
	    }
	    else
	    {
		my $p2 = shift;
		$r_loc = { "x" =&gt; $p1,
			   "y" =&gt; $p2
			 };
	    }
	}
	else
	{
	    $r_loc = { "x" =&gt; 0,
		       "y" =&gt; 0
		     };
	}
    }
    bless $r_loc, 'loc';
    return $r_loc;
}


#
#  let $loc be equal to $loc2 or P(x,y)
#  $loc-&gt;eq(x,y)
#  $loc-&gt;eq($loc2)
#
sub eq
{
    my $r_loc = shift;
    my $p1 = shift;
    if (ref($p1) eq "loc")
    {
	$r_loc-&gt;{"x"} = $p1-&gt;{"x"};
	$r_loc-&gt;{"y"} = $p1-&gt;{"y"};
    }
    else
    {
	my $p2 = shift;
	$r_loc-&gt;{"x"} = $p1;
	$r_loc-&gt;{"y"} = $p2;
    }
}


#
#  adds offset $loc2 or vec(x,y) to $loc
#  $loc-&gt;translate(x,y)
#  $loc-&gt;translate($loc2)
#
sub translate
{
    my $r_loc = shift;
    my $p1 = shift;
    if (ref($p1) eq "loc")
    {
	$r_loc-&gt;{"x"} += $p1-&gt;{"x"};
	$r_loc-&gt;{"y"} += $p1-&gt;{"y"};
    }
    else
    {
	my $p2 = shift;
	$r_loc-&gt;{"x"} += $p1;
	$r_loc-&gt;{"y"} += $p2;
    }
}


#
#  rotates around origin counterclockwise
#  $loc-&gt;rotate(phi)
#
sub rotate
{
    my $r_loc = shift;
    my $p1 = shift;
    $p1 *= $::pi/180;
    my ($x, $y) = ($r_loc-&gt;{"x"}, $r_loc-&gt;{"y"});
    $r_loc-&gt;{"x"} = $x*cos($p1) - $y*sin($p1);
    $r_loc-&gt;{"y"} = $x*sin($p1) + $y*cos($p1);
}


# show values
# $loc-&gt;dump( [name] )
sub dump
{
    my $r_loc = shift;
    my $name = shift;
    printf "%s (LOC): x = %f, y = %f\n", $name, $r_loc-&gt;{"x"}, $r_loc-&gt;{"y"};
}

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