home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff339.lzh / PCQ / Examples / Circle.p < prev    next >
Text File  |  1990-03-19  |  3KB  |  127 lines

  1. Program Circle;
  2.  
  3. {
  4.       This program just draws two simple circles.  The first is
  5.       drawn using PCQ's new (at the moment) sine and cosine
  6.       functions.  The second is drawn directly over the top with
  7.       the SPSin and SPCos functions from the mathtrans.library.
  8.  
  9.       I wrote this to determine whether the trig functions I had
  10.       just written were accurate enough to be worthwhile.  Since
  11.       these two circles come pretty close to overlapping, I
  12.       left them in.
  13.  
  14.       To run this program without the mathtrans.library, just
  15.       remove the MathTrans.i include, the open and close
  16.       of the library, and lines that draw the second circle.
  17.       That's all.
  18.  
  19.       Later Note: I replaced the older, less accurate functions
  20.       with more traditional series-based functions, which are
  21.       much more accurate and only a little slower.
  22. }
  23.  
  24. {$I ":Include/Exec.i" for Forbid, Permit and library things }
  25. {$I ":Include/Ports.i" for the Message stuff }
  26. {$I ":Include/Intuition.i" for window & screen structures and functions }
  27. {$I ":Include/Graphics.i" for drawing stuff }
  28. {$I ":Include/MathTrans.i"}
  29.  
  30. Const
  31.     Pi = 3.1415927;
  32.     TwoPi = Pi * 2.0;
  33.  
  34.     Aspect = 2.0;    { To account for pixel shape }
  35.  
  36. var
  37.     w  : WindowPtr;
  38.     m  : MessagePtr;
  39.  
  40. Function OpenTheWindow() : Boolean;
  41. var
  42.     nw : NewWindowPtr;
  43. begin
  44.     new(nw);
  45.     with nw^ do begin
  46.     LeftEdge := 0;
  47.     TopEdge := 0;
  48.     Width := 640;
  49.     Height := 200;
  50.  
  51.     DetailPen := -1;
  52.     BlockPen  := -1;
  53.     IDCMPFlags := CLOSEWINDOW_f;
  54.     Flags := WINDOWSIZING_f + WINDOWDRAG_f + WINDOWDEPTH_f +
  55.          WINDOWCLOSE_f + SMART_REFRESH_f + ACTIVATE_f;
  56.     FirstGadget := nil;
  57.     CheckMark := nil;
  58.     Title := "Horseshoes, handgrenades, and some trigonomentry";
  59.     Screen := Nil;
  60.     BitMap := nil;
  61.     MinWidth := 50;
  62.     MaxWidth := -1;
  63.     MinHeight := 20;
  64.     MaxHeight := -1;
  65.     WType := WBENCHSCREEN_f;
  66.     end;
  67.  
  68.     w := OpenWindow(nw);
  69.     dispose(nw);
  70.     OpenTheWindow := w <> nil;
  71. end;
  72.  
  73. Procedure DoCircle(RP : RastPortPtr; CX, CY, Radius : Short);
  74. {
  75.     Draw a circle using 500 line segments
  76. }
  77. Const
  78.     Division = TwoPi / 500.0;
  79. var
  80.     t : Real;
  81.     i : Integer;
  82.     RealRad : Real;
  83. begin
  84.     SetAPen(rp, 1);
  85.     RealRad := Float(Radius);
  86.     Move(rp, CX + Round(RealRad * Aspect), CY);
  87.     for i := 1 to 500 do
  88.     Draw(rp, CX + Round(Cos(Float(i) * Division) * RealRad * Aspect),
  89.          CY + round(Sin(Float(i) * Division) * RealRad));
  90.     Draw(rp, CX + Round(RealRad * Aspect), CY);
  91.     SetAPen(rp, 3);
  92.     Move(rp, CX + Round(RealRad * Aspect), CY);
  93.     for i := 1 to 500 do
  94.     Draw(rp, CX + Round(SPCos(Float(i) * Division) * RealRad * Aspect),
  95.          CY + round(SPSin(Float(i) * Division) * RealRad));
  96.     Draw(rp, CX + Round(RealRad * Aspect), CY);
  97. end;
  98.  
  99. begin
  100.     { Note that the startup code of all PCQ programs depends on
  101.       Intuition, so if we got to this point Intuition must be
  102.       open, so the run time library just uses the pointer that
  103.       the startup code created.  Same with DOS, although we don't
  104.       use that here. }
  105.  
  106.     GfxBase := OpenLibrary("graphics.library", 0);
  107.     if GfxBase <> nil then begin
  108.     if OpenMathTrans then begin
  109.         if OpenTheWindow() then begin
  110.         DoCircle(w^.RPort, 320, 105, 92);
  111.         m := WaitPort(w^.UserPort);
  112.         Forbid;
  113.         repeat
  114.             m := GetMsg(w^.UserPort);
  115.         until m = nil;
  116.         CloseWindow(w);
  117.         Permit;
  118.         end else
  119.         writeln('Could not open the window');
  120.         CloseMathTrans;
  121.     end else
  122.         Writeln('Could not open math library');
  123.     CloseLibrary(GfxBase);
  124.     end else
  125.     Writeln('Could not open graphics.library');
  126. end.
  127.