clip.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993,99 by the Free Pascal development team
  5. This include implements the different clipping algorithms
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. const
  13. LEFT = 1; { Left window }
  14. RIGHT = 2; { Right window }
  15. BOTTOM = 4; { Bottom window }
  16. TOP = 8; { Top window }
  17. { 0 = in window }
  18. function LineClipped(var x1, y1,x2,y2: smallint; xmin, ymin,
  19. xmax, ymax:smallint): boolean;
  20. {********************************************************}
  21. { Function LineClipped() }
  22. {--------------------------------------------------------}
  23. { This routine clips the line coordinates to the }
  24. { min. and max. values of the window. Returns TRUE if }
  25. { the ENTIRE line was clipped. Updated }
  26. { clipped line endpoints are also returned. }
  27. { This algorithm is the classic Cohen-Sutherland line }
  28. { clipping algorithm. }
  29. {--------------------------------------------------------}
  30. var
  31. code1, code2: longint;
  32. done:boolean;
  33. code: longint;
  34. newx,newy: word;
  35. function outcode(x,y:smallint): longint;
  36. {********************************************************}
  37. { Function OutCode() }
  38. {--------------------------------------------------------}
  39. { This routine determines if the specified end point }
  40. { of a line lies within the visible window, if not it }
  41. { determines in which window the point is. }
  42. {--------------------------------------------------------}
  43. var
  44. code: longint;
  45. begin
  46. code := 0;
  47. if (x<xmin) then
  48. code:=code or LEFT
  49. else if (x>xmax) then
  50. code:=code or RIGHT;
  51. if (y>ymax) then
  52. code:=code or BOTTOM
  53. else if (y<ymin) then
  54. code:=code or TOP;
  55. outcode:=code;
  56. end;
  57. begin
  58. done:=false;
  59. code1:= OutCode(x1,y1);
  60. code2:= OutCode(x2,y2);
  61. while not done do
  62. begin
  63. { Accept trivially }
  64. { both points are in window }
  65. if ((code1=0) and (code2=0)) then
  66. begin
  67. done:=TRUE;
  68. LineClipped:=FALSE;
  69. exit;
  70. end
  71. else
  72. { Reject trivially }
  73. { Neither points are in window }
  74. if (code1 and code2) <> 0 then
  75. begin
  76. done:=true;
  77. LineClipped:=TRUE;
  78. exit;
  79. end
  80. else
  81. begin
  82. { Some points are partially out of the window }
  83. { find the new end point of the lines... }
  84. if code1 = 0 then
  85. code:=code2
  86. else
  87. code:=code1;
  88. if (code and LEFT) <> 0 then
  89. begin
  90. newy:=y1+((y2-y1)*(xmin-x1)) div (x2-x1);
  91. newx:=xmin;
  92. end
  93. else
  94. if (code and RIGHT) <> 0 then
  95. begin
  96. newy:=y1+((y2-y1)*(xmax-x1)) div (x2-x1);
  97. newx:=xmax;
  98. end
  99. else
  100. if (code and BOTTOM) <> 0 then
  101. begin
  102. newx:=x1+((x2-x1)*(ymax-y1)) div (y2-y1);
  103. newy:=ymax;
  104. end
  105. else
  106. if (code and TOP) <> 0 then
  107. begin
  108. newx:=x1+((x2-x1)*(ymin-y1)) div (y2-y1);
  109. newy:=ymin;
  110. end;
  111. if (code1 = code) then
  112. begin
  113. x1 := newx; y1:= newy;
  114. code1:=outcode(x1,y1)
  115. end
  116. else
  117. begin
  118. x2:= newx; y2:= newy;
  119. code2:=outcode(x2,y2);
  120. end
  121. end;
  122. end;
  123. LineClipped:=FALSE;
  124. end;
  125. {
  126. $Log$
  127. Revision 1.7 1999-12-20 11:22:35 peter
  128. * integer -> smallint to overcome -S2 switch needed for ggi version
  129. Revision 1.6 1999/09/27 12:35:27 jonas
  130. * execute multiplications before divisions in lineclipped to avoid rounding errors
  131. Revision 1.5 1999/09/18 22:21:09 jonas
  132. + hlinevesa256 and vlinevesa256
  133. + support for not/xor/or/andput in vesamodes with 32k/64k colors
  134. * lots of changes to avoid warnings under FPC
  135. Revision 1.4 1999/09/12 17:28:59 jonas
  136. * several changes to internalellipse to make it faster
  137. and to make sure it updates the ArcCall correctly
  138. (not yet done for width = 3)
  139. * Arc mostly works now, only sometimes an endless loop, don't know
  140. why
  141. Revision 1.3 1999/07/12 13:27:09 jonas
  142. + added Log and Id tags
  143. * added first FPC support, only VGA works to some extend for now
  144. * use -dasmgraph to use assembler routines, otherwise Pascal
  145. equivalents are used
  146. * use -dsupportVESA to support VESA (crashes under FPC for now)
  147. * only dispose vesainfo at closegrph if a vesa card was detected
  148. * changed int32 to longint (int32 is not declared under FPC)
  149. * changed the declaration of almost every procedure in graph.inc to
  150. "far;" becquse otherwise you can't assign them to procvars under TP
  151. real mode (but unexplainable "data segnment too large" errors prevent
  152. it from working under real mode anyway)
  153. }