w =300; h =160
x0=150; y0= 80
r =100; e =0.4; tt=20
*
[v 1,  32,   39,   8,    21 ]
*
[d 1,  10,   10,   30,   25 ]
*
[c 1,   [ch 255]+[ch   0]+[ch   0]]
[c 2,   [ch   0]+[ch 255]+[ch   0]]
[c 3,   [ch   0]+[ch   0]+[ch 255]]
[c 4,   [ch 255]+[ch 255]+[ch   0]]
*
[c 101, [ch 120]+[ch   0]+[ch   0]]
[c 102, [ch   0]+[ch 120]+[ch   0]]
[c 103, [ch   0]+[ch   0]+[ch 120]]
[c 104, [ch 120]+[ch 120]+[ch   0]]
*
cl =    [ch   0]
cb =    [ch 255]+[ch 255]+[ch 255]
*
f0 = 20
*
drawbar a,w,h, 1,1,w,h, cb
*
call pie
*
makepng a,w,h, 'c:\s.png'
---
stop
*
:pie
array f,r,q,l; ns=0; nl=0
*
vs=0; for i=1 to [v 0] vs=vs+[v i]; endfor
u1=f0
for i=1 to [v 0]
  u2=u1 + 360*[v i]/vs
  p1=[ro u1]; p2=[ro u2]; c1=[c i]; c3=[c i+100]; d=[d i]
  call 3dsec
  u1=u2
endfor
*
sort array r,q
*
*
for i=1 to ns
  q=[q i]
  n=[f q]; f1=[f q+100]; f2=[f q+200]; dx=[f q+300]; dy=[f q+400]
  c1=[f q+500]; c2=cl; c3=[f q+600]
  if     n=1 call 3dsector1
  elseif n=2 call 3dsector2
  elseif n=3 call 3dsector3
  elseif n=4 call 3dsector4
  endif
endfor
for i=1 to nl
  drawline a,w,h, [l i],[l i+100],[l i+200],[l i+300], [l i+400]
endfor
return

:add3dsec
ns=ns+1
[f ns,sn]; [f ns+100,f1]; [f ns+200,f2]; [f ns+300,dx]; [f ns+400,dy]
[f ns+500, c1]; [f ns+600, c3]
f=(f1+f2)/2; if f < 90 f=180-f; endif; [r ns, [abs 270-f]]
return

:addline
nl=nl+1
[l nl,x1+dx]; [l nl+100,y1+dy]; [l nl+200,x2+dx]; [l nl+300,y2+dy];
[l nl+400,c]
return


:3dsec
dx=[ro d*[fcos [rad p1+p2]/2]]; dy=[ro d*e*[fsin [rad p1+p2]/2]]
*
f1=p1; f2=p2
if     (p1 in   0 ..  90) & (p2 in   0 ..  90) sn=1; call add3dsec
elseif (p1 in  90 .. 180) & (p2 in  90 .. 180) sn=2; call add3dsec
elseif (p1 in 180 .. 270) & (p2 in 180 .. 270) sn=3; call add3dsec
elseif (p1 in 270 .. 360) & (p2 in 270 .. 360) sn=4; call add3dsec
*
elseif (p1 in   0 ..  90) & (p2 in  90 .. 180)
  f1= p1; f2= 90; sn=1; call add3dsec
  f1= 90; f2= p2; sn=2; call add3dsec
  x1=x0; y1=y0+1;     x2=x0; y2=y0+e*r-1;    c=c1; call addline
  x1=x0; y1=y0+e*r+1; x2=x0; y2=y0+e*r+tt-1; c=c3; call addline
*
elseif (p1 in   0 .. 90) & (p2 in  180 .. 270)
  f1=180; f2= p2; sn=3; call add3dsec
  f1=p1;  f2= 90; sn=1; call add3dsec
  f1=90;  f2=180; sn=2; call add3dsec
  x1=x0;   y1=y0+1;     x2=x0;     y2=y0+e*r-1;    c=c1; call addline
  x1=x0;   y1=y0+e*r+1; x2=x0;     y2=y0+e*r+tt-1; c=c3; call addline
  x1=x0-1; y1=y0;       x2=x0-r+1; y2=y0;          c=c1; call addline
