Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.2 KiB

  1. #include "fow_2dplane.h"
  2. #include "mathlib/vector.h"
  3. // memdbgon must be the last include file in a .cpp file!!!
  4. #include <tier0/memdbgon.h>
  5. //-----------------------------------------------------------------------------
  6. // Purpose: construct the plane from the given line segment
  7. // Input : bx - starting x coord
  8. // by - starting y coord
  9. // ex - ending x coord
  10. // ey - ending y coord
  11. //-----------------------------------------------------------------------------
  12. CFOW_2DPlane::CFOW_2DPlane( float bx, float by, float ex, float ey )
  13. {
  14. Init( bx, by, ex, ey );
  15. }
  16. //-----------------------------------------------------------------------------
  17. // Purpose: construct the plane from the given point and normal
  18. // Input : x - point on plane
  19. // y - point on plane
  20. // vNormal - normal of the plane
  21. //-----------------------------------------------------------------------------
  22. CFOW_2DPlane::CFOW_2DPlane( float x, float y, Vector2D &vNormal )
  23. {
  24. Init( x, y, vNormal );
  25. }
  26. //-----------------------------------------------------------------------------
  27. // Purpose: construct the plane from the given distance and normal
  28. // Input : flDistance - distance for the plane
  29. // vNormal - normal of the plane
  30. //-----------------------------------------------------------------------------
  31. CFOW_2DPlane::CFOW_2DPlane( float flDistance, Vector2D &vNormal )
  32. {
  33. m_flDistance = flDistance;
  34. m_vNormal = vNormal;
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Purpose: init routine to generate the plane from the given line segment
  38. // Input : bx - starting x coord
  39. // by - starting y coord
  40. // ex - ending x coord
  41. // ey - ending y coord
  42. //-----------------------------------------------------------------------------
  43. void CFOW_2DPlane::Init( float bx, float by, float ex, float ey )
  44. {
  45. float nx = ( ex - bx );
  46. float ny = ( ey - by );
  47. float flLen = ( float )sqrt( nx * nx + ny * ny );
  48. Vector2D vNormal;
  49. nx /= flLen;
  50. ny /= flLen;
  51. vNormal.x = ny;
  52. vNormal.y = -nx;
  53. Init( bx, by, vNormal );
  54. }
  55. //-----------------------------------------------------------------------------
  56. // Purpose: init routine to generate the plane from the given point and normal
  57. // Input : x - point on plane
  58. // y - point on plane
  59. // vNormal - normal of the plane
  60. //-----------------------------------------------------------------------------
  61. void CFOW_2DPlane::Init( float x, float y, Vector2D &vNormal )
  62. {
  63. m_vNormal = vNormal;
  64. m_flDistance = ( x * m_vNormal.x + y * m_vNormal.y );
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Purpose: returns true if the point is in front of the plane
  68. // Input : px - point to check
  69. // py - point to check
  70. // Output : returns true if the point is in front of the plane
  71. //-----------------------------------------------------------------------------
  72. bool CFOW_2DPlane::PointInFront( float px, float py )
  73. {
  74. return ( DistanceFrom( px, py ) >= 0.0f );
  75. }
  76. //-----------------------------------------------------------------------------
  77. // Purpose: returns the distance the point is from the plane
  78. // Input : px - point to check
  79. // py - point to check
  80. // Output : returns the distance the point is from the plane
  81. //-----------------------------------------------------------------------------
  82. float CFOW_2DPlane::DistanceFrom( float px, float py )
  83. {
  84. float d = ( px * m_vNormal.x + py * m_vNormal.y );
  85. return d - m_flDistance;
  86. }
  87. //-----------------------------------------------------------------------------
  88. // Purpose: finds the fraction from the starting point towards the normal along the line formed with ending point
  89. // Input : bx - starting x coord of the line segment
  90. // by - starting y coord of the line segment
  91. // ex - ending x coord of the line segment
  92. // ey - ending y coord of the line segment
  93. // Output : returns the distance along the line segment the plane from the starting coord
  94. //-----------------------------------------------------------------------------
  95. float CFOW_2DPlane::DistanceFromLineStart( float bx, float by, float ex, float ey )
  96. {
  97. Vector2D vPointA( bx, by );
  98. Vector2D vPointB( ex, ey );
  99. Vector2D vDiff( vPointB - vPointA );
  100. Vector2D vNormal = vDiff;
  101. float flLen = ( float )sqrt( vDiff.x * vDiff.x + vDiff.y * vDiff.y );
  102. vNormal /= flLen;
  103. float t = -( m_vNormal.Dot( vPointA ) - m_flDistance ) / m_vNormal.Dot( vNormal );
  104. return t;
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Purpose: finds the fraction from the starting point towards the normal along the line formed with ending point
  108. // Input : bx - starting x coord of the line segment
  109. // by - starting y coord of the line segment
  110. // ex - ending x coord of the line segment
  111. // ey - ending y coord of the line segment
  112. // Output : returns the distance along the line segment the plane from the starting coord
  113. //-----------------------------------------------------------------------------
  114. float CFOW_2DPlane::DistanceFromRay( float bx, float by, float dx, float dy )
  115. {
  116. Vector2D vPointA( bx, by );
  117. Vector2D vNormal( dx, dy );
  118. float flNormalDiff = m_vNormal.Dot( vNormal );
  119. if ( flNormalDiff == 0.0f )
  120. {
  121. return 0.0f;
  122. }
  123. float t = -( m_vNormal.Dot( vPointA ) - m_flDistance ) / flNormalDiff;
  124. return t;
  125. }
  126. #include <tier0/memdbgoff.h>