
%CONSTANT %INTEGER TRUE=1,FALSE=0
%own %integer comments on = False
%own %longreal path length


%routine frame2(%integer x1,y1,x2,y2)
polygon (0,0)
axy (x1,0)
axy (x1,y1)
axy (0,y1)
axy (0,0)
axy (round ((x1/2)-(x2/2)),round ((y1/2)-(y2/2)))
axy (round ((x1/2)-(x2/2)),round ((y1/2)+(y2/2)))
axy (round ((x1/2)+(x2/2)),round ((y1/2)+(y2/2)))
axy (round ((x1/2)+(x2/2)),round ((y1/2)-(y2/2)))
axy (round ((x1/2)-(x2/2)),round ((y1/2)-(y2/2)))
axy (0,0)
%end






%routine disaster (%String(255) Message)
   Select Output (0)
   Printstring (Message); newline
   %stop
%end









%ROUTINE  TZA(%INTEGER HW,H,LW)
%REAL ANGLE1,ANGLE2
%INTEGER DX,DY,W
W=HW
ANGLE1 = ARCTAN (W,H)
ANGLE2 = ((90/DTOR)-ANGLE1)
DX = INT ( (LW)/ SIN (ANGLE1) ) 
DY = INT ( (LW)/ SIN (ANGLE2) )


POLYGON (0,0)
AXY	(DX,0)
AXY	(W,h)
AXY	(W-DX,H)
AXY (0,0)
%END


%ROUTINE  TZb(%INTEGER HW,H,LW)
%REAL ANGLE1,ANGLE2
%INTEGER DX,DY,W
W=HW
ANGLE1 = ARCTAN (W,H)
ANGLE2 = ((90/DTOR)-ANGLE1)
DX = INT ( (LW)/ SIN (ANGLE1) ) 
DY = INT ( (LW)/ SIN (ANGLE2) )


POLYGON (0,h)
AXY	(DX,h)
AXY	(W,0)
AXY	(W-DX,0)
AXY (0,h)
%END









%routine frame(%integer w,h,lw)
box (0,0,lw,h+(lw*2))
box (0,0,w+(lw*2),lw)
box (w+lw,0,w+(lw*2),h+(lw*2))
box (0,h+lw,w+(lw*2),h+(lw*2))
%end








%ROUTINE  SL (%INTEGER X1,Y1,X2,Y2,W)
%REAL ANGLE
%INTEGER DX,DY
ANGLE = ARCTAN (X2-X1,Y2-Y1)
DX=INT ((W/2)*SIN (ANGLE))
DY=INT ((W/2)*COS (ANGLE))

POLYGON (X1+DX,Y1-DY)
AXY	(X1-DX,Y1+DY)
AXY	(X2-DX,Y2+DY)
AXY	(X2+DX,Y2-DY)
AXY	(X1+DX,Y1-DY)
%END







%routine stroud(%REAL START X, START Y, len, ht, wid,%INTEGER steps, slope)
	%real x,y,xc,T,SQ
	%integer count

	Polygon (ROUND (START X), ROUND (START Y))

	%for count=1,1,steps %cycle
	 xc=count/steps
	 x=xc * len
	 y=ht * xc^3 * ((6*xc-15)*xc+10) * slope
	 axy  (ROUND (START X + X), ROUND (START Y + Y))
	%repeat
	
	%for count=steps,-1,0  %cycle
	 xc=count/steps
	T=30*(XC*(1-XC))^2
	SQ=SQRT (1+T^2) 
	x=xc*len-WID*T/SQ
	 y=(ht*xc^3*((6*xc-15)*xc+10)+wid/SQ) * slope
	 axy ( ROUND (START X + X),ROUND (START Y + Y ))
	%repeat
	
	 axy (ROUND ( START X),ROUND ( START Y ))
	 CIF COMMENT ("END OF SLOPE")
%end








%routine incr path (%longreal by)
   path length = path length + by
!!!   printstring (rtos (by,0,2).", ".rtos (pathlength,0,2)); newline
%end





