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

  1. Program TurbleGraphics;
  2.  
  3. Const
  4.   SCRUNCHUP = 0.83;      {User defined variable -- from ASPECT.PAS}
  5.   HIXMAX = 640; HIYMAX = 480;
  6.   CIRCUMFERENCE = 360; HALFCIRCUMFERENCE = 180;
  7.  
  8. Var
  9.   ColorOfPen, ColorOfBack, PaletteColors : Integer;
  10.   PenDown, ColorIsOn, ResolutionHigh : Boolean;
  11.                                
  12.   {The maximum X and Y values (automatically adjusted).  Use like constants.}
  13.  
  14. Function XMax : Integer;
  15. begin
  16.   If ResolutionHigh then XMax := HiXMax else XMax := HiXMax div 2;
  17. end;
  18.  
  19. Function YMax : Integer;
  20. begin
  21.   If ResolutionHigh then YMax := HiYMax else YMax := HiYMax div 2;
  22. end;
  23.  
  24.   {For the next procedure, use the Turbo color constants or these constants.} 
  25.  
  26. Const
  27.   NONE = 99;             {A color constant high enough not to be a color}
  28.   ON = 1; OFF = 0;       {Use ON only in black and white or high resolution}
  29.  
  30.     {Sets the color of the pen.}
  31.  
  32. Procedure PenColor(Color : Integer);
  33. begin
  34.   If Color > 15 then
  35.     begin
  36.       PenDown := False;                 {Too high number turns off pen}
  37.       ColorOfPen := Off;
  38.     end
  39.   else
  40.     begin
  41.       ColorOfPen := Color;
  42.       PenDown := True;
  43.     end;
  44. end;
  45.  
  46.     {Sets background in color mode}
  47.  
  48. Procedure BackColor(Color : Integer);
  49. begin
  50.   If not ResolutionHigh then
  51.     begin
  52.       ColorOfBack := Color mod 15;
  53.       GraphBackGround(ColorOfBack);
  54.     end
  55.   else ColorOfBack := 0;
  56. end;
  57.  
  58.    {Sets Palette in color mode.}
  59.  
  60. Procedure Palettor(Number : Integer);
  61. begin
  62.   If ColorIsOn then
  63.     begin
  64.       PaletteColors := Number mod 4;
  65.       Palette(PaletteColors);
  66.     end
  67.   else if not ResolutionHigh then
  68.     begin
  69.       PaletteColors := Number mod 2;
  70.       Palette(PaletteColors);
  71.     end
  72.   else
  73.     begin
  74.       PaletteColors := Number mod 16;
  75.       HiResColor(PaletteColors);
  76.     end;
  77. end;
  78.  
  79.    {You can use these variables in programs, especially CenterX, and CenterY}
  80.  
  81. Var
  82.   CenterX, CenterY, StartX, StartY, TurtleAngle : Integer;
  83.  
  84.    {Sets turtle initial parameters. Use to reset startup defaults.}
  85.  
  86. Procedure InitTurtle;
  87. begin                           {Places turtle in center...       }
  88.   TurtleAngle := 0;             {...with angle of 0.              }
  89.   CenterX := XMax div 2;        {Sets global variables.           }
  90.   CenterY := YMax div 2;        {Center variables stay the same.  }
  91.   StartX := CenterX;            {Start variable change often.     }
  92.   StartY := CenterY;            
  93.   If not ResolutionHigh then
  94.     begin                       {With medium resolution set the...}
  95.       BackColor(0);             {...background to 0...            }
  96.       Palettor(0);              {...and palette to 0.             }
  97.     end else Palettor(15);      {Set to brightest color for HiRes.}
  98.   PenColor(On);                 {Turn pen on.                     }
  99. end;
  100.  
  101.    {Set graphics or text modes using the following constants.}
  102.  
  103. Const
  104.   MR  =  4;              {Medium Resolution }
  105.   CMR =  5;   MRC  = 5;  {Medium Resolution Color --two versions}
  106.   HR  =  6;              {High Resolution   }    
  107.                          
  108.    {Use this variable if you need to adjust for aspect.}
  109.  
  110. Var
  111.   Scrunch : Real;                                       
  112.  
  113.    {Sets the mode and the appropriate variables for each.}
  114.  
  115. Procedure Mode(Setting : Integer);
  116. begin
  117.   If Setting > 6 then Setting := Setting mod 6;
  118.   Case Setting of
  119.     0 : TextMode(BW40);        {Use Turbo's predefined constants for these}
  120.     1 : TextMode(C40);
  121.     2 : TextMode(BW80);
  122.     3 : TextMode(C80);
  123.     4 : begin
  124.           GraphMode;                  {Sets mode...               }
  125.           Scrunch := Scrunchup;       {...screen aspect...        }
  126.           ColorIsOn := False;         {...color flag...           }
  127.           ResolutionHigh := False;    {...and hi resolution flag. }
  128.           InitTurtle;                 {Initiate automatically.    }
  129.         end;
  130.     5 : begin
  131.           GraphColorMode;             {Mode...                    }
  132.           Scrunch := Scrunchup;       {...screen aspect...        }
  133.           ColorIsOn := True;          {...color flag...           }
  134.           ResolutionHigh := False;    {...and resolution flag.    }
  135.           InitTurtle;                 {Initiate automatically.    }
  136.         end;
  137.     6 : begin
  138.           HiRes;                      {Mode...                    }
  139.           Scrunch := Scrunchup / 2;   {...screen aspect to half...}
  140.           ColorIsOn := False;         {...color flag...           }
  141.           ResolutionHigh := True;     {...and resolution flag.    }
  142.           InitTurtle;                 {Initiate automatically.    }
  143.         end;
  144.   end;
  145. end;
  146.  
  147.     {Transforms Turbo's Plot to work on adjusted grid with predefined color.}
  148.  
  149. Procedure Dot(X, Y : Integer);
  150. begin
  151.      Y := Round(Y * Scrunch);
  152.      Plot(X,Y,ColorOfPen);
  153. end;
  154.  
  155.   {Transforms Turbo's Draw to work on adjusted grid with predefined color.}
  156.  
  157. Procedure Line(X1, Y1, X2, Y2 : Integer);
  158. begin
  159.     Y1 := Round(Y1 * Scrunch); Y2 := Round(Y2 * Scrunch);
  160.     Draw(X1,Y1,X2,Y2,ColorOfPen);
  161. end;
  162.  
  163.   {Sets TurtleAngle relative to starting angle.  Adjusts angles above 360.}
  164.  
  165. Procedure Turn(Angle : Integer);
  166. begin
  167.   TurtleAngle := TurtleAngle + Angle;
  168.   If (TurtleAngle > Circumference) or (TurtleAngle < -Circumference)
  169.     then TurtleAngle := TurtleAngle mod Circumference;
  170. end;
  171.  
  172.   {Sets absolute TurtleAngle.  Adjusts angles above 360.}
  173.  
  174. Procedure TurnTo(Angle : Integer);
  175. begin
  176.   TurtleAngle := Angle;
  177.   If (TurtleAngle > Circumference) or (TurtleAngle < -Circumference)
  178.     then TurtleAngle := TurtleAngle mod Circumference;
  179. end;
  180.  
  181.   {The heart of Turtle graphics.  Draws a line from the current Turtle  
  182.    position to a point determined by calculating the arc of the        
  183.    TurtleAngle.  Then makes the new point destination the new turtle    
  184.    position.}
  185.  
  186. Procedure Go(Distance : Integer);
  187. var
  188.   dX, dY : Integer;
  189.   WorkAngle : Real;
  190.  
  191. begin
  192.   WorkAngle := (TurtleAngle - 90) * pi / HalfCircumference;
  193.   dX := Round(Distance * cos(WorkAngle));
  194.   dY := Round(Distance * sin(WorkAngle));
  195.   If PenDown then Line(StartX,StartY,StartX + dX,StartY + dY);
  196.   StartX := StartX + dX;
  197.   StartY := StartY + dY;
  198. end;
  199.  
  200.    {Moves to an absolute position. Resets the Turtle position.}
  201.  
  202. Procedure MoveTo(X, Y : Integer);
  203. begin
  204.   If PenDown then Line(StartX,StartY,X,Y);
  205.   StartX := X;StartY := Y;
  206. end;
  207.  
  208.   {Returns the color of a dot at any given position.  This routine is 
  209.    used in the Paint procedure.  It can also be used in animation for 
  210.    testing to see if a moving figure has reached a certain point.} 
  211.  
  212. Function ColorDot(X, Y : Integer) : Byte;
  213. Type
  214.   RegPack = Record
  215.               ax,bx,cx,dx,bp,di,si,ds,es,flags: Integer;
  216.             end;
  217. Const
  218.   Vid = $10;                             {The video routine interrupt. }
  219.  
  220. Var
  221.   RecPack:          RegPack;             {Assign record.               }
  222.   ah,al:   Byte;                                                        
  223.  
  224. begin
  225.   ah := $0D;                             {Initialize registers with... } 
  226.   With RecPack do                        {...appropriate values.       }
  227.     begin
  228.       ax := ah Shl 8 + al;
  229.       cx := x;
  230.       dx := y;
  231.     end;
  232.   Intr(Vid,RecPack);                     {Call interrupt. It returns...}
  233.   ColorDot := Lo(RecPack.ax);            {...the color of the dot.     }
  234. end;
  235.  
  236.    {Draws any regular figure.  This is the most popular and versatile
  237.     procedure in Turtle graphics.  Many of the more complex procedures
  238.     in TURBLE2.PAS are variations of it.  Since the angle must be 
  239.     rounded, small inaccuracies creep in, especially with many-sided
  240.     figures.  Your figures may be slightly out of proportion.}
  241.  
  242. Procedure Poly(Number, Side : Integer);
  243.  
  244. Var 
  245.   I, Angle : Integer;                                         
  246.  
  247. begin
  248.   Angle := Round(360/Number);     {The turning angle is 360 divided by...}
  249.   For I := 1 to Number do         {...the number of sides.               } 
  250.     begin
  251.       Go(Side);                   {Draw a side...                        }
  252.       Turn(Angle);                {...turn the angle.                    }
  253.     end;
  254. end;
  255.  
  256.