console: Format tabs semi-intelligently
[attac-man.git] / math.c
blobe969803976432cc27343900b8920772a09d7bff0
1 /*
2 Pacman Arena
3 Copyright (C) 2003 Nuno Subtil
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 static const char cvsid[] =
21 "$Id: math.c,v 1.13 2003/11/22 17:32:09 nsubtil Exp $";
23 #ifdef _WIN32
24 #include <windows.h>
25 #endif
27 #include <GL/gl.h>
28 #include <stdio.h>
29 #include <math.h>
31 #include "object.h"
32 #include "m_math.h"
35 subtrai dois vectores (/r = /v1 - /v2)
37 void math_sub_vec3(float r[3], float v1[3], float v2[3])
39 r[X] = v1[X] - v2[X];
40 r[Y] = v1[Y] - v2[Y];
41 r[Z] = v1[Z] - v2[Z];
45 soma dois vectores (/r = /v1 + /v2)
47 void math_add_vec3(float r[3], float v1[3], float v2[3])
49 r[X] = v1[X] + v2[X];
50 r[Y] = v1[Y] + v2[Y];
51 r[Z] = v1[Z] + v2[Z];
55 multiplica um vector por um escalar (/r = k * /v)
57 void math_scale_vec3(float r[3], float k, float v[3])
59 r[X] = k * v[X];
60 r[Y] = k * v[Y];
61 r[Z] = k * v[Z];
64 void math_scale_vec2(float r[2], float k, float v[2])
66 r[X] = k * v[X];
67 r[Y] = k * v[Y];
71 roda vec em torno de axis
73 void math_rotate_vec3(float ret[3], float vec[3], float axis[3], float angle)
75 float matrix[4*4];
77 glMatrixMode(GL_MODELVIEW);
78 glPushMatrix();
80 glLoadIdentity();
81 glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
83 glRotatef(angle, axis[X], axis[Y], axis[Z]);
85 matrix[12] = vec[X];
86 matrix[13] = vec[Y];
87 matrix[14] = vec[Z];
89 glMultMatrixf(matrix);
90 glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
92 ret[X] = matrix[12];
93 ret[Y] = matrix[13];
94 ret[Z] = matrix[14];
96 glPopMatrix();
100 roda um vector 2d em torno da origem
102 void math_rotate_vec2(float ret[2], float vec[2], float angle)
104 float matrix[4*4];
106 glMatrixMode(GL_MODELVIEW);
107 glPushMatrix();
109 glLoadIdentity();
110 glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
112 glRotatef(angle, 0.0, -1.0, 0.0);
114 matrix[12] = vec[X];
115 matrix[13] = 0.0;
116 matrix[14] = vec[Y];
118 glMultMatrixf(matrix);
119 glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
121 ret[X] = matrix[12];
122 ret[Y] = matrix[14];
124 glPopMatrix();
129 calcula a norma de um vector
131 float math_norm_vec3(float v[3])
133 return sqrt(v[X] * v[X] + v[Y] * v[Y] + v[Z] * v[Z]);
136 float math_norm_vec2(float v[2])
138 return sqrt(v[X] * v[X] + v[Y] * v[Y]);
142 altera a norma de um vector para um valor definido
144 void math_len_vec3(float ret[3], float v[3], float len)
146 if(len == 0.0)
148 ret[X] = 0.0;
149 ret[Y] = 0.0;
150 ret[Z] = 0.0;
151 return;
154 math_scale_vec3(ret, 1.0 / (math_norm_vec3(v) / len), v);
157 void math_len_vec2(float ret[2], float v[2], float len)
159 math_scale_vec2(ret, 1.0 / (math_norm_vec2(v) / len), v);
163 produto escalar de dois vectores
165 float math_dot_vec2(float v1[2], float v2[2])
167 return v1[X] * v2[X] + v1[Y] * v2[Y];
170 float math_dot_vec3(float v1[3], float v2[3])
172 return v1[X] * v2[X] + v1[Y] * v2[Y] + v1[Z] * v2[Z];
176 menor ângulo entre dois vectores
178 float math_angle_vec2(float v1[2], float v2[2])
180 return acosf(math_dot_vec2(v1, v2) / (math_norm_vec2(v1) * math_norm_vec2(v2)));
183 float math_angle_vec3(float v1[2], float v2[2])
185 return acosf(math_dot_vec3(v1, v2) / (math_norm_vec3(v1) * math_norm_vec3(v2)));
189 devolve o quadrante de um vector 2d
191 int math_quadrant_vec2(float v[2])
193 if(v[X] >= 0.0 && v[Y] >= 0.0)
194 /* 1o quadrante */
195 return 1;
197 if(v[X] < 0.0 && v[Y] >= 0.0)
198 /* 2o quadrante */
199 return 2;
201 if(v[X] < 0.0 && v[Y] < 0.0)
202 /* 3o quadrante */
203 return 3;
205 return 4;
209 distância entre dois pontos
211 float math_dist_vec3(float v1[3], float v2[3])
213 float tmp[3];
215 math_sub_vec3(tmp, v1, v2);
216 return math_norm_vec3(tmp);
220 projecção de v sobre u
221 r = proj(u) v
223 void math_proj_vec3(float r[3], float v[3], float u[3])
225 float k, dv, du;
227 dv = math_dot_vec3(v, u);
228 du = math_dot_vec3(u, u);
229 k = dv / du;
231 math_scale_vec3(r, k, u);
235 gram-schmidt
236 ret = v2 - proj(v1) v2
238 void math_gschmidt_2vec3(float ret[3], float v1[3], float v2[3])
240 float proj[3];
242 math_proj_vec3(proj, v2, v1);
243 math_sub_vec3(ret, v2, proj);