Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

m_vector2.cpp

Go to the documentation of this file.
00001 // OMICRON ENGINE HEADER FILE
00002 //
00003 // --------------------------------------------------------------------------
00004 // Copyright (C) 2001-2002 by Bjoern Paetzel <kolrabi@gmx.de>
00005 //
00006 // This file is part of the Omicron Engine.
00007 //
00008 // The Omicron Engine is free software; you can redistribute it and/or modify
00009 // it under the terms of the  GNU General Public License  as published by the
00010 // Free Software Foundation;  either version  2  of the License,  or (at your
00011 // option) any later version.
00012 //
00013 // The Omicron Engine  is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00015 // or  FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License
00016 // for more details.
00017 //
00018 // You should have  received a copy of the  GNU General Public License  along
00019 // with The Omicron Engine;  if not,  write to the  Free Software Foundation,
00020 // Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
00021 //
00022 // --------------------------------------------------------------------------
00023 // Last modified:       $Date: 2002/12/07 19:02:06 $
00024 // By           :       $Author: kolrabi $
00025 // $Id: m_vector2.cpp,v 1.1.1.1 2002/12/07 19:02:06 kolrabi Exp $ 
00026 
00027 /*
00028 
00029   $Log: m_vector2.cpp,v $
00030   Revision 1.1.1.1  2002/12/07 19:02:06  kolrabi
00031   initial release
00032 
00033 
00034 */
00035  
00040 #include            "omicron/internal.h"
00041 #include            <math.h>
00042 
00043 const vec2_t vec2_c::vec2x   = { 1, 0 };
00044 const vec2_t vec2_c::vec2y   = { 0, 1 };
00045 const vec2_t vec2_c::vec20   = { 0, 0 };
00046 
00047 /**************************************************************************** 
00048  **************************************************************************** 
00049  * VECTOR ROUTINES ********************************************************** 
00050  **************************************************************************** 
00051  ****************************************************************************/ 
00052 
00053 vec2_c::vec2_c()
00054 {
00055     set(0,0);
00056 }
00057 
00058 vec2_c::vec2_c(const vec2_t v)
00059 {
00060     set(v[0], v[1]);
00061 }
00062 
00063 vec2_c::vec2_c(const vec2_c &v)
00064 {
00065     set(v.vec[0], v.vec[1]);
00066 }
00067 
00068 vec2_c::vec2_c(float x, float y)
00069 {
00070     set(x,y);
00071 }
00072 
00073 vec2_c::~vec2_c()
00074 {
00075 }
00076 
00077 vec2_c::operator const float*()
00078 {
00079     return vec;
00080 }
00081 
00082 // -- 
00083 
00084 void vec2_c::set(float x, float y)
00085 {
00086     vec[0] = x;
00087     vec[1] = y;
00088 }
00089 
00090 void vec2_c::set_component(int n, float x)
00091 {
00092     vec[n] = x;
00093 }
00094 
00095 void vec2_c::clear()
00096 {
00097     set(0,0);
00098 }
00099 
00100 float vec2_c::length()
00101 {
00102     return (float)sqrt(
00103         vec[VECT_X]*vec[VECT_X] +
00104         vec[VECT_Y]*vec[VECT_Y]);       // sqrts are slow :( 
00105 }
00106 
00107 float vec2_c::length_squared()
00108 {
00109     return vec[VECT_X]*vec[VECT_X] + vec[VECT_Y]*vec[VECT_Y];
00110 }
00111 
00112 bool vec2_c::operator ==(const vec2_t v)
00113 {
00114     ulong     i; 
00115 
00116     AssertReturnValue1(v, false);
00117 
00118     for (i=0 ; ++i<2;) 
00119         if (fabs(vec[i]-v[i]) > EQUAL_EPSILON) 
00120             return false; 
00121  
00122     return true; 
00123 }
00124 
00125 vec2_c vec2_c::operator +(const vec2_t v)
00126 {
00127     return vec2_c(v[0]+vec[0], v[1]+vec[1]);
00128 }
00129 
00130 vec2_c vec2_c::operator -(const vec2_t v)
00131 {
00132     return vec2_c(vec[0]-v[0], vec[1]-v[1]);
00133 }
00134 
00135 vec2_c vec2_c::operator *(const float f)
00136 {
00137     return vec2_c(f*vec[0], f*vec[1]);
00138 }
00139 
00140 vec2_c vec2_c::operator *(const vec2_t v)
00141 {
00142     return vec2_c(vec[0]*v[0], vec[1]*v[1]);
00143 }
00144 
00145 /**************************************************************************** 
00146  * vector_dot                                        calculate dot product * 
00147  ****************************************************************************/
00148 float vec2_c::dot
00149 ( 
00150     const vec2_t v
00151 ) 
00152 { 
00153     AssertReturnValue1(v, 0);
00154 
00155     return  v[VECT_X]*vec[VECT_X] + 
00156             v[VECT_Y]*vec[VECT_Y]; 
00157 } // vector_dot 
00158 
00159 vec2_c vec2_c::ma(const vec_t s, const vec2_t v)
00160 {
00161     return *this+vec2_c(v)*s;
00162 }
00163 
00164 vec2_c vec2_c::ma_vec(const vec2_t s, const vec2_t v)
00165 {
00166     return *this+vec2_c(v)*s;
00167 }
00168 
00169 vec2_c vec2_c::operator =(const vec2_t v)
00170 {
00171     vec[0] = v[0];
00172     vec[1] = v[1];
00173 
00174     return *this;
00175 }
00176 
00177 vec2_c vec2_c::normalize()
00178 {
00179     return *this * (1/length());
00180 }
00181 
00182 /****************************************************************************
00183  * vector_rotate                                    rotates a vector (ypr) *
00184  ****************************************************************************/
00185 vec2_c vec2_c::rotate
00186 ( 
00187     const vec_t angle
00188 )
00189 { 
00190     float rad; 
00191     float radsin; 
00192     float radcos; 
00193     vec2_t tmp;
00194  
00195     rad = -angle*DEG2RAD; 
00196     radsin = (float)(sin(rad)); radcos = (float)(cos(rad)); 
00197  
00198     tmp[0] = vec[0]*radcos-vec[1]*radsin; 
00199     tmp[1] = vec[0]*radsin+vec[1]*radcos; 
00200 
00201     return vec2_c(tmp);
00202 } 
00203 
00204 /****************************************************************************
00205  * vector_linear                              interpolation of two vectors *
00206  ****************************************************************************/
00207 vec2_c vec2_c::linear
00208 (
00209     const vec2_t p1, 
00210     const vec2_t p2, 
00211     float frac
00212 ) 
00213 { 
00214     AssertReturnValue2(p1,p2, vec2_c());
00215 
00216     vec2_c vec;
00217 
00218     for (ulong i=0; i<2; i++) 
00219         vec.set_component(i, _interpolate_float_linear(p1[i], p2[i], frac));
00220 
00221     return vec;
00222 } 
00223 
00224 /****************************************************************************
00225  * vector_hermite                     hermite interpolation of two vectors *
00226  ****************************************************************************/
00227 vec2_c vec2_c::hermite
00228 (
00229     const vec2_t p1, 
00230     const vec2_t t1, 
00231     const vec2_t p2, 
00232     const vec2_t t2, 
00233     float   frac
00234 ) 
00235 { 
00236     AssertReturnValue4(p1,p2,t1,t2, vec2_c());
00237 
00238     vec2_c vec;
00239 
00240     float tsquared  = frac*frac; 
00241     float tsquared3 = tsquared*3; 
00242     float tcubed    = frac*tsquared; 
00243     float tcubed2   = tcubed*2; 
00244     float t         = frac; 
00245  
00246     float a = (tcubed2-tsquared3+1); 
00247     float b = (tcubed-(2*tsquared)+t); 
00248     float c = (tcubed-tsquared); 
00249     float d = (-tcubed2+tsquared3); 
00250  
00251     for (ulong i=0; i<2; i++) 
00252     { 
00253         float   pa = p1[i]; 
00254         float   pb = p2[i]; 
00255         float   ma = t1[i]; 
00256         float   mb = t2[i]; 
00257  
00258         vec.set_component(i, a*pa+b*ma+c*mb+d*pb);
00259     } 
00260 
00261     return vec;
00262 } 

Generated on Wed Dec 18 15:48:47 2002 for omicron engine by doxygen1.2.18