summaryrefslogtreecommitdiff
path: root/support/ochem/loc.pm
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /support/ochem/loc.pm
Initial commit
Diffstat (limited to 'support/ochem/loc.pm')
-rw-r--r--support/ochem/loc.pm122
1 files changed, 122 insertions, 0 deletions
diff --git a/support/ochem/loc.pm b/support/ochem/loc.pm
new file mode 100644
index 0000000000..dc78776b23
--- /dev/null
+++ b/support/ochem/loc.pm
@@ -0,0 +1,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;