home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / sonderh1 / clipline.pas < prev    next >
Pascal/Delphi Source File  |  1987-05-18  |  3KB  |  110 lines

  1. (*-------------------------------------------------------------------------*)
  2. (*                            CLIPLINE.PAS                                 *)
  3. (*            Zeichnen einer Linie mit Clipping auf Fenstergrenzen.        *)
  4.  
  5. PROCEDURE ClipLine(x1, y1, x2, y2 : REAL);
  6.  
  7. TYPE direction = SET OF (left,right,top,bottom);
  8.  
  9. VAR m, m_inverse : REAL;      (* Steigung und reziproke Steigung *)
  10.     x, y : REAL;              (* Schnittpunkt mit Windowgrenzen  *)
  11.     dir, dir1, dir2 : direction;
  12.  
  13.   PROCEDURE region(x, y : REAL; VAR Endpunkt : direction); (* Wo liegt's ? *)
  14.  
  15.   BEGIN
  16.     Endpunkt := [];
  17.     IF x < Window[AktWin]^.xmin THEN
  18.       Endpunkt := [left]
  19.     ELSE
  20.       IF x > Window[AktWin]^.xmax THEN
  21.         Endpunkt := [right];
  22.     IF y < Window[AktWin]^.ymin THEN
  23.       Endpunkt := Endpunkt + [bottom]
  24.     ELSE
  25.       IF y > Window[AktWin]^.ymax THEN
  26.         Endpunkt := Endpunkt + [top]
  27.   END; (* region *)
  28.  
  29.  
  30.   PROCEDURE clip_left(VAR x, y : REAL); (* Links abschneiden *)
  31.  
  32.   BEGIN
  33.     y := m*(Window[AktWin]^.xmin - x1) + y1;
  34.     x := Window[AktWin]^.xmin
  35.   END;
  36.  
  37.  
  38.   PROCEDURE clip_right(VAR x, y : REAL); (* Rechts abschneiden *)
  39.  
  40.   BEGIN
  41.     y := m*(Window[AktWin]^.xmax - x1) + y1;
  42.     x := Window[AktWin]^.xmax
  43.   END;
  44.  
  45.  
  46.   PROCEDURE clip_top(VAR x, y : REAL);   (* Oben abschneiden *)
  47.  
  48.   BEGIN
  49.     x := m_inverse*(Window[AktWin]^.ymax - y1) + x1;
  50.     y := Window[AktWin]^.ymax
  51.   END;
  52.  
  53.  
  54.   PROCEDURE clip_bottom(VAR x, y : REAL); (* Unten abschneiden *)
  55.  
  56.   BEGIN
  57.     x := m_inverse*(Window[AktWin]^.ymin - y1) + x1;
  58.     y := Window[AktWin]^.ymin
  59.   END;
  60.  
  61.  
  62. BEGIN (* Line-Draw mit Clipping *)
  63.   region(x1,y1,dir1); (* In welchen Bereich liegen die Linienendpunkte ? *)
  64.   region(x2,y2,dir2);
  65.   IF x1 <> x2 THEN
  66.     m := (y2 - y1)/(x2 - x1);
  67.   IF y1 <> y2 THEN
  68.     m_inverse :=  (x2-x1)/(y2-y1);
  69.   WHILE (dir1 <> []) OR (dir2 <> []) DO BEGIN
  70.     IF dir1*dir2 <> [] THEN  (* Linie ausserhalb des Windows *)
  71.       Exit;
  72.     IF dir1 = [] THEN BEGIN (* P1 innerhalb des Windows, P2 clippen *)
  73.       dir := dir2;
  74.       x := x2;
  75.       y := y2
  76.     END
  77.     ELSE BEGIN
  78.       dir := dir1;         (* P1 clippen *)
  79.       x := x1;
  80.       y := y1
  81.     END;
  82.     IF left IN dir THEN
  83.       clip_left(x,y)
  84.     ELSE
  85.     IF right IN dir THEN
  86.       clip_right(x,y)
  87.     ELSE
  88.     IF bottom IN dir THEN
  89.       clip_bottom(x,y)
  90.     ELSE
  91.     IF top IN dir THEN
  92.       clip_top(x,y);
  93.     IF dir = dir1 THEN BEGIN
  94.       x1 := x;
  95.       y1 := y;
  96.       region(x1,y1,dir1)
  97.     END
  98.     ELSE BEGIN
  99.       x2 := x;
  100.       y2 := y;
  101.       region(x2,y2,dir2)
  102.     END
  103.   END;
  104.  
  105.   linew(x1,y1,x2,y2)
  106. END;
  107.  
  108. (*-------------------------------------------------------------------------*)
  109. (*                        Ende CLIPLINE.PAS                                *)
  110.