clip.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. This include implements the different clipping algorithms
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. const
  12. LEFT = 1; { Left window }
  13. RIGHT = 2; { Right window }
  14. BOTTOM = 4; { Bottom window }
  15. TOP = 8; { Top window }
  16. { 0 = in window }
  17. function LineClipped(var x1, y1,x2,y2: smallint; xmin, ymin,
  18. xmax, ymax:smallint): boolean;
  19. {********************************************************}
  20. { Function LineClipped() }
  21. {--------------------------------------------------------}
  22. { This routine clips the line coordinates to the }
  23. { min. and max. values of the window. Returns TRUE if }
  24. { the ENTIRE line was clipped. Updated }
  25. { clipped line endpoints are also returned. }
  26. { This algorithm is the classic Cohen-Sutherland line }
  27. { clipping algorithm. }
  28. {--------------------------------------------------------}
  29. var
  30. code1, code2: longint;
  31. code: longint;
  32. newx,newy: smallint;
  33. done:boolean;
  34. function outcode(x,y:smallint): longint;
  35. {********************************************************}
  36. { Function OutCode() }
  37. {--------------------------------------------------------}
  38. { This routine determines if the specified end point }
  39. { of a line lies within the visible window, if not it }
  40. { determines in which window the point is. }
  41. {--------------------------------------------------------}
  42. var
  43. code: longint;
  44. begin
  45. code := 0;
  46. if (x<xmin) then
  47. code:=code or LEFT
  48. else if (x>xmax) then
  49. code:=code or RIGHT;
  50. if (y>ymax) then
  51. code:=code or BOTTOM
  52. else if (y<ymin) then
  53. code:=code or TOP;
  54. outcode:=code;
  55. end;
  56. begin
  57. done:=false;
  58. code1:= OutCode(x1,y1);
  59. code2:= OutCode(x2,y2);
  60. while not done do
  61. begin
  62. { Accept trivially }
  63. { both points are in window }
  64. if ((code1=0) and (code2=0)) then
  65. begin
  66. done:=TRUE;
  67. LineClipped:=FALSE;
  68. exit;
  69. end
  70. else
  71. { Reject trivially }
  72. { Neither points are in window }
  73. if (code1 and code2) <> 0 then
  74. begin
  75. done:=true;
  76. LineClipped:=TRUE;
  77. exit;
  78. end
  79. else
  80. begin
  81. { Some points are partially out of the window }
  82. { find the new end point of the lines... }
  83. if code1 = 0 then
  84. code:=code2
  85. else
  86. code:=code1;
  87. if (code and LEFT) <> 0 then
  88. begin
  89. newy:=y1+((y2-y1)*(xmin-x1)) div (x2-x1);
  90. newx:=xmin;
  91. end
  92. else
  93. if (code and RIGHT) <> 0 then
  94. begin
  95. newy:=y1+((y2-y1)*(xmax-x1)) div (x2-x1);
  96. newx:=xmax;
  97. end
  98. else
  99. if (code and BOTTOM) <> 0 then
  100. begin
  101. newx:=x1+((x2-x1)*(ymax-y1)) div (y2-y1);
  102. newy:=ymax;
  103. end
  104. else
  105. if (code and TOP) <> 0 then
  106. begin
  107. newx:=x1+((x2-x1)*(ymin-y1)) div (y2-y1);
  108. newy:=ymin;
  109. end;
  110. if (code1 = code) then
  111. begin
  112. x1 := newx; y1:= newy;
  113. code1:=outcode(x1,y1)
  114. end
  115. else
  116. begin
  117. x2:= newx; y2:= newy;
  118. code2:=outcode(x2,y2);
  119. end
  120. end;
  121. end;
  122. LineClipped:=FALSE;
  123. end;