  % Title "Intersections Demo"
 '% Animated
 '% ScaleUp
 '% PlaceCentral
 '% AnchorCentral
  uses consoleG
  uses GLO2\GeoPlanar
  uses chaos

  sub boundaries()
  ================
  indexbase 0

  string tab="    "
  point  p
  int    a,i,j,k

  seed=0x123567

  line  d[360]
  int c,n
  CreatePolylines(d[0],0,-.0, 120,1.5,1.0)
  CreatePolylines(d[120],0,-.2, 120,0.7,0.7)
  CreatePolylines(d[240],0,-.2, 120,0.3,0.3)
  n=360
  



  'shading
  'flat 'default
  '
  color 0,1,1
  'printl "Inside " tab p.x tab p.y

  scale 10,10

  move 1.5,-1.2
  '
  color 0,1,1

  for i=0 to n-1
    drawline @d[i]
  next
  '
  point pio
  point pd[360]
  int ex[100],exc
int scl
  int pc
  line dd 'each hatch line
  float gx,gy, 'gradient of hatch line
  float lx,ly  'line ends
  'hatch
  ------
  pio={-2,-2}
  gx=1.0
  gy=0.0
  lx=100*gx
  ly=100*gy
  for j=-40 to 40
    pio.x+=0.1
    pio.y+=0.1
    dd={ -lx+pio.x, -ly+pio.y, lx+pio.x,ly+pio.y }
    gosub LinesInside
  next
  '
'cross hatch
------------
  pio={2,-2}
  gx=0 '-1.0
  gy=1.0
  lx=100*gx
  ly=100*gy
  for j=-40 to 40
    pio.x-=0.1
    pio.y+=0.1
    dd={ -lx+pio.x, -ly+pio.y, lx+pio.x,ly+pio.y }
    gosub LinesInside
  next
  '
'mbox "pio " pio.y
  exit sub
  '
  LinesInside:
  ============
  color 1,1,0
  'pointsize 4
  pc=0
  for i=0 to n-1
   'a=intersectedp(@d[i],@dd,@p)
   a=intersected d[i],dd,p
   if a=16
     pd[pc]=p
     pc++
'scl++
   endif
  next
  '
  int e=pc-1

  'sort points in ascending order
  exc=0
  float ic,kc
  int ib,kb
  for i=0 to e-1
    if gx 
      ic=gx*pd[i].x
    else
      ic=gy*pd[i].y
    endif
    for k=i+1 to e
      if gx 
        kc=gx*pd[k].x
      else
        kc=gy*pd[k].y
      endif
      if ic>kc
        swap pd[i],pd[k]
        swap kc,ic
      endif
    next
  next

  if e>=1
    'draw interior lines
    color 1,1,0
    for i=0 to e step 2
      k=i+1
      if k<=e
        dd={ pd[i].x,pd[i].y, pd[k].x,pd[k].y }
        drawline @dd
      endif
    next
'scl++
  endif
  ret  
 
  end sub


  sub main()
  ==========
  string s
  cls 0,0,0
  pushstate
  scale 2,2
  'typeface=1
  printl "Inside / Outside Hatching"
  popstate
  '
  move 0,-2
  '
  PushState
  move 1,-1
  boundaries()
  PopState
  '
  
  end sub

  EndScript
