/
TobyGeometry.h
134 lines (114 loc) · 4.54 KB
/
TobyGeometry.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* TOBY -- An abstract interpreter engine and system for learning.
* Copyright (C) 1999 Ryan C. Gordon.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _INCLUDE_TOBYGEOMETRY_H_
#define _INCLUDE_TOBYGEOMETRY_H_
#include <math.h>
#include "util/TobyObject.h"
/*
* Some needed math routines.
*
* Written by Ryan C. Gordon. (icculus@linuxgames.com)
*/
class TobyGeometry
{
public:
static inline double degreesToRadians(double degrees)
{
return(degrees * (M_PI / 180.0));
} // TobyGeometry::degreesToRadians
static inline double radiansToDegrees(double radians)
{
return(radians * (180.0 / M_PI));
} // TobyGeometry::degreesToRadians
static inline int roundDoubleToInt(double dbl)
{
return((int) (dbl + 0.5));
} // TobyGeometry::roundDoubleToInt
static inline void calculateLine(double heading, double distance,
double startX, double startY,
double *endX, double *endY)
/*
* This procedure calculates coordinates for a line. No line is actually
* drawn by this procedure, though.
*
* params : heading == 0 - 360 degree direction line goes.
* distance == total space line should cover.
* startX == Starting x coordinate for line.
* startY == Starting y coordinate for line.
* *endX == filled with end point on X-axis.
* *endY == filled with end point on Y-axis.
* returns : void. Data is return in (endX) and (endY).
*/
{
assert(endX != NULL);
assert(endY != NULL);
double rad = degreesToRadians(heading);
*endX = (cos(rad) * (double) distance) + startX;
*endY = (sin(rad) * (double) distance) + startY;
} // TobyGeometry::calculateLine
/*
* This converts a float in the range of 0.0 to 1.0 to an 8-bit integer.
* A value of 0.0 (zero percent) yields a return of 0. A value of 1.0
* (one hundred percent) yields a return of 255. Everything else is
* somewhere inbetween. Values not in the range of 0.0 to 1.0 are clamped
* before conversion.
*/
static inline toby_uint8 floatTo8Bit(float val)
{
if (val > 1.0)
val = 1.0;
else if (val < 0.0)
val = 0.0;
return((toby_uint8) (val * 255.0));
} // TobyGeometry::floatTo8Bit
static inline double pythagorian(double s1, double hypotenuse)
/*
* The Pythagorian Theorem: Figure out the length of the third side
* of a right triangle, based on the lengths of the other two.
*
* The formula is like this: The sum of the squares of the lengths of
* the two non-hypotenuse sides of a right triangle will equal the
* square of the hypotenuse. So, if we know two sides of a right
* triangle, we can get the third. (In this function, you need to know
* which is the hypotenuse, but you can easily write a version of
* this that figures out the hypotenuse based on the other two sides.
*
* To find a non-hypotenuse side:
* (H2 represents hypotenuse, squared.)
* (X2 represents 1st non-hypotenuse side, squared.)
* (Y2 represents 2nd non-hypotenuse side, squared.)
*
* H2 = X2 + Y2
* H2 - X2 = Y2 + X2 - X2
* H2 - X2 = Y2
* H2 - H2 - X2 = Y2 - H2
* -X2 = Y2 - H2
* X2 = - Y2 + H2
* X2 = H2 - Y2
*
* params : s1 == length of known non-hypotenuse side of rt. triangle.
* hypotenuse == length of hypotenuse of right triangle.
* returns : length of third side of right triangle.
*/
{
return(sqrt((hypotenuse * hypotenuse) - (s1 * s1)));
} // TobyGeometry::pythagorian
}; // class TobyGeometry
#endif // !defined _INCLUDE_TOBYGEOMETRY_H_
// end of TobyGeometry.h ...