2 * Copyright (c) 2007 Ivan Leben
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library in the file COPYING;
16 * if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "shVectors.h"
23 #define _ITEM_T SHVector2
24 #define _ARRAY_T SHVector2Array
25 #define _FUNC_T shVector2Array
26 #define _COMPARE_T(v1,v2) EQ2V(v1,v2)
28 #include "shArrayBase.h"
30 void SHVector2_ctor(SHVector2 *v) {
34 void SHVector2_dtor(SHVector2 *v) {
37 void SHVector3_ctor(SHVector3 *v) {
38 v->x=0.0f; v->y=0.0f; v->z=0.0f;
41 void SHVector3_dtor(SHVector3 *v) {
44 void SHVector4_ctor(SHVector4 *v) {
45 v->x=0.0f; v->y=0.0f; v->z=0.0f; v->w=0.0f;
48 void SHVector4_dtor(SHVector4 *v) {
51 void SHRectangle_ctor(SHRectangle *r) {
52 r->x=0.0f; r->y=0.0f; r->w=0.0f; r->h=0.0f;
55 void SHRectangle_dtor(SHRectangle *r) {
58 void shRectangleSet(SHRectangle *r, SHfloat x,
59 SHfloat y, SHfloat w, SHfloat h)
61 r->x=x; r->y=y; r->w=w; r->h=h;
64 void SHMatrix3x3_ctor(SHMatrix3x3 *mt)
69 void SHMatrix3x3_dtor(SHMatrix3x3 *mt)
73 void shMatrixToGL(SHMatrix3x3 *m, SHfloat mgl[16])
75 /* When 2D vectors are specified OpenGL defaults Z to 0.0f so we
76 have to shift the third column of our 3x3 matrix to right */
77 mgl[0] = m->m[0][0]; mgl[4] = m->m[0][1]; mgl[8] = 0.0f; mgl[12] = m->m[0][2];
78 mgl[1] = m->m[1][0]; mgl[5] = m->m[1][1]; mgl[9] = 0.0f; mgl[13] = m->m[1][2];
79 mgl[2] = m->m[2][0]; mgl[6] = m->m[2][1]; mgl[10] = 1.0f; mgl[14] = m->m[2][1];
80 mgl[3] = 0.0f; mgl[7] = 0.0f; mgl[11] = 0.0f; mgl[15] = 1.0f;
83 int shInvertMatrix(SHMatrix3x3 *m, SHMatrix3x3 *mout)
85 /* Calculate determinant */
86 SHfloat D0 = m->m[1][1]*m->m[2][2] - m->m[2][1]*m->m[1][2];
87 SHfloat D1 = m->m[2][0]*m->m[1][2] - m->m[1][0]*m->m[2][2];
88 SHfloat D2 = m->m[1][0]*m->m[2][1] - m->m[2][0]*m->m[1][1];
89 SHfloat D = m->m[0][0]*D0 + m->m[0][1]*D1 + m->m[0][2]*D2;
91 /* Check if singular */
92 if( D == 0.0f ) return 0;
95 /* Calculate inverse */
96 mout->m[0][0] = D * D0;
97 mout->m[1][0] = D * D1;
98 mout->m[2][0] = D * D2;
99 mout->m[0][1] = D * (m->m[2][1]*m->m[0][2] - m->m[0][1]*m->m[2][2]);
100 mout->m[1][1] = D * (m->m[0][0]*m->m[2][2] - m->m[2][0]*m->m[0][2]);
101 mout->m[2][1] = D * (m->m[2][0]*m->m[0][1] - m->m[0][0]*m->m[2][1]);
102 mout->m[0][2] = D * (m->m[0][1]*m->m[1][2] - m->m[1][1]*m->m[0][2]);
103 mout->m[1][2] = D * (m->m[1][0]*m->m[0][2] - m->m[0][0]*m->m[1][2]);
104 mout->m[2][2] = D * (m->m[0][0]*m->m[1][1] - m->m[1][0]*m->m[0][1]);
109 SHfloat shVectorOrientation(SHVector2 *v) {
110 SHfloat norm = (SHfloat)NORM2((*v));
111 SHfloat cosa = v->x/norm;
112 SHfloat sina = v->y/norm;
113 return (SHfloat)(sina>=0 ? SH_ACOS(cosa) : 2*PI-SH_ACOS(cosa));
116 int shLineLineXsection(SHVector2 *o1, SHVector2 *v1,
117 SHVector2 *o2, SHVector2 *v2,
120 SHfloat rightU = o2->x - o1->x;
121 SHfloat rightD = o2->y - o1->y;
123 SHfloat D = v1->x * (-v2->y) - v1->y * (-v2->x);
124 SHfloat DX = rightU * (-v2->y) - rightD * (-v2->x);
125 /*SHfloat DY = v1.x * rightD - v1.y * rightU;*/
134 xsection->x = o1->x + t1*v1->x;
135 xsection->y = o1->y + t1*v1->y;