*
elseif (p1 in   0 .. 90) & (p2 in  270 .. 360)
  f1=270; f2=p2;  sn=4; call add3dsec
  f1=180; f2=270; sn=3; call add3dsec
  f1=90;  f2=180; sn=2; call add3dsec
  f1=p1;  f2=90;  sn=1; call add3dsec
  x1=x0;  y1=y0+1;      x2=x0;     y2=y0+e*r-1;    c=c1; call addline
  x1=x0;  y1=y0-1;      x2=x0;     y2=y0-e*r+1;    c=c1; call addline
  x1=x0;  y1=y0+e*r+1;  x2=x0;     y2=y0+e*r+tt-1; c=c3; call addline
  x1=x0-1;y1=y0;        x2=x0-r+1; y2=y0;          c=c1; call addline
*
elseif (p1 in  90 .. 180) & (p2 in  180 .. 270)
  f1=180; f2=p2;  sn=3; call add3dsec
  f1=p1;  f2=180; sn=2; call add3dsec
  x1=x0-1; y1=y0; x2=x0-r+1; y2=y0; c=c1; call addline
*
elseif (p1 in  90 .. 180) & (p2  in 270 .. 360)
  f1=270; f2=p2;  sn=4; call add3dsec
  f1=180; f2=270; sn=3; call add3dsec
  f1=p1;  f2=180; sn=2; call add3dsec
  x1=x0-1;y1=y0;   x2=x0-r+1; y2=y0;       c=c1; call addline
  x1=x0;  y1=y0-1; x2=x0;     y2=y0-e*r+1; c=c1; call addline
*
elseif (p1 in 180 .. 270) & (p2 in 270 .. 360)
  f1=p1;  f2=270; sn=3; call add3dsec
  f1=270; f2= p2; sn=4; call add3dsec
  x1=x0;  y1=y0-1; x2=x0;     y2=y0-e*r+1; c=c1; call addline
*
elseif (p1 in 270 .. 360) & (p2 in 360 .. 450)
  f1=p1; f2=    360; sn=4; call add3dsec
  f1= 0; f2= p2-360; sn=1; call add3dsec
  x1=x0+1; y1=y0;   x2=x0+r-1; y2=y0;      c=c1; call addline
*
endif
return


:3dsector1
drawarc  a,w,h, x0+dx,y0+dy,    f1,f2, r,e,2, c1
drawarc  a,w,h, x0+dx,y0+dy,    f1,f2, r,e,1, c3
drawarc  a,w,h, x0+dx,y0+tt+dy, f1,f2, r,e,0, c3
drawline a,w,h, x0+dx,y0+dy, x0+dx,y0+tt+dy,  c3
x1=x0+r*[fcos [rad f2]]; y1=y0+tt+e*r*[fsin [rad f2]]
drawline a,w,h, x0+dx,y0+tt+dy, x1+dx, y1+dy, c3
drawline a,w,h, x1+dx,y1+dy, x1+dx,y1-tt+dy,  c3
x2=x0+r*[fcos [rad f1]]; y2=y0+tt+e*r*[fsin [rad f1]]
drawline a,w,h, x2+dx,y2+dy, x2+dx,y2-tt+dy,  c3
for y=y0 to y0+tt drawarc a,w,h, x0+dx,y+dy, f1,f2, r,e,0,c3; endfor
x=x0+r*[fcos [rad f2]]; y=y0+e*r*[fsin [rad f2]]
for t=0 to tt drawline a,w,h, x0+dx,y0+dy+t, x+dx,y+dy+t, c3; endfor
drawarc  a,w,h, x0+dx,y0+dy,    f1,f2, r,e,1, c2
drawarc  a,w,h, x0+dx,y0+tt+dy, f1,f2, r,e,0, c2
drawline a,w,h, x0+dx,y0+dy, x0+dx,y0+tt+dy, c2
drawline a,w,h, x0+dx,y0+tt+dy, x1+dx,y1+dy, c2
drawline a,w,h, x1+dx,y1+dy, x1+dx,y1-tt+dy, c2
drawline a,w,h, x2+dx,y2+dy, x2+dx,y2-tt+dy, c2
return

