clip.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 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. code: longint;
  33. newx,newy: smallint;
  34. done:boolean;
  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.4 2002-09-07 15:07:46 peter
  128. * old logs removed and tabs fixed
  129. }