@Outline tracing Extended
--track0:dashPeriod,0,100,100,0.01
--track1:dashSpace,0,100,0,0.01
--track2:Smoothing,0,1000,0,1
--track3:Clearness,0,100,0
--dialog:Shape/fig,fig="Circle";Direction/chk,td=0;Show tip/chk,senp=0;Tip shape/fig,senz="Triangle";Tip size,sens=50;
  enh=1
  ivf=obj.track0*0.01
  ivl=obj.track1*0.01
  sm=math.floor(obj.track2)
  halp=1-obj.track3*0.01

@Outline tracing(Display)
--track0:Drawing,0,100,100
--track1:LineWd,0,1000,10
--track2:StartPt,0,100,0
--track3:Threshold,0,255,128,1
--dialog:Color/col,local col=0xffffff;Reverse spin/chk,local rev=0;Outline only/chk,local rin=0;Below outline/chk,local Rover=0;

  if enh==nil or enh==0 then
    fig="Circle"
    ivf=1
    ivl=0
    td=0
    sm=0
    halp=1
    senp=0
  end

  local hp=obj.track0*0.01
  local lw=obj.track1
  local zure=obj.track2*0.01
  local T=obj.track3
  local dpx={-1, 0, 1, 1, 1, 0,-1,-1}
  local dpy={ 1, 1, 1, 0,-1,-1,-1, 0}
  local dky={ 2^0.5,1,2^0.5,1,2^0.5,1,2^0.5,1}
  local w,h = obj.getpixel()

  local nn=0
  local vold=0
  local ni={}
  local nj={}
  local ky={}
  local r,g,b,a

  for j=0,h-1 do
    for i=0,w-1 do
      r,g,b,a = obj.getpixel(i,j,"rgb")
      if a>T then
        ni[0]=i
        nj[0]=j
        break 
      end
    end
    if a>T then break end
  end

  local res
  local vnew
  local ti
  local tj

  repeat
    res=0
    for i=0,7 do
      vnew=(vold+6+i)%8
      ti=ni[nn]+dpx[vnew+1]
      tj=nj[nn]+dpy[vnew+1]
      if ti>=0 and ti<w and tj>=0 and tj<h then
        r,g,b,a = obj.getpixel(ti,tj,"rgb")
        if a>T then
          nn=nn+1
          ni[nn]=ti
          nj[nn]=tj
          ky[nn]=dky[vnew+1]
          vold=vnew
          if nn==1 then v0=vnew end
          res=1
          break 
        end
      end
    end
  until ( ni[nn-1]==ni[0] and nj[nn-1]==nj[0] and v0==vnew and nn>1) or res==0

  nn=nn-1

  local nmax=math.floor(nn*hp*0.01)
  local ii=math.floor(nn*zure)
  local ALL=0
  for i=1,nn do
    ALL=ALL+ky[i]
  end

  local ttx={}
  local tty={}

  if sm>0 then  
    for i=1,nn do
      ttx[i]=0
      tty[i]=0
      for s=1,sm do
        ii=(nn+i+s-2)%nn+1
        ttx[i]=ttx[i]+ni[ii]
        tty[i]=tty[i]+nj[ii]
      end
      ttx[i]=ttx[i]/sm
      tty[i]=tty[i]/sm
    end
    for i=1,nn do
      ni[i]=ttx[i]
      nj[i]=tty[i]
    end
  end

  obj.setoption("drawtarget","tempbuffer",w+lw,h+lw)

  if rin==0 and Rover~=1 then
    obj.draw(0,0,0,1,halp)
  else
    obj.copybuffer("cache:IMG","obj")
  end

  obj.load("figure",fig,col,lw)

  local ALP=ALL*hp
  local AL=0
  local i=0
  local oz

  if ALP>0 then

    repeat
      i=i+1
      ii=(i+math.floor(nn*zure))%nn
      if rev==0 then
        ii=nn-ii-1
      end
      ii=ii+1
      AL=AL+ky[ii]

      local hanl=AL/ALL
      local count=math.floor(hanl/ivf)

      if count*ivf<hanl and hanl<=(count+1)*ivf-ivl then
        if td==1 then
          oz=math.deg(math.atan2(nj[ii]-nj[ii-1],ni[ii]-ni[ii-1]))
        else
          oz=0
        end
        obj.draw(ni[ii]-w/2,nj[ii]-h/2,0,1,1,0,0,oz)
      end
    until AL>=ALP

    if senp==1 then
      obj.load("figure",senz,col,sens)
      obj.draw(ni[ii]-w/2,nj[ii]-h/2,0,1,1,0,0,oz-90)
    end

  end

  if rin==0 and Rover==1 then
    obj.copybuffer("obj","cache:IMG")
    obj.draw(0,0,0,1,halp)
  end

  enh=0

  obj.load("tempbuffer")