%string(31) %fn  tolerance (%integer x,y)
   %string(31) temp
   temp = I to S ((x+y)//2,0)." +/- ".I to S (|x-y|//2,0)
   %result = temp
%end









%ROUTINE RS (%STRING(31) THIS SYMBOL,%INTEGER X TIMES,Y TIMES,START X,X STEP,START Y,Y STEP)
%INTEGER I,J, X,Y
X=START X
%FOR I=1,1,X TIMES %CYCLE
Y=START Y
%FOR J=1,1,Y TIMES %CYCLE
DRAW (THIS SYMBOL,X,Y)
Y=Y+Y STEP
%REPEAT
X=X+X STEP
%REPEAT
%END



%routine circle(%integer centre x, centre y, radius, vertices)
   %integer vertex, x, y
   %longreal angle, dangle
   dangle = pi*2/vertices
   angle  = - dangle/2
   x = int(radius * sin (angle))
   y = int(radius * cos (angle))

   Polygon ( centre x + x, centre y + y )

   %for vertex = 1,1,vertices %cycle
      angle = angle + dangle
      x = int(radius * sin (angle))
      y = int(radius * cos (angle))
      axy ( centre x + x, centre y + y )
   %repeat      

   cif comment ("End of Circle")
%end








%routine arc (%integer centre x, centre y, radius, vertices, steps, %c
              %real start angle)
   %integer vertex, x, y
   %longreal angle, dangle
   dangle = 2*pi/vertices
   dangle = -dangle %if steps < 0
   %if comments on = true %then %c
   cif comment ("Arc radius is ".Tolerance (int(radius*cos(dangle/2)),radius))
   angle = start angle - pi/2



   %for vertex = 1,1,|steps| %cycle
      angle = angle + dangle
      x = int(radius * sin (angle))
      y = int(radius * cos (angle))
      axy ( centre x + x, centre y + y )
   %repeat      
%end








%routine ring (%c
   %integer centre x, centre y,outside radius, inside radius, vertices)
   %integer vertex, new radius, ix, iy, oy, ox, liy, lix, lox, loy 
   %longreal angle, dangle, temp1, temp2
   dangle = pi*2/vertices
   temp1 = dangle/2
   temp2 = inside radius
   new radius = int(temp2/cos (temp1))
   angle  = - temp1
   Warning ("Possible Ring Inside-Out") %if new radius >= outside radius
   lix = int(new radius * sin (angle))
   liy = int(new radius * cos (angle))
   lox = int(outside radius * sin (angle))
   loy = int(outside radius * cos (angle))


   %if comments on = true %then %c
   cif comment ("Inner/Outer radii are ".Tolerance(inside radius,new radius).%c
                                     ",".Tolerance(outside radius,loy))  
   %for vertex = 1,1,vertices %cycle
      angle = angle + dangle 
      ix = int(new radius * sin (angle))
      iy = int(new radius * cos (angle))
      ox = int(outside radius * sin (angle))
      oy = int(outside radius * cos (angle))


      Polygon (centre x + ix, centre y + iy)
      axy     (centre x + ox, centre y + oy)
      axy     (centre x + lox , centre y + loy )
      axy     (centre x + lix, centre y + liy )
      axy     (centre x + ix, centre y + iy )

      lix = ix
      liy = iy
      lox = ox
      loy = oy

   %repeat      

   cif comment ("End of Ring")
%end









%routine cont ring seg (%c
   %long %real centre x, centre y,outside radius, inside radius, 
   %integer vertices,
   %long %real start angle, finish angle,
   %integer %name lix, liy, lox, loy)
   %integer vertex, ix, iy, oy, ox, v
   %longreal angle, dangle, new radius, temp1, temp2, dix, diy, dox, doy,
             limit, last angle, mod dangle
   dangle = (finish angle - start angle)/vertices
   mod dangle = |dangle|
   temp2 = inside radius
   temp1 = cos (dangle/2)
   Disaster ("Too Few Steps in Ring") %if temp1 <= 0
   new radius = temp2/temp1
   angle  = start angle
   Warning ("Ring Inside-Out") %if new radius >= outside radius
   temp2 = vertices
   dix = (lix - (centre x + new radius * cos (start angle)))/temp2
   diy = (liy - (centre y + new radius * sin (start angle)))/temp2
   dox = (lox - (centre x + outside radius * cos (start angle)))/temp2
   doy = (loy - (centre y + outside radius * sin (start angle)))/temp2
   limit = arctan (sin (mod dangle) * new radius, 1)   {angle for unit step)
   limit = 2 * limit + mod dangle/100
   %if comments on = true %start
      cif comment ("Misalignment, Inside = ".ftos(dix,3,2).",".ftos(diy,3,2).%c
                              ", Outside = ".ftos(dox,3,2).",".ftos(doy,3,2))
      cif comment ("Angle tolerance = ".ftos(limit,4,2).%c
                   " in ".ftos(dangle,4,2)." Rad")
   %finish

   last angle = start angle
   path length = 0
   %for vertex = 1,1,vertices %cycle
      angle = start angle + vertex * dangle
      v = vertices - vertex
      ix = int(new radius * cos (angle)     + centre x + v*dix)
      iy = int(new radius * sin (angle)     + centre y + v*diy)
      ox = int(outside radius * cos (angle) + centre x + v*dox)
      oy = int(outside radius * sin (angle) + centre y + v*doy)

      Polygon (ix,  iy)
      axy     (ox,  oy)
      axy     (lox,loy)
      axy     (lix,liy)
      axy     (ix,  iy)

      incr path (%c
         sqrt(((lox+lix)/2 - (ox+ix)/2)^2 + ((loy+liy)/2 - (oy+iy)/2)^2))

      %if (ix-lix) # 0 %start
         temp1 = |arc tan (ix-lix,iy-liy)|
      %else %if (iy-liy) # 0
         temp1 = pi/2
      %else 
         temp1 = 0
      %finish
      temp2 = ||temp1 - last angle| - mod dangle|
      %if temp2 > limit %start
         %if temp2 < 1 %start
            Warning ("Possible Ring Angle Error ".Ftos(temp2,4,2)."R") 
         %finish
      %finish
      last angle = temp1
      lix = ix
      liy = iy
      lox = ox
      loy = oy
   %repeat      

   cif comment ("End of Cont Ring Seg")
%end







%routine ring seg (%c
   %long %real centre x, centre y,outside radius, inside radius, 
   %integer vertices,
   %long %real start angle, finish angle)
   %integer  liy, lix, lox, loy
   %longreal new radius, dangle, temp1, temp2
   dangle = (finish angle - start angle)/vertices
   temp1 = dangle/2
   temp2 = inside radius
   new radius = int(temp2/cos (temp1))
   lix = int(new radius * cos (start angle)     + centre x)
   liy = int(new radius * sin (start angle)     + centre y)
   lox = int(outside radius * cos (start angle) + centre x)
   loy = int(outside radius * sin (start angle) + centre y)

   cont ring seg (%c
      centre x, centre y,outside radius, inside radius, vertices,
      start angle, finish angle, lix, liy, lox, loy)
%end








%external %longreal last radius
%external %longreal last angle
%const %integer corners = 75
%routine bend %c
   (%integer start x,start y, wire width, end x, end y, vertices)
   %long %real angle, short, radius, temp path, length, height
   %integer liy, lix, lox, loy, half w, sx,sy,ex,ey
   %if start x < end x %start
      sx = start x
      sy = start y
      ex = end x
      ey = end y
   %else
      sx = end x
      sy = end y
      ex = start x
      ey = start y
   %finish

   length = ex - sx
   %if ey > sy %then height = ey - sy %else height = sy - ey
   angle  = arc tan (length, height)
   last angle = angle
   radius = sqrt(length^2+height^2)/(4*sin(angle))
   last radius = radius
   cif comment ("Bend Radius is ".Itos(int(radius),0))  
   half w = wire width//2
   angle = angle * 2
   short = angle/vertices
   lix = sx; lox = sx
   %if ey > sy %start
      liy = sy + half w
      loy = sy - half w
      cont ring seg (%c
      sx, sy + radius, radius + half w, radius - half w, vertices, %c
       1.5 * pi, 1.5 * pi + angle, lix, liy, lox, loy)
      temp path = path length
      cont ring seg (%c
       sx + length, sy - radius + height, 
       radius + half w, radius - half w, vertices-1, %c
       0.5 * pi + angle, 0.5 * pi + short, lox, loy, lix, liy)
      path length = temp path + path length

      polygon (lox,loy)
          axy (lix,liy)
          axy (ex, ey + half w)
          axy (ex, ey - half w)
          axy (lox,loy)

      incr path (sqrt(((lix+lox)/2-ex)^2 + ((liy+loy)/2-ey)^2))


   %else

      liy = sy - half w
      loy = sy + half w
      cont ring seg (%c
      sx, sy - radius, radius + half w, radius - half w, vertices, %c
       0.5 * pi, 0.5 * pi - angle, lix, liy, lox, loy)
      temp path = path length
      cont ring seg (%c
      sx + length, sy + radius - height, %c
       radius + half w, radius - half w, vertices-1, %c
       1.5 * pi - angle, 1.5 * pi - short, lox, loy, lix, liy)
      path length = temp path + path length

      polygon (lox,loy)
          axy (lix,liy)
          axy (ex, ey - half w)
          axy (ex, ey + half w)
          axy (lox,loy)

      incr path (sqrt(((lix+lox)/2-ex)^2 + ((liy+loy)/2-ey)^2))
   %finish

   cif comment ("End of Bend")
%end


%ROUTINE ROSE(%string (31) NAME,%REAL C1,C2)
%long %real angle,ANGLE2
%integer X
ANGLE=0
ANGLE2=360
%CYCLE
DRAW ANGLED(NAME,0,0,ANGLE)
ANGLE=ANGLE+(C1/C2)
X=INT (ANGLE*1000000)
NEWLINE
CIF COMMENT ("ANGLE  HAD THE VALUE".RTOS(X/1000000,1,7))
%EXIT %IF ANGLE > (ANGLE2-(C1/C2)+1)
%REPEAT
%END




%endoffile
