osize:		equ	200
maxstretch:	equ	300	;don't forget the table set

*****************************************************************
*	Calculate a huge table full of strecthing values	*
*	Osize should be set to the original size of the object.	*
*	Maxstretch is the widest (or highest) osize will be	*
*	stretched across.					*
*****************************************************************
start:
	move.l	4(a7),a3		base page
	move.l	#mystack,a7
	move.l	$c(a3),d0		text len
	add.l	$14(a3),d0		data len
	add.l	$1c(a3),d0		BSS len
	add.l	#$100,d0		basepage
	move.l	d0,-(sp)
	move.l	a3,-(sp)
	clr.w	-(sp)
	move.w	#$4a,-(sp)
	trap	#1			shrink memory
	lea	12(sp),sp


	pea	fseltitle
	pea	fselsp			;get the PNT file
	jsr	_fileselector
	add.l	#8,sp
	cmpi.l	#1,d0
	bne	exit




stretchcalc:
	lea	array,a0
	move.w	#maxstretch-1,d7
	moveq	#1,d3
scl:
	bsr	plotelipse		;calc table
	bsr	arrange
	addq.l	#1,d3
	dbf	d7,scl


	jsr	_openfile		;open the file
	pea	array
	move.l	#arrayend,d0
	sub.l	#array,d0
	move.l	d0,-(sp)
	jsr	_writetofile		;write it out
	add.l	#8,sp
	jsr	_closefile

	lea	_myfp,a0
.loop:
	tst.b	(a0)+
	bne	.loop
	subq.l	#4,a0
	move.b	#'O',(a0)+
	move.b	#'F',(a0)+
	move.b	#'F',(a0)+
	moveq	#0,d0

	jsr	_openfile		;open the file
	pea	stablestarts
	move.l	#tableend,d0
	sub.l	#stablestarts,d0
	move.l	d0,-(sp)
	jsr	_writetofile		;write it out
	add.l	#8,sp
	jsr	_closefile

exit:
	clr.w	-(sp)
	trap	#1

	include	'file.s'

fseltitle:	dc.b	'name the stretch table',0
fselsp:		dc.b	'TAB',0
	even

arrange:
	movem.l	d0-d7/a1-a6,-(sp)
	move.l	elipseend,a2	;the end of our coord list
*	lea	array,a1
	sub.l	#2,a2		;now pointing at last coord pair
atl:
	move.w	(a2),d0		;are we at the top of the list?
	bmi	atp1		;yep well move on
	move.w	d0,(a0)+
	move.w	d0,d6
	sub.l	#2,a2
	bra	atl
atp1:
	move.w	d6,d1		;save last value
	move.w	d1,d2
	lea	coords,a2	;start at top this time
	add.l	#2,a2
atl2:
	move.w	(a2)+,d0		;are we at the top of the list?
	bmi	atp2		;yep well move on
	cmp.w	d0,d1
	beq	sameold
	addq.w	#1,d2
	move.w	d0,d1
sameold:
	move.w	d2,(a0)+	;store the distance
	bra	atl2
atp2:
	movem.l	(sp)+,d0-d7/a1-a6
	rts

*********************************************************
*	Routine to calculate stretchy stuff		*
*	Pass x width in d3				*
*	block address in d0
*	array to fill in a0				*
*********************************************************
*******************************************************************
a:	dc.l	5
b:	dc.l	20
xe:	dc.l	0
ye:	dc.l	0
asquare:	dc.l	0
bsquare:	dc.l	0
a22:		dc.l	0
b22:		dc.l	0
xslope:		dc.l	0
yslope:		dc.l	0
fmid:		dc.l	0
plotelipse:
	movem.l	d0-a6,-(sp)
	ext.l	d3
	asr.l	#1,d3
	move.l	d3,b
	move.l	#osize,d0
	asr.l	#1,d0
	move.l	d0,a
	move.w	d0,value
	lea	coords,a0
	move.w	#-1,(a0)+
	move.l	a,xe		;x=a
	move.l	#0,ye		;y=0
	move.l	a,d0
	mulu	d0,d0		;asquare=a*a
	move.l	d0,asquare
	move.l	b,d1		;bsquare=b*b
	mulu	d1,d1
	move.l	d1,bsquare
	add.l	d0,d0
	add.l	d1,d1
	move.l	d0,a22		;a22=asquare+asquare
	move.l	d1,b22		;b22=bsquare+bsquare
	move.l	a,d2
	muls.l	d1,d2
	
	move.l	d2,xslope
	move.l	#0,yslope	;yslope=0
	move.l	bsquare,d0
	move.l	a,d1
	neg.l	d1
	muls.l	d1,d0
	
	add.l	asquare,d0
	move.l	d0,fmid		;fmid=bsquare*(.25-a)+asquare
	move.l	xe,d0
	move.l	ye,d1
	move.l	xslope,d2
	move.l	yslope,d3
	move.l	fmid,d4
	move.l	asquare,d5
	move.l	a22,d6
	move.l	b22,d7
eloop1:
	cmp.l	d3,d2
	ble	beginnext

	move.w	value,(a0)+
	addq.l	#1,d1		;y=y+1
	add.l	d6,d3
	tst.l	d4		;if fmid<0
	bgt.s	fmt1
	add.l	d3,d4		;fmid=fmid+yslope+asquare
	add.l	d5,d4
	bra.s	eloop1
fmt1:
	sub.w	#1,value
	subq.l	#1,d0		;x=x-1
	sub.l	d7,d2		;xslope=xslope-b22
	sub.l	d2,d4		;fmid=fmid-xslope+yslope+asquare
	add.l	d3,d4
	add.l	d5,d4
	bra	eloop1

beginnext:
	move.l	d2,d5
	add.l	d3,d5		;(yslope+xslope)/2
	asr.l	#1,d5
	sub.l	d5,d4
	move.l	bsquare,d5
eloop2:
	subq.l	#1,d0		;x=x-1
	sub.l	d7,d2		;xslope=xslope-b22
	tst.l	d4		;if fmid<0
	blt.s	fmt2
	sub.l	d2,d4
	add.l	d5,d4
	tst.l	d0
	bge	eloop2
	move.l	a0,elipseend
	move.w	#-1,(a0)+	;signify end of coord list
	movem.l	(sp)+,d0-a6
	rts
fmt2:
	move.w	value,(a0)+
skipcoadd2:
	addq.l	#1,d1		;y=y+1
	add.l	d6,d3		;yslope=yslope+a22
	sub.l	d2,d4		;fmid=fmid-xslope+yslope+bsquare
	add.l	d3,d4
	add.l	d5,d4
	tst.l	xe
	bge	eloop2
	move.l	a0,elipseend
	move.w	#-1,(a0)+	;signify end of coord list
	movem.l	(sp)+,d0-a6
	rts

elipseend:	dc.l	0
value:		dc.w	0
coords:		ds.w	1000


		ds.l	1000
mystack:	ds.l	1

*****************************************************************
*	This builds a lookup table for the stretch array	*
*	base on factorial (I think!)				*
*****************************************************************
stablestarts:
n	set	1
	rept	300
	dc.l	((n+(n*n))/2)*2		;for words
n	set	n+1
	endr
tableend:

array:		ds.w	(maxstretch+(maxstretch*maxstretch))/2
arrayend:

