Advertisement
cd62131

Theta

Jul 20th, 2014
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.17 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use feature 'say';
  5. use Math::Trig;
  6. my $cos = {};
  7. my $sin = {};
  8. sub theta {
  9.   my ($x1, $y1, $x2, $y2) = map { deg2rad int $_ } @_;
  10.   $cos->{$x1} or $cos->{$x1} = cos $x1;
  11.   $sin->{$x1} or $sin->{$x1} = sin $x1;
  12.   $cos->{$y1} or $cos->{$y1} = cos $y1;
  13.   $sin->{$y1} or $sin->{$y1} = sin $y1;
  14.   $cos->{$x2} or $cos->{$x2} = cos $x2;
  15.   $sin->{$x2} or $sin->{$x2} = sin $x2;
  16.   $cos->{$y2} or $cos->{$y2} = cos $y2;
  17.   $sin->{$y2} or $sin->{$y2} = sin $y2;
  18.   my $theta = rad2deg(
  19.     acos(
  20.       (1 - (1 / 2) *
  21.         (
  22.           ($cos->{$y1} * $sin->{$x1} - $cos->{$y2} * $sin->{$x2}) ** 2 +
  23.           ($cos->{$y1} * $cos->{$x1} - $cos->{$y2} * $cos->{$x2}) ** 2 +
  24.           ($sin->{$y1} - $sin->{$y2}) ** 2
  25.         )
  26.       )
  27.     )
  28.   );
  29.   return $theta;
  30. }
  31. my $data = [];
  32. while (<>) {
  33.   chomp;
  34.   next if /\A\s*\z/;
  35.   my @d = split;
  36.   next if 2 != (scalar @d);
  37.   push @$data, \@d;
  38. }
  39. my $size = scalar @$data;
  40. for (my $i = 0; $i < $size; $i++) {
  41.   for (my $j = $i + 1; $j < $size; $j++) {
  42.     my $r = [$data->[$i][0], $data->[$i][1], $data->[$j][0], $data->[$j][1]];
  43.     push @$r, theta @$r;
  44.     say join "\t", @$r;
  45.   }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement