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.

354 lines
8.1 KiB

  1. Copyright (c) 1994-1999 Microsoft Corporation
  2. ���������������������������������ͻ
  3. �RUNNING THE WALL OF THE TRAPEZOID�
  4. ���������������������������������ͼ�
  5. This file contains an explanation of how to run the wall of a
  6. trapezoid. If there are any questions please feel free to ask
  7. Kirk
  8. INTRODUCTION:
  9. A FIX point number is a 28.4 representation of a real number. Thus
  10. the real number associated with a FIX point number Q is
  11. Q
  12. q = ���
  13. F
  14. The radix shift factor F is has the form
  15. F = 2^L .
  16. In NT Windows the radix shift factor is
  17. L = 4
  18. and
  19. F = 2^4 = 16 .
  20. The real values of the endpoints of the line are:
  21. � M0 N0 �
  22. P0 = (x0,y0) = � ���� , ���� � ,
  23. � F F �
  24. � M1 M1 �
  25. P1 = (x1,y1) = � ���� , ���� � .
  26. � F F �
  27. The equation of the wall line is
  28. (x1 - x0)
  29. x(y) = ��������� (y - y0) + x0 .
  30. (y1 - y0)
  31. I can use the FIX point representation of the real coordiantes to
  32. provide an alternative expression of the equation of the wall line.
  33. (M1 - M0) � N0 � M0
  34. x(y) = ��������� � y - ���� � + ���� .
  35. (N1 - N0) � F � F
  36. At this point I shall introduce some convenient notation.
  37. DM = M1 - M0 ,
  38. DN = N1 - N0 > 0 ,
  39. m0 = floor(x0) = M0 >> L ,
  40. n0 = floor(y0) = N0 >> L ,
  41. qx = floor(F * frac(x0)) = M0 - (m0 << L) ,
  42. 0 <= qx < F
  43. qy = floor(F * frac(y0)) = N0 - (n0 << L) ,
  44. 0 <= qx < F
  45. Rsup = F DN .
  46. Note that m0, n0, qx, and qy are integers.
  47. This leads to the following form for the equation of the line:
  48. DM � qy � qx
  49. x(y) = ���� � y - n0 - ��� � + m0 + ����
  50. DN � F � F
  51. Note that x and y are real numbers while the offsets m0 and n0 are
  52. integers.
  53. According to the filling conventions of Windows, the position of the
  54. wall at y = j is given by:
  55. i(j) = ceil ( x(j) )
  56. � DM � qy � qx �
  57. = ceil � ���� � j - n0 - ��� � + m0 + ��� �
  58. � DN � F � F �
  59. � DM � qy � qx �
  60. = m0 + ceil � ���� � j - n0 - ��� � + ��� �
  61. � DN � F � F �
  62. � DM [ F(j - n0) - qy ] + DN qx �
  63. = m0 + ceil � ������������������������������� �
  64. � F DN �
  65. � DM [ F(j - n0) - qy ] + DN (F + qx) - 1 �
  66. = m0 + floor� ��������������������������������������� �
  67. � F DN �
  68. The quotient and remainder is calculated just once for the initial value
  69. of j = j0.
  70. DM [ F(j0 - n0) - qy ] + DN (F + qx) - 1 R
  71. ���������������������������������������� = Q + ���� ,
  72. F DN F DN
  73. where 0 <= R < F DN.
  74. ������������������������������������������������������������������Ŀ
  75. � CAUTION �
  76. � �
  77. � The programer must be cautions in the case where the numerator �
  78. � is negative. The C standard does not spcecify how the quotient �
  79. � should be rounded in this case. Just make sure that you adjust �
  80. � things so that R is positive! See Appendix I. �
  81. ��������������������������������������������������������������������
  82. Having done the division properly we are left with the answer
  83. � R �
  84. i(j) = m0 + floor� Q + ���� �
  85. � F DN �
  86. � R �
  87. = m0 + Q + floor� ���� �
  88. � F DN �
  89. = m0 + Q
  90. Now upon traversal the value of j changes by dj = +1 or -1. This will
  91. affect the value of i(j) as follows:
  92. i(j+dj) =
  93. � R dR �
  94. = m0 + floor� Q + ���� + di + �����ij
  95. � F DN F DN �
  96. � R + dR �
  97. = m0 + Q + di + floor� �������� �
  98. � F DN �
  99. � R + dR �
  100. = i(j) + di + floor� �������� �
  101. � F DN �
  102. Where we have introduced di and dR defined as follows:
  103. dR F DM dj
  104. di + ������ = ������� ,
  105. F DN F DN
  106. where 0 <= dR < F DN . [ Be careful about negative numerators. See Appendix I].
  107. /************************************************************************/
  108. ALGORITHM 0: <initialize: i, di, R, dR>
  109. INPUT:
  110. (i) (x0,y0) = (M0/16,N0/16) and (x1,y1) = (M1/16,N1/16) with y1 > y0.
  111. (ii) j0 such that y0 <= j0 <= y1.
  112. (iii) dj = +1 or -1.
  113. OUTPUT:
  114. i, di, R, dR, Rsup
  115. {
  116. DM = M1 - M0;
  117. DN = N1 - N0;
  118. m0 = M0 >> 4; // signed shifts work for all numbers!
  119. n0 = N0 >> 4;
  120. qx = M0 - (m0 << 4); // 0 <= qx < 16
  121. qy = N0 - (n0 << 4); // 0 <= qy < 16
  122. Rsup = DN << 4;
  123. // Find the quotient and remainder 0 <= R < Rsup.
  124. // Be careful about using '/' and '%' they do funny things when
  125. // the numerator is negative
  126. // Calculating i and R
  127. R DM*[16*(j0 - n0) - qy] + DN*(16 + qx) - 1
  128. i + ���� = ������������������������������������������ ;
  129. Rsup Rsup
  130. i += m0;
  131. // Calculating di and dR:
  132. // Similarly, be cautious about the next step. It must satisfy
  133. // 0 <= dR < 16 * DN
  134. dR 16*DM*dj
  135. di + ������� = �������� ;
  136. Rsup Rsup
  137. }
  138. /*********************************************************************/
  139. ALGORITHM 1: <Run the wall of the trapezoid>
  140. INPUT:
  141. (i) (x0,y0) = (M0/16,N0/16) and (x1,y1) = (M1/16,N1/16) with y1 > y0.
  142. (ii) j0 such that y0 <= j0 <= y1.
  143. (iii) dj = +1 or -1.
  144. OUTPUT:
  145. An array of integers i0, i1, i2, ... that represent the position
  146. of the trapezoid wall at the rows with y-coordinates:
  147. j0, j1 = j0 + dj, j2 = j1 + dj, ...
  148. {
  149. <initialize: i, di, R, dR, Rsup>;
  150. <process row i>;
  151. while (<not done>)
  152. {
  153. i += di;
  154. j += dj;
  155. R += dR;
  156. if (R >= Rsup)
  157. {
  158. R -= Rsup;
  159. i++;
  160. }
  161. <process row i>;
  162. }
  163. }
  164. **********************************************************************
  165. APPENDIX I
  166. Q: How do I handle those negative numerators anyway?
  167. A: Easy, consider the following division with a negative numerator
  168. and positive denominator.
  169. R -A
  170. Q + ��� = ��� , A>0, B>0
  171. B B
  172. First we convert the problem to one with positive numerator and
  173. denominator. Let
  174. A-1
  175. Q' = floor( ��� ) ,
  176. B
  177. R' = A - 1 - Q'B = (A - 1) mod B .
  178. Then the desired result is ...
  179. Q = - (Q'+ 1) ,
  180. R = B - R'- 1 .
  181. It is a useful exercise to prove this result.