1 /***************************************************************************
4 * Copyright (C) 2011 Ryan Bourgeois <bluedragonx@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 **************************************************************************/
26 /* Convert a radians value into an mtrack angle.
28 static double trig_encode_radians(double radians) {
29 double angle = (radians / M_PI) * 4;
35 /* Convert an mtrack angle value into radians.
37 static double trig_decode_radians(double angle) {
39 return (angle * M_PI) / 4;
41 return ((8 - angle) * M_PI) / -4;
44 double trig_direction(double dx, double dy) {
45 double angle = TR_NONE;
46 if (dx != 0 || dy != 0)
47 return trig_encode_radians(atan2(dx, dy*-1));
51 int trig_generalize(double dir)
55 else if (dir > 1 && dir <= 3)
57 else if (dir > 3 && dir <= 5)
59 else if (dir > 5 && dir <= 7)
65 double trig_angles_add(double a1, double a2)
67 double a = MODVAL(a1 + a2, 8.0);
73 double trig_angles_sub(double a1, double a2)
75 return trig_angles_add(a1, -1.0*a2);
78 double trig_angles_acute(double a1, double a2)
82 angle = trig_angles_sub(a1, a2);
84 angle = trig_angles_sub(a2, a1);
90 double trig_angles_avg(double* angles, int len)
95 for (i = 0; i < len; i++) {
96 r = trig_decode_radians(angles[i]);
100 return trig_encode_radians(atan2(dy, dx));
103 int trig_angles_cmp(double a1, double a2)