home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TURBLE.ZIP / PAINT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1986-03-31  |  5.9 KB  |  139 lines

  1.    {Paints the inside of any regular figure and some irregular 
  2.    figures.  There are two known limitations to this procedure.  
  3.    
  4.    First, if you try to paint a jagged horizontal edge (a range of 
  5.    mountains or sharks teeth), only one tooth will be painted.  You 
  6.    can sometimes get around this by drawing and painting each tooth 
  7.    separately. 
  8.    
  9.    Second, if you draw a circle and try to paint the inside, 
  10.    everything is fine.  But if you try to paint the outside, only one 
  11.    side will be painted.  This is no great problem since you can paint 
  12.    the outside, then draw the circle and paint the inside blank.
  13.    
  14.    I'll try to fix the first limitation soon.  The second doesn't seem 
  15.    so important, but maybe I'll do it too. }
  16.  
  17. Procedure Paint(X, Y, Border, Color : Integer);
  18. Const
  19.   Up    = -1;                 {Constants to use as boolean flags...       }
  20.   Down  =  1;                 {...and to decrement or increment.          }
  21.   Left  = -1;
  22.   Right =  1;
  23.  
  24. Var
  25.   UpDown, RightLeft, 
  26.   TUY, TDY, UpX, DownX,
  27.   LastUpRX, LastUpLX, 
  28.   LastDownRX, LastDownLX : Integer;
  29.   Inside, UpDone, DownDone : Boolean;
  30.  
  31. Procedure OneLine(OX, OY : integer);   {Finds the left and right edges...}
  32. var                                    {and draws a line between them.   }
  33.   TLX, TRX : Integer;                  {Also stores edges for future use.}
  34.  
  35. begin
  36.   TRX := OX;                          {Find the right edge.              }
  37.   Repeat TRX := TRX + Right until (ColorDot(TRX,OY) = Border) or (TRX = XMax);
  38.   If UpDown = Up then LastUpRX := TRX else LastDownRX := TRX;
  39.   TLX := OX;                          {Find the left edge.               }
  40.   Repeat TLX := TLX + Left until (ColorDot(TLX,OY) = Border) or (TLX = 0);
  41.   If UpDown = Up then LastUpLX := TLX else LastDownLX := TLX;
  42.   Draw(TLX + 1,OY,TRX,OY,Color);           {Draw the line.               }
  43. end;
  44.    
  45.   {Notice above that the line starts one pixel to the left of the edge.  
  46.    That's how Turbo's LINE procedure works and the sooner you figure it out, 
  47.    the sooner you can avoid all the trouble I have writing this procedure.}
  48.    
  49.   {This procedure tries to find a point inside the figure.  If it finds one, 
  50.    it paints the line.}
  51.  
  52. Procedure CheckIn(TY : Integer);
  53.                        
  54.   {This procedure checks one side, either right or left depending on the 
  55.    current status of RightLeft. }
  56.  
  57. Procedure CheckSide;
  58. var                              
  59.   TestX, LastRX, LastLX : Integer;
  60.  
  61. begin
  62.   If UpDown = Up then
  63.     begin                                  {Get the last edges...          }
  64.       LastRX := LastUpRX;                  {...for appropriate direction.  }
  65.       LastLX := LastUpLX
  66.     end
  67.   else
  68.     begin
  69.       LastRX := LastDownRX;
  70.       LastLX := LastDownLX;
  71.     end;
  72.   TestX := X;
  73.   Repeat                                   {Increment or decrement until...}
  74.     TestX := TestX + RightLeft;            {...you reach an empty space.   }
  75.   Until (ColorDot(TestX, TY) <> Border) or (TestX = 0) or (TestX = XMax);
  76.   If (TestX < LastRX) and (TestX > LastLX) {See if it's inside.            }
  77.     then Inside := True else Inside := False;
  78.   If Inside then
  79.     begin                                  {If it is, then change X...     }
  80.       X := TestX;                          {...and draw a line.            }
  81.       OneLine(X,TY);
  82.     end;
  83. end;
  84.  
  85. begin                  
  86.   CheckSide;                               {Check the last successful side.}
  87.   If not Inside then                       {Then the other if necessary.   }
  88.     begin
  89.       RightLeft := -RightLeft;
  90.       CheckSide;
  91.     end;
  92.   If not Inside then                       {If still not in, you're done.  }
  93.     If UpDown = Up then UpDone := True else DownDone := True;
  94. end;
  95.  
  96. Procedure DrawUp;            
  97. begin                        
  98.   UpDown := Up;                            {Set direction.                 }
  99.   X := UpX;                                {Get saved X for Up.            }
  100.   TUY := TUY + Up;                         {Decrement the height.          }
  101.                    {Draw until you hit the border, then check the upline.  }
  102.   If ColorDot(X, TUY) = Border then CheckIn(TUY) else OneLine(X,TUY);
  103.   If TUY = 0 then UpDone := True;          {Quit at edge of screen.        }
  104.   UpX := X;                                {Save X for Up.                 }
  105. end;
  106.  
  107. Procedure DrawDown;          
  108. begin                        
  109.   UpDown := Down;                          {Set direction.                 }
  110.   X := DownX;                              {Get saved X for Down.          }
  111.   TDY := TDY + Down;                       {Increment the height.          }
  112.                    {Draw until you hit the border, then check the downline.}
  113.   If ColorDot(X, TDY) = Border then CheckIn(TDY) else OneLine(X,TDY);
  114.   If TDY = 199 then DownDone := True;      {Quit at edge of screen.        }
  115.   DownX := X;                              {Save X for Up.                 }
  116. end;
  117.  
  118. begin
  119.   RightLeft := Right;                      {Set starting values.           }
  120.   UpDown := Down;                    
  121.   DownX := X;  UpX := X;
  122.   If ColorDot(X,Y) <> Border then          {Initial point can't be border. }
  123.     begin                                  
  124.       Y := Round(Y * Scrunch);             {Adjust for aspect.             }
  125.       OneLine(X, Y);                       {Draw first line.               }
  126.       TUY := Y; UpDone := False;           {Set variables.                 }
  127.       TDY := Y; DownDone := False;         
  128.       Repeat                               
  129.         If not UpDone then DrawUp;         {Alternately draw Up...         }
  130.         If not DownDone then DrawDown;     {...and Down...                 }
  131.       Until UpDone and DownDone;           {...until done.                 }
  132.     end;
  133. end;
  134.  
  135.  
  136.  
  137.  
  138.  
  139.