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 $";
35 subtrai dois vectores (/r = /v1 - /v2)
37 void math_sub_vec3(float r
[3], float v1
[3], float v2
[3])
45 soma dois vectores (/r = /v1 + /v2)
47 void math_add_vec3(float r
[3], float v1
[3], float v2
[3])
55 multiplica um vector por um escalar (/r = k * /v)
57 void math_scale_vec3(float r
[3], float k
, float v
[3])
64 void math_scale_vec2(float r
[2], float k
, float v
[2])
71 roda vec em torno de axis
73 void math_rotate_vec3(float ret
[3], float vec
[3], float axis
[3], float angle
)
77 glMatrixMode(GL_MODELVIEW
);
81 glGetFloatv(GL_MODELVIEW_MATRIX
, matrix
);
83 glRotatef(angle
, axis
[X
], axis
[Y
], axis
[Z
]);
89 glMultMatrixf(matrix
);
90 glGetFloatv(GL_MODELVIEW_MATRIX
, matrix
);
100 roda um vector 2d em torno da origem
102 void math_rotate_vec2(float ret
[2], float vec
[2], float angle
)
106 glMatrixMode(GL_MODELVIEW
);
110 glGetFloatv(GL_MODELVIEW_MATRIX
, matrix
);
112 glRotatef(angle
, 0.0, -1.0, 0.0);
118 glMultMatrixf(matrix
);
119 glGetFloatv(GL_MODELVIEW_MATRIX
, matrix
);
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
)
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)
197 if(v
[X
] < 0.0 && v
[Y
] >= 0.0)
201 if(v
[X
] < 0.0 && v
[Y
] < 0.0)
209 distância entre dois pontos
211 float math_dist_vec3(float v1
[3], float v2
[3])
215 math_sub_vec3(tmp
, v1
, v2
);
216 return math_norm_vec3(tmp
);
220 projecção de v sobre u
223 void math_proj_vec3(float r
[3], float v
[3], float u
[3])
227 dv
= math_dot_vec3(v
, u
);
228 du
= math_dot_vec3(u
, u
);
231 math_scale_vec3(r
, k
, u
);
236 ret = v2 - proj(v1) v2
238 void math_gschmidt_2vec3(float ret
[3], float v1
[3], float v2
[3])
242 math_proj_vec3(proj
, v2
, v1
);
243 math_sub_vec3(ret
, v2
, proj
);