home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / CNC11TP.ZIP / POLYDA.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-01-19  |  2.3 KB  |  63 lines

  1. { Copyright (C) 1989 Adam Fritz, 133 Main St., Afton, N.Y. 13730 }
  2.  
  3. procedure StrokePolygon ( xc, yc : integer ; { center }
  4.                             r, n : word ; { radius and number of sides }
  5.                               ta : single ) ; { rotation angle (rad) }
  6.  
  7.      { PolyDA - draw regular polygon using difference      }
  8.      {          angle method and floating point arithmetic }
  9.  
  10. var
  11.    ixar,iyar        : word ;    { aspect ratio parameters }
  12.    ar               : single ;  { aspect ratio }
  13.    da,cosda,sinda   : single ;  { step angle and functions }
  14.    cosdat2          : single ;  { difference equation coefficient }
  15.    costa,sinta      : single ;  { rotation angle functions }
  16.    i                : integer ; { loop control }
  17.    x0,x1,x2         : single ;  { coordinate variables }
  18.    y0,y1,y2         : single ;  { coordinate variables }
  19.    ix0,iy0          : integer ; { display variables }
  20.  
  21. begin
  22.                                 { constraint test }
  23.    if (r > 0) and (n > 2) then begin
  24.                                 { aspect ratio }
  25.       GetAspectRatio(ixar,iyar) ;
  26.       ar := ixar/iyar ;
  27.                                 { step angle and functions }
  28.       da := 2.0 * Pi / n ;
  29.       cosda := cos(da) ;
  30.       sinda := sin(da) ;
  31.       cosdat2 := 2.0 * cosda ;
  32.                                 { initialize difference equations }
  33.       costa := cos(ta) ;
  34.       sinta := sin(ta) ;
  35.  
  36.       x1 := r * costa ;
  37.       x2 := r * (costa * cosda + sinta * sinda) ;
  38.       y1 := r * sinta * ar ;
  39.       y2 := r * (sinta * cosda - costa * sinda) * ar ;
  40.                                 { starting point with offset }
  41.       ix0 := Round(x1) ;
  42.       iy0 := Round(y1) ;
  43.       MoveTo(xc+ix0,yc+iy0) ;
  44.                                 { polygon }
  45.       for i := 1 to n-1 do begin
  46.                                 { rotate coordinates }
  47.          x0 := cosdat2 * x1 - x2 ;
  48.          y0 := cosdat2 * y1 - y2 ;
  49.                                 { apply aspect ratio }
  50.          LineTo(xc+Round(x0),yc+Round(y0)) ;
  51.                                 { ladder down }
  52.          x2 := x1 ;  x1 := x0 ;
  53.          y2 := y1 ;  y1 := y0
  54.  
  55.       end ;
  56.                                 { closure }
  57.       LineTo(xc+ix0,yc+iy0)
  58.  
  59.    end
  60. end ;
  61.  
  62. { Copyright (C) 1989 Adam Fritz, 133 Main St., Afton, N.Y. 13730 }
  63.