;	Perlin Production Co. Ltd. August 1993 (c).
;	High-resolution LINE procedure for C 1.2.3 compiler.

public	$line
extrn	$SETCOL
extrn	$ELF

MACLIB	INTRFACE.LIB

DRAW	MACRO	PXS,PYS,PDX,PDY,SLOW,FAST
LOCAL	NONEGC
LOCAL	LPIXEL
	lhld	PDY		;	NEGCOR= - DY * 2
	dad	h
	cmh
	shld	NEGCOR
	lhld	PDX		;	POSCOR= DX * 2
	dad	h
	mov	b,h
	mov	c,l
	lhld	PDY;		;	ERROR = POSCOR - DY
	cmh
	dad b
	xchg
	lhld	PDY;		;	COUNTER=DY+1
	inx	h
;
;	<HL>	-	COUNTER
;	<DE>	-	ERROR
;	<BC>	-	positive_error_correction

lpixel:	push h
	call XPLOT

;	ERROR+=positive_error_correction

	mov a,d
	xchg
	dad b
	xchg

;	if ERROR < 0 then no_negative_correction_needed

	cpi 0;		maybe ORA A ?
	jm NONEGC

;	else	ERROR+=negative_error_correction

	lhld NEGCOR
	dad d
	xchg

;	XSTART++

	lhld PXS
	SLOW h
	shld PXS

NONEGC:

;	YSTART++

	lhld PYS
	FAST h
	shld PYS

;	COUNTER--

	pop h
	dcx h
	mov a,h
	ora l
	jnz LPIXEL
ENDM



$line:
	lda	$ELF
	ora	a
	rz
	getint XSTART,4
	getint YSTART,3
	getint XEND,2
	getint YEND,1
	
	INITGRAPH
	cmpw	YSTART,YEND;		if ( YSTART < YEND ) goto LIN0
	jc	LIN0
	exchg	XSTART,XEND
	exchg	YSTART,YEND

;	/*	YSTART < YEND	*/

LIN0:	subw	DX,XEND,XSTART	;	if ( (DX=XEND-XSTART) < 0 ) goto LIN10
	jc	LIN10
	subw	DY,YEND,YSTART	;	DY=YEND-YSTART
	cmpw	DY,DX		;	if (DY < DX) goto LIN5
	jc	LIN5
	DRAW	XSTART,YSTART,DX,DY,inx,inx
	CLOSEGRAPH
LIN5:	DRAW	YSTART,XSTART,DY,DX,inx,inx
	CLOSEGRAPH
LIN10:	subw	DX,XSTART,XEND
	subw	DY,YEND,YSTART
	cmpw	DY,DX
	jc	LIN15
	DRAW	XSTART,YSTART,DX,DY,dcx,inx
	CLOSEGRAPH
lin15:	DRAW	YSTART,XSTART,DY,DX,inx,dcx
	CLOSEGRAPH




xplot:	push h
	push d
	push b
	lxi h,XPL2+1
	mvi m,0c0h
	lhld XSTART
	mov a,h
	rrc
	mov a,l
	rar
	mov d,a;	Horizontal coordinate
	jnc XPL1
	lxi h,XPL2+1
	mvi m,0a0h
XPL1:	lxi h,YSTART
	mov e,m;	Vertical coordinate
	lxi h,PIXTAB
	mvi b,0
	mov a,d
	ani 7
	mov c,a
	dad b
	mov b,m;	Masc from PIXTAB
	mov a,d
	ani 0f8h
	rrc
	rrc
	rrc
XPL2:	adi 0c0h
	mov h,a;	Real horizontal coordinate: PANEL_BASE+(0..1F)
	mov l,e;	Vertical coordinate
	lda $SETCOL
	ani 6
	mov a,b
	jz XPL3
	ora m
	mov m,a
	pop b
	pop d
	pop h
	ret
XPL3:	cma
	ana m
	mov m,a
	pop b
	pop d
	pop h
	ret


XSTART:	DW	0
YSTART:	DW	0
XEND:	DW	0
YEND:	DW	0
DX:	DW	0
DY:	DW	0
NEGCOR:	DW	0;	Negative correction
PIXTAB:	DB	80H,40H,20H,10H,8,4,2,1

	END
