libnxter  0.1
Angle.nxc
Go to the documentation of this file.
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  Angle.nxc
4  Copyright (C) 2008 Naba Kumar <naba@gnome.org>
5 
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.
10 
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.
15 
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 Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 #ifndef __ANGLE_H_
22 #define __ANGLE_H_
23 
39 #include "Debug.nxc"
40 
44 inline int AngleAbsToRel(int absAngle)
45 {
46  if (absAngle > 180)
47  return (absAngle - 360);
48  return absAngle;
49 }
50 
54 inline int AngleRelToAbs(int relAngle)
55 {
56  if (relAngle < 0)
57  return (relAngle + 360);
58  return relAngle;
59 }
60 
65 inline int AngleAbsAdd(int absAngleA, int angle)
66 {
67  int val = (absAngleA + angle) % 360;
68  if (val < 0) val += 360;
69  return val;
70 }
71 
76 inline int AngleAbsSub(int absAngleA, int angle)
77 {
78  int val = (absAngleA - angle) % 360;
79  if (val < 0) val += 360;
80  return val;
81 }
82 
87 inline int AngleRelAdd(int relAngleA, int angle)
88 {
89  int val = relAngleA + angle;
90  val %= 360;
91  if (val > 180) val -= 360;
92  if (val < -180) val += 360;
93  return val;
94 }
95 
100 inline int AngleRelSub(int relAngleA, int angle)
101 {
102  int val = relAngleA - angle;
103  val %= 360;
104  if (val > 180) val -= 360;
105  if (val < -180) val += 360;
106  return val;
107 }
108 
109 /* Tests */
110 
111 #ifdef ENABLE_TEST
112 
116 void TestAngle()
117 {
118  TEST((AngleAbsToRel(10) == 10), "AbsToRel 10");
119  TEST((AngleAbsToRel(270) == -90), "AbsToRel 270");
120  TEST((AngleAbsToRel(0) == 0), "AbsToRel 0");
121  TEST((AngleAbsToRel(181) == -179), "AbsToRel 181");
122  TEST((AngleAbsToRel(180) == 180), "AbsToRel 180");
123  TEST((AngleAbsToRel(360) == 0), "AbsToRel 360");
124 
125  TEST((AngleRelToAbs(10) == 10), "AbsToRel 10");
126  TEST((AngleRelToAbs(-90) == 270), "AbsToRel -90");
127  TEST((AngleRelToAbs(0) == 0), "AbsToRel 0");
128  TEST((AngleRelToAbs(-179) == 181), "AbsToRel -179");
129  TEST((AngleRelToAbs(180) == 180), "AbsToRel 180");
130  TEST((AngleRelToAbs(-180) == 180), "AbsToRel -180");
131 
132  TEST((AngleAbsAdd(270, 100) == 10), "AbsAdd 100");
133  TEST((AngleAbsAdd(270, -100) == 170), "AbsAdd -100");
134  TEST((AngleAbsAdd(0, 10) == 10), "AbsAdd 10");
135  TEST((AngleAbsAdd(0, -10) == 350), "AbsAdd -10");
136  TEST((AngleAbsAdd(10, 500) == 150), "AbsAdd 500");
137  TEST((AngleAbsAdd(180, 180) == 0), "AbsAdd 180");
138 
139  TEST((AngleAbsSub(270, 100) == 170), "AbsSub 100");
140  TEST((AngleAbsSub(270, -100) == 10), "AbsSub -100");
141  TEST((AngleAbsSub(0, 10) == 350), "AbsSub 10");
142  TEST((AngleAbsSub(0, -10) == 10), "AbsSub -10");
143  TEST((AngleAbsSub(10, 500) == 230), "AbsSub 500");
144  TEST((AngleAbsSub(180, 180) == 0), "AbsSub 180");
145 
146  TEST((AngleRelAdd(170, 100) == -90), "RelAdd 100");
147  TEST((AngleRelAdd(10, -100) == -90), "RelAdd -100");
148  TEST((AngleRelAdd(0, 10) == 10), "RelAdd 10");
149  TEST((AngleRelAdd(0, -10) == -10), "RelAdd -10");
150  TEST((AngleRelAdd(10, 500) == 150), "RelAdd 500");
151  TEST((AngleRelAdd(10, -500) == -130), "RelAdd -500");
152  TEST((AngleRelAdd(180, 180) == 0), "RelAdd 180");
153 
154  TEST((AngleRelSub(270, 100) == 170), "RelSub 100");
155  TEST((AngleRelSub(270, -100) == 10), "RelSub -100");
156  TEST((AngleRelSub(0, 10) == -10), "RelSub 10");
157  TEST((AngleRelSub(0, -10) == 10), "RelSub -10");
158  TEST((AngleRelSub(10, 500) == -130), "RelSub 500");
159  TEST((AngleRelSub(180, 180) == 0), "RelSub 180");
160 }
161 
162 #endif /* ENABLE_TEST */
163 #endif __ANGLE_H_
164 
int AngleRelSub(int relAngleA, int angle)
Subtracts angle from a relative angle (-180 .. 180) relAngleA. Returns a relative angle properly trun...
Definition: Angle.nxc:100
Debugging utility macros.
int AngleAbsAdd(int absAngleA, int angle)
Adds angle to an absolute angle (0 .. 360) absAngleA. Returns an absolute angle properly truncated to...
Definition: Angle.nxc:65
int AngleRelAdd(int relAngleA, int angle)
Adds angle to a relative angle (-180 .. 180) relAngleA. Returns a relative angle properly truncated t...
Definition: Angle.nxc:87
int AngleAbsToRel(int absAngle)
Converts absolute angle (0 .. 360) to relative angle (-180 .. 180)
Definition: Angle.nxc:44
int AngleAbsSub(int absAngleA, int angle)
Subtracts angle from an absolute angle (0 .. 360) absAngleA. Returns an absolute angle properly trunc...
Definition: Angle.nxc:76
int AngleRelToAbs(int relAngle)
Converts relative angle (-180 .. 180) to absolute angle (0 .. 360)
Definition: Angle.nxc:54