[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Trig Trouble



I'm working on a MacPerl program that involves trig.  For the purpose of
'brushing up' on this topic, and gaining a basic insight into what I will
need to do, I threw together the following code, which essentially
increments an angle by 15 degrees (for each loop) throughout a range of 0
through 360 degrees.  For each such angle, I compute the radian equivalent
of the angle.  I then compute the x,y coordinates (of a 'unit circle') via
'x=cos(radians)' and 'y=sin(radians)'.  This 'for' loop exists primarily to
generate sample x,y coordinate data.  The real point of what I'm trying to
do is the following;  'given two points (0,0) and any arbitrary point on a
'unit circle', compute the angle using the 'atan2' function'.

The script follows;

$pi = 22/7;

for ($theta = 0; $theta <= 360; $theta+=15) {
   $radians = $theta * $pi / 180;

#debug
print "theta = $theta radians = $radians  ";

   $x2 = cos($radians);
   $y2 = sin($radians);
   &angle (0, 0, $x2, $y2);
}

sub angle {

   my ($x1) = $_[0];
   my ($y1) = $_[1];
   my ($x2) = $_[2];
   my ($y2) = $_[3];
   my ($pi) = 22/7;

   my ($delta_x) = $x2 - $x1;
   my ($delta_y) = $y2 - $y1;

   $atan    = atan2 ($delta_y , $delta_x);  #in radians
   $degrees = $atan * 180 / $pi;            #this is how to convert radians
to degrees

#debug
$x1prt = sprintf("%1.6f", $x1);
$y1prt = sprintf("%1.6f", $y1);
$x2prt = sprintf("%1.6f", $x2);
$y2prt = sprintf("%1.6f", $y2);
print ("($x1prt, $y1prt) ($x2prt, $y2prt) : $degrees degrees\n");

   return $degrees;

}


The output of which follows (it may 'wrap' in the email);

theta =   0 radians = 0                  (0.000000, 0.000000) ( 1.000000,
0.000000) :    0               degrees
theta =  15 radians = 0.261904761904762  (0.000000, 0.000000) ( 0.965899,
0.258921) :   15               degrees
theta =  30 radians = 0.523809523809524  (0.000000, 0.000000) ( 0.865920,
0.500183) :   30               degrees
theta =  45 radians = 0.785714285714286  (0.000000, 0.000000) ( 0.706883,
0.707330) :   45               degrees
theta =  60 radians = 1.04761904761905   (0.000000, 0.000000) ( 0.499635,
0.866236) :   60               degrees
theta =  75 radians = 1.30952380952381   (0.000000, 0.000000) ( 0.258310,
0.966062) :   75               degrees
theta =  90 radians = 1.57142857142857   (0.000000, 0.000000) (-0.000632,
1.000000) :   90               degrees
theta = 105 radians = 1.83333333333333   (0.000000, 0.000000) (-0.259531,
0.965735) :  105               degrees
theta = 120 radians = 2.09523809523809   (0.000000, 0.000000) (-0.500730,
0.865604) :  120               degrees
theta = 135 radians = 2.35714285714286   (0.000000, 0.000000) (-0.707777,
0.706436) :  135               degrees
theta = 150 radians = 2.61904761904762   (0.000000, 0.000000) (-0.866552,
0.499087) :  150               degrees
theta = 165 radians = 2.88095238095238   (0.000000, 0.000000) (-0.966225,
0.257699) :  165               degrees
theta = 180 radians = 3.14285714285714   (0.000000, 0.000000) (-0.999999,
-0.001264) : -179.855158502104  degrees
theta = 195 radians = 3.40476190476191   (0.000000, 0.000000) (-0.965570,
-0.260142) : -164.855158502104  degrees
theta = 210 radians = 3.66666666666667   (0.000000, 0.000000) (-0.865287,
-0.501277) : -149.855158502104  degrees
theta = 225 radians = 3.92857142857143   (0.000000, 0.000000) (-0.705988,
-0.708224) : -134.855158502104  degrees
theta = 240 radians = 4.19047619047619   (0.000000, 0.000000) (-0.498539,
-0.866867) : -119.855158502104  degrees
theta = 255 radians = 4.45238095238095   (0.000000, 0.000000) (-0.257088,
-0.966388) : -104.855158502104  degrees
theta = 270 radians = 4.71428571428571   (0.000000, 0.000000) ( 0.001897,
-0.999998) : - 89.8551585021036 degrees
theta = 285 radians = 4.97619047619048   (0.000000, 0.000000) ( 0.260752,
-0.965406) : - 74.8551585021036 degrees
theta = 300 radians = 5.23809523809524   (0.000000, 0.000000) ( 0.501824,
-0.864970) : - 59.8551585021036 degrees
theta = 315 radians = 5.5                (0.000000, 0.000000) ( 0.708670,
-0.705540) : - 44.8551585021036 degrees
theta = 330 radians = 5.76190476190476   (0.000000, 0.000000) ( 0.867182,
-0.497991) : - 29.8551585021036 degrees
theta = 345 radians = 6.02380952380952   (0.000000, 0.000000) ( 0.966550,
-0.256477) : - 14.8551585021036 degrees
theta = 360 radians = 6.28571428571429   (0.000000, 0.000000) ( 0.999997,
0.002529) :    0.1448414978964 degrees

Note that for 'input' angles (ie, column 1 values) of 0 through 'less than
180', the angle returned by 'atan2' (ie, the last column) EXACTLY MATCHES
the 'input' angle (ie, column 1 exactly matches the last column).

Note that for 'input' angles of 180 through 360, the angle returned by
'atan2' DOES NOT MATCH the 'input' angle (ie, column 1 does not exactly
match the last column).

My questions are;

1. Why is this happenning?

2. Would it be incorrect of me to assume (just because it looks so in this
sparse sample data set) that if I constrain my evaluations to be in the
range of 0 through 90, that I will always have EXACT results, or does the
possibility of error still exist, even though it is not evident from this
sparse sample data set?

3.  How have others traditionally solved this problem?



===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org