Leaked source code of windows server 2003
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.

197 lines
4.3 KiB

  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2000 Microsoft Corporation
  4. *
  5. * Module Name:
  6. *
  7. * Geometry: Some 2D geometry helper routines.
  8. *
  9. * Created:
  10. *
  11. * 08/26/2000 asecchia
  12. * Created it.
  13. *
  14. **************************************************************************/
  15. #ifndef _GEOMETRY_HPP
  16. #define _GEOMETRY_HPP
  17. // return the square of the distance between point 1 and point 2.
  18. inline REAL distance_squared(const GpPointF &p1, const GpPointF &p2)
  19. {
  20. return ((p1.X-p2.X)*(p1.X-p2.X)+(p1.Y-p2.Y)*(p1.Y-p2.Y));
  21. }
  22. // return the dot product of two points treated as 2-vectors.
  23. inline double dot_product(const GpPointF &a, const GpPointF &b)
  24. {
  25. return (a.X*b.X + a.Y*b.Y);
  26. }
  27. // Return the intersection of a line specified by p0-p1 along the
  28. // y axis. Returns FALSE if p0-p1 is parallel to the yaxis.
  29. // Only returns intersections between p0 and p1 (inclusive).
  30. BOOL intersect_line_yaxis(
  31. IN const GpPointF &p0,
  32. IN const GpPointF &p1,
  33. OUT REAL *length
  34. );
  35. // Return the intersection of a line p0-p1 with the line r0-r1
  36. // The return value
  37. BOOL IntersectLines(
  38. IN const GpPointF &line1Start,
  39. IN const GpPointF &line1End,
  40. IN const GpPointF &line2Start,
  41. IN const GpPointF &line2End,
  42. OUT REAL *line1Length,
  43. OUT REAL *line2Length,
  44. OUT GpPointF *intersectionPoint
  45. );
  46. INT intersect_circle_line(
  47. IN const GpPointF &C, // center
  48. IN REAL radius2, // radius * radius (i.e. squared)
  49. IN const GpPointF &P0, // line first point (origin)
  50. IN const GpPointF &P1, // line last point (end)
  51. OUT GpPointF *intersection // return intersection point.
  52. );
  53. // Return true if point is inside the polygon defined by poly and count.
  54. // Use the FillModeAlternate (even-odd) rule.
  55. BOOL PointInPolygonAlternate(
  56. GpPointF point,
  57. INT count,
  58. GpPointF *poly
  59. );
  60. GpStatus GetFastAngle(REAL* angle, const GpPointF& vector);
  61. class GpVector2D : public GpPointF
  62. {
  63. public:
  64. GpVector2D()
  65. {
  66. X = Y = 0.0f;
  67. }
  68. GpVector2D(IN const PointF &point)
  69. {
  70. X = point.X;
  71. Y = point.Y;
  72. }
  73. GpVector2D(IN const GpVector2D &vec)
  74. {
  75. X = vec.X;
  76. Y = vec.Y;
  77. }
  78. GpVector2D(IN REAL x, IN REAL y)
  79. {
  80. X = x;
  81. Y = y;
  82. }
  83. // Scale.
  84. GpVector2D operator*(REAL k)
  85. {
  86. return GpVector2D(X*k, Y*k);
  87. }
  88. // Dot Product
  89. REAL operator*(IN const GpVector2D &V)
  90. {
  91. return (X*V.X+Y*V.Y);
  92. }
  93. VOID operator+=(IN const GpVector2D &V)
  94. {
  95. X += V.X;
  96. Y += V.Y;
  97. }
  98. VOID operator-=(IN const GpVector2D &V)
  99. {
  100. X -= V.X;
  101. Y -= V.Y;
  102. }
  103. VOID operator*=(IN const REAL k)
  104. {
  105. X *= k;
  106. Y *= k;
  107. }
  108. // Length or Vector Norm of the Vector.
  109. REAL Norm()
  110. {
  111. double length = (double)X*X+(double)Y*Y;
  112. length = sqrt(length);
  113. if( fabs(length) < REAL_EPSILON )
  114. {
  115. return 0.0f;
  116. }
  117. else
  118. {
  119. return (REAL)length;
  120. }
  121. }
  122. // Unitize the vector. If it is degenerate, return 0.0f
  123. REAL Normalize()
  124. {
  125. double length = (double)X*X+(double)Y*Y;
  126. if( length < 0.0 )
  127. {
  128. X = 0.0f;
  129. Y = 0.0f;
  130. return 0.0f;
  131. }
  132. length = sqrt(length);
  133. if( fabs(length) < REAL_EPSILON )
  134. {
  135. X = 0.0f;
  136. Y = 0.0f;
  137. return 0.0f;
  138. }
  139. else
  140. {
  141. X /= (REAL)length;
  142. Y /= (REAL)length;
  143. return (REAL)length;
  144. }
  145. }
  146. // This is the determinant of two 2-vectors. The formula is defined to
  147. // be the determinant of the 2x2 matrix formed by using the two vectors
  148. // as the columns of the matrix.
  149. // This is also known as the cross product of the two input vectors
  150. // though some math texts claim that cross products are only defined
  151. // for 3-vectors.
  152. static REAL Determinant(const GpVector2D &a, const GpVector2D &b)
  153. {
  154. return (a.X*b.Y-a.Y*b.X);
  155. }
  156. };
  157. #endif