uses console
declare drawline(int x1,y1,x2,y2)
declare tri(int x1,y1,x2,y2,x3,y3)
declare sier(int n,x1,y1,x2,y2,x3,y3)

'dimension
==========
indexbase 1
%  cvx    = 82
%  cvy    = 40
%  cx     = 40
%  cy     = 20
%= nchar = cvx*cvy
string ch=space(nchar) 'char canvas
byte cb at strptr(ch)
int sz=32 'prefer binary 2,4,8,16,32
'
gosub CreateCharCanvas
'gosub MarkCanvasCentre
'gosub Triangle
sier 4, 1,1,33,33,65,1
gosub ShowCanvas
end



sub drawline(int x1,y1,x2,y2)
=============================
y1=cvy-y1 'flip y
y2=cvy-y2 'flip y
int e
int i
int x
int y
float g  '
float gx=x2-x1
float gy=y2-y1
if gy=0 and gx=0
  i=x1+y1*cvx : gosub mark
elseif abs(gx)>=abs(gy)
  g=gy/gx
  e=sgn(gx)
  for x=x1 to x2 step e
    y=y1+g*(x-x1)
    i=x+(y*cvx)
    'cb[i]=35 '#'
    cb[i]=42 '*'
  next
else
  g=gx/gy
  e=sgn(gy)
  for y=y1 to y2 step e
    x=x1+g*(y-y1)
    i=x+(y*cvx) : gosub mark
  next
endif
end sub

sub tri(int x1,y1,x2,y2,x3,y3)
==============================
drawline x1,y1,x2,y2
drawline x2,y2,x3,y3
drawline x3,y3,x1,y1
end sub

sub sier(int n,x1,y1,x2,y2,x3,y3)
=================================
if n>0
tri(int x1,y1,x2,y2,x3,y3) 'main
if n>1
  'macro to create line midpoints
  $ midpoint int x%1%2=(x%1+x%2)>>1, y%1%2=(y%1+y%2)>>1
  midpoint 1 2 : midpoint 1 3 : midpoint 2 3
  tri(x12,y12,x13,y13,x23,y23) 'central
  if n>2
    'corners
    sier(n-1,x1,y1,x12,y12,x13,y13)
    sier(n-1,x2,y2,x23,y23,x12,y12)
    sier(n-1,x3,y3,x23,y23,x13,y13)
  endif
endif
endif
end sub

SUBROUTINES:
============

int d
int i
int j
int k
int m
int x
int y


Mark:
=====
cb[i]=42 '*'
ret

Triangle: 'around central point
===============================
'apex
x=cx
y=(cy-sz\2)*cvx
i=x+y
gosub mark
'sides
for j=1 to sz
  k=x+y+cvx*j
  d=j
  i=k+d : gosub mark 'right side
  i=k-d : gosub mark 'left side
  if j=sz
    for k=1 to sz*2-1
      i++ : gosub mark
    next 
  endif
next
ret


CreateCharCanvas:
=================
for i=1 to len ch
  cb[i]=32 'spaces
next
for i=1 to 40
  j=80+cvx*(i-1)
  cb[j]=13   'cr
  cb[j+1]=10 'lf
next
ret

ShowCanvas:
===========
output ch
pause
ret

MarkCanvasCentre:
====================
cb[cy*cvx+cx]=0x4f 'O'
ret

