Skip to content

Instantly share code, notes, and snippets.

@mateoconlechuga
Last active November 5, 2016 05:35
Show Gist options
  • Save mateoconlechuga/f3f25a2fa385dee370e4d969de3510df to your computer and use it in GitHub Desktop.
Save mateoconlechuga/f3f25a2fa385dee370e4d969de3510df to your computer and use it in GitHub Desktop.
;-------------------------------------------------------------------------------
_Line:
; Draws an arbitrarily clipped line
; Arguments:
; arg0: x0
; arg0: y0
; arg0: x1
; arg0: y1
; Returns:
; true if drawn, false if offscreen
ld iy,0
add iy,sp
push hl
ld hl,(iy+3) ; x0
ld de,(iy+6) ; y0
call _ComputeOutcode_ASM \.r
ld (iy+-1),a
ld hl,(iy+9) ; x1
ld de,(iy+12) ; y1
call _ComputeOutcode_ASM \.r
ld (iy+-2),a
CohenSutherlandLoop:
ld b,(iy+-1) ; b = outcode0
ld a,(iy+-2) ; a = outcode1
tst a,b
jp nz,TrivialReject \.r ; if(outcode0|outcode1)
or a,a
jr nz,GetOutOutcode
or a,b
jp z,TrivialAccept \.r
GetOutOutcode: ; select correct outcode
push af ; a = outoutcode
rra
jr nc,NotTop ; if (outcodeOut & TOP)
ld hl,(_ymax) \.r
dec hl ; inclusive
jr ComputeNewX
NotTop:
rra
jr nc,NotBottom ; if (outcodeOut & BOTTOM)
ld hl,(_ymin) \.r
ComputeNewX:
push hl
ld bc,(iy+6)
or a,a
sbc hl,bc ; ymax_ymin - y0
ex de,hl
ld hl,(iy+9)
ld bc,(iy+3)
or a,a
sbc hl,bc ; x0 - x1
call __imulsDE_ASM \.r
ex de,hl ; (x0 - x1)*(ymax_ymin - y0)
ld hl,(iy+12)
ld bc,(iy+6)
or a,a
sbc hl,bc ; y1 - y0
push hl
pop bc
ex de,hl
call __idivs_ASM \.r ; ((x0 - x1)*(ymax_ymin - y0))/(y1 - y0)
ld bc,(iy+3)
add hl,bc ; (x) hl = x0 + ((x0 - x1)*(ymax_ymin - y0))/(y1 - y0)
pop de ; (y) de = ymax_ymin
jr FinishComputations
NotBottom:
rra
jr nc,NotRight ; if (outcodeOut & RIGHT)
ld hl,(_xmax) \.r
dec hl ; inclusive
jr ComputeNewY
NotRight:
rra
jr nc,FinishComputations ; if (outcodeOut & LEFT)
ld hl,(_xmin) \.r
ComputeNewY:
push hl
ld bc,(iy+3)
or a,a
sbc hl,bc ; xmax_xmin - x0
ex de,hl
ld hl,(iy+12)
ld bc,(iy+6)
sbc hl,bc ; x1 - x0
call __imulsDE_ASM \.r
ex de,hl ; (x1 - x0)*(xmax_xmin - x0)
ld hl,(iy+9)
ld bc,(iy+3)
or a,a
sbc hl,bc ; y1 - y0
push hl
pop bc
ex de,hl
call __idivs_ASM \.r ; ((x1 - x0)*(xmax_xmin - x0))/(y1 - y0)
ld bc,(iy+6)
add hl,bc
ex de,hl ; (y) de = y0 + ((x1 - x0)*(xmax_xmin - x0))/(y1 - y0)
pop hl ; (x) hl = ymax_ymin
FinishComputations:
pop af
cp a,(iy+-1)
jr nz,OutcodeOutIsOutcode1
ld (iy+3),hl
ld (iy+6),de
call _ComputeOutcode_ASM \.r
ld (iy+-1),a ; b = outcode0
jp CohenSutherlandLoop \.r
OutcodeOutIsOutcode1:
ld (iy+9),hl
ld (iy+12),de
call _ComputeOutcode_ASM \.r
ld (iy+-2),a ; c = outcode1
jp CohenSutherlandLoop \.r
TrivialAccept:
dec iy
call _Line_NoClip_ASM \.r
TrivialReject:
inc sp
inc sp
inc sp
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment