1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
package loc;
#
# create new location object.
# $loc1 = loc->new() create with default (0,0)
# $loc2 = loc->new(x,y) create with (x,y)
# $loc3 = $loc1->new() clone $loc1
#
sub new
{
my $r_loc;
my $pck = shift; # first parameter is class name
if (ref($pck))
# called as instance method $locobject->new()
{
$r_loc = { "x" => $pck->{"x"},
"y" => $pck->{"y"}
};
}
else
# called as class method loc->new()
{
if (@_)
{
my $p1 = shift;
if (ref($p1) eq "loc")
{
$r_loc = { "x" => $p1->{"x"},
"y" => $p1->{"y"}
};
}
else
{
my $p2 = shift;
$r_loc = { "x" => $p1,
"y" => $p2
};
}
}
else
{
$r_loc = { "x" => 0,
"y" => 0
};
}
}
bless $r_loc, 'loc';
return $r_loc;
}
#
# let $loc be equal to $loc2 or P(x,y)
# $loc->eq(x,y)
# $loc->eq($loc2)
#
sub eq
{
my $r_loc = shift;
my $p1 = shift;
if (ref($p1) eq "loc")
{
$r_loc->{"x"} = $p1->{"x"};
$r_loc->{"y"} = $p1->{"y"};
}
else
{
my $p2 = shift;
$r_loc->{"x"} = $p1;
$r_loc->{"y"} = $p2;
}
}
#
# adds offset $loc2 or vec(x,y) to $loc
# $loc->translate(x,y)
# $loc->translate($loc2)
#
sub translate
{
my $r_loc = shift;
my $p1 = shift;
if (ref($p1) eq "loc")
{
$r_loc->{"x"} += $p1->{"x"};
$r_loc->{"y"} += $p1->{"y"};
}
else
{
my $p2 = shift;
$r_loc->{"x"} += $p1;
$r_loc->{"y"} += $p2;
}
}
#
# rotates around origin counterclockwise
# $loc->rotate(phi)
#
sub rotate
{
my $r_loc = shift;
my $p1 = shift;
$p1 *= $::pi/180;
my ($x, $y) = ($r_loc->{"x"}, $r_loc->{"y"});
$r_loc->{"x"} = $x*cos($p1) - $y*sin($p1);
$r_loc->{"y"} = $x*sin($p1) + $y*cos($p1);
}
# show values
# $loc->dump( [name] )
sub dump
{
my $r_loc = shift;
my $name = shift;
printf "%s (LOC): x = %f, y = %f\n", $name, $r_loc->{"x"}, $r_loc->{"y"};
}
1;
|