m15o

Drawing a line

I can't even count how many hours I've spent writing the following blob in tal. It draws a line using Bresenham's [1] algorithm. It doesn't handle all the cases (e.g. x1 needs to be > x2), but I'm fairly happy with it.


@line ( x1* y1* x2* y2* c -- )
	,&color STR ROT2 LTH2k DUP ,&dec STR #01 EOR #06 JCN SWP2
	SUB2k NIP2 OVR2 ADD2 SWP2 STH2 STH2 SWP2 OVR2 OVR2 STH2 STH2
	SUB2 ROT2r STH2r ROT2r STH2r OVR2 OVR2 STH2 STH2 SUB2 GTH2
	,&high JCN #01 ,&mode STR SWP2r STH2r ROT2r STH2r STH2r STH2r
	,&paint JMP &high SWP2r STH2r ROT2r STH2r SWP2 STH2r STH2r
	SWP2 ,&paint JMP BRK &mode $1 &dec $1 &color $1 &paint ROT2
	SUB2k STH2 NIP2 ROT2 ROT2 SWP2 SUB2k DUP2 2** STH2rk ADD2 SWP2
	STH2 STH2 SWP2 &loop GTH2k ,&end JCN ,&mode LDR ,&inv JCN SWP2
	DUP2 .Screen/x DEO2 ROT2 DUP2 .Screen/y DEO2 ,&cont JMP &inv
	SWP2 DUP2 .Screen/y DEO2 ROT2 DUP2 .Screen/x DEO2 &cont
	,&color LDR .Screen/pixel DEO SWP2 INC2 STH2r DUP2 STH2r DUP2
	#0003 MUL2 ROT2 SWP2 LTH2 ,&no-inc-y JCN DUP2 ROT2 STH2rk 2**
	ADD2 SWP2 2** SUB2 STH2 STH2 SWP2r SWP2 ,&dec LDR ,&s JCN INC2
	,&c JMP &s #0001 SUB2 &c SWP2 ROT2 ,&loop JMP &no-inc-y STH2
	SWP2r STH2r SWP2 OVR2 2** ADD2 STH2 STH2 ROT2 ROT2r ROT2r
	,&loop JMP &end POP2r POP2r POP2r
RTN

[1] Bresenham line algorithm

EOF

Written with Smol Pub
From Dusk's End, Nightfall City

m15o@posteo.net