T2DMath.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2010 Nokia Corporation.
  3. */
  4. #include "T2DMath.h"
  5. int CT2DVector::setLength( const int l )
  6. {
  7. int oldl = length();
  8. if (oldl<1) {
  9. return 0;
  10. }
  11. m_x = tmath_fpmuldiv( m_x, l, oldl );
  12. m_y = tmath_fpmuldiv( m_y, l, oldl );
  13. return oldl;
  14. }
  15. void CT2DVector::normalize()
  16. {
  17. int l = length()+1;
  18. //if (l==0) return;
  19. m_x = tmath_fpdiv( m_x, l );
  20. m_y = tmath_fpdiv( m_y, l );
  21. }
  22. int CT2DVector::length()
  23. {
  24. __int64 x = (__int64)m_x * (__int64)m_x;
  25. __int64 y = (__int64)m_y * (__int64)m_y;
  26. return tmath_sqrt( x + y );
  27. }
  28. void CT2DVector::fixedMul( const int mul, const int bits )
  29. {
  30. m_x = tmath_interpolate( m_x, mul, bits );
  31. m_y = tmath_interpolate( m_y, mul, bits );
  32. }
  33. void CT2DVector::add( const CT2DVector &add )
  34. {
  35. m_x += add.m_x;
  36. m_y += add.m_y;
  37. }
  38. void CT2DVector::sub( const CT2DVector &add )
  39. {
  40. m_x -= add.m_x;
  41. m_y -= add.m_y;
  42. }
  43. void CT2DVector::createBetweenVectors( const CT2DVector &start, const CT2DVector &end )
  44. {
  45. m_x = (end.m_x - start.m_x);
  46. m_y = (end.m_y - start.m_y);
  47. }
  48. int CT2DVector::dotProduct( const CT2DVector &v ) const
  49. {
  50. return (int)((
  51. (__int64)m_x * (__int64)v.m_x +
  52. (__int64)m_y * (__int64)v.m_y) >> FP_BITS);
  53. }
  54. int CT2DVector::project( const CT2DVector &v )
  55. {
  56. int divid = dotProduct( *this );
  57. int todiv = dotProduct( v );
  58. if (divid == 0) divid = 1;
  59. return (int)((((__int64)todiv)<<FP_BITS)/(__int64)divid);
  60. }
  61. CT2DMatrix::CT2DMatrix()
  62. {
  63. setIdentity();
  64. }
  65. CT2DMatrix::~CT2DMatrix()
  66. {
  67. }
  68. void CT2DMatrix::setIdentity()
  69. {
  70. m[0][0] = FP_VAL;
  71. m[0][1] = 0;
  72. m[1][0] = 0;
  73. m[1][1] = FP_VAL;
  74. }
  75. void CT2DMatrix::transform( const CT2DVector *source, CT2DVector *target, int count )
  76. {
  77. while (count>0) {
  78. target->m_x = ((source->m_x*m[0][0] + source->m_y*m[0][1])>>8);
  79. target->m_y = ((source->m_x*m[1][0] + source->m_y*m[1][1])>>8);
  80. count--;
  81. target++;
  82. source++;
  83. }
  84. }
  85. CT2DVector::CT2DVector()
  86. {
  87. m_x = 0;
  88. m_y = 0;
  89. }
  90. CT2DVector::~CT2DVector()
  91. {
  92. }