:3dsector2
drawarc  a,w,h, x0+dx,y0+dy,    f1,f2, r,e,2, c1
drawarc  a,w,h, x0+dx,y0+dy,    f1,f2, r,e,1, c3
drawarc  a,w,h, x0+dx,y0+tt+dy, f1,f2, r,e,0, c3
drawline a,w,h, x0+dx,y0+dy, x0+dx,y0+tt+dy,  c3
x1=x0+r*[fcos [rad f1]]; y1=y0+tt+e*r*[fsin [rad f1]]
drawline a,w,h, x0+dx,y0+tt+dy, x1+dx, y1+dy, c3
drawline a,w,h, x1+dx,y1+dy, x1+dx,y1-tt+dy,  c3
x2=x0+r*[fcos [rad f2]]; y2=y0+tt+e*r*[fsin [rad f2]]
drawline a,w,h, x2+dx,y2+dy, x2+dx,y2-tt+dy,  c3
for y=y0 to y0+tt drawarc a,w,h, x0+dx,y+dy, f1,f2, r,e,0,c3; endfor
x=x0+r*[fcos [rad f1]]; y=y0+e*r*[fsin [rad f1]]
for t=0 to tt drawline a,w,h, x0+dx,y0+dy+t, x+dx,y+dy+t, c3; endfor
drawarc  a,w,h, x0+dx,y0+dy,    f1,f2, r,e,1, c2
drawarc  a,w,h, x0+dx,y0+tt+dy, f1,f2, r,e,0, c2
drawline a,w,h, x0+dx,y0+dy, x0+dx,y0+tt+dy, c2
drawline a,w,h, x0+dx,y0+tt+dy, x1+dx,y1+dy, c2
drawline a,w,h, x1+dx,y1+dy, x1+dx,y1-tt+dy, c2
drawline a,w,h, x2+dx,y2+dy, x2+dx,y2-tt+dy, c2
return

:3dsector3
drawarc  a,w,h, x0+dx,y0+dy,    f1,f2, r,e,2, c1
drawarc  a,w,h, x0+dx,y0+dy,    f1,f2, r,e,1, c3
drawline a,w,h, x0+dx,y0+dy, x0+dx,y0+tt+dy,  c3
x1=x0+r*[fcos [rad f1]]; y1=y0+tt+e*r*[fsin [rad f1]]
drawline a,w,h, x0+dx,y0+tt+dy, x1+dx, y1+dy, c3
drawline a,w,h, x1+dx,y1+dy, x1+dx,y1-tt+dy,  c3
x=x0+r*[fcos [rad f1]]; y=y0+e*r*[fsin [rad f1]]
for t=0 to tt drawline a,w,h, x0+dx,y0+dy+t, x+dx,y+dy+t, c3; endfor
drawarc  a,w,h, x0+dx,y0+dy,    f1,f2, r,e,1, c2
drawline a,w,h, x0+dx,y0+dy, x0+dx,y0+tt+dy, c2
drawline a,w,h, x0+dx,y0+tt+dy, x1+dx,y1+dy, c2
drawline a,w,h, x1+dx,y1+dy, x1+dx,y1-tt+dy, c2
return

:3dsector4
drawarc  a,w,h, x0+dx,y0+dy,    f1,f2, r,e,2, c1
drawarc  a,w,h, x0+dx,y0+dy,    f1,f2, r,e,1, c3
drawline a,w,h, x0+dx,y0+dy, x0+dx,y0+tt+dy,  c3
x1=x0+r*[fcos [rad f2]]; y1=y0+tt+e*r*[fsin [rad f2]]
drawline a,w,h, x0+dx,y0+tt+dy, x1+dx, y1+dy, c3
drawline a,w,h, x1+dx,y1+dy, x1+dx,y1-tt+dy,  c3
x=x0+r*[fcos [rad f2]]; y=y0+e*r*[fsin [rad f2]]
for t=0 to tt drawline a,w,h, x0+dx,y0+dy+t, x+dx,y+dy+t, c3; endfor
drawarc  a,w,h, x0+dx,y0+dy,    f1,f2, r,e,1, c2
drawline a,w,h, x0+dx,y0+dy, x0+dx,y0+tt+dy, c2
drawline a,w,h, x0+dx,y0+tt+dy, x1+dx,y1+dy, c2
drawline a,w,h, x1+dx,y1+dy, x1+dx,y1-tt+dy, c2
return