Aviator on the BBC Micro

# Drawing lines: DrawClippedLine (Part 4 of 6)

```       Name: DrawClippedLine (Part 4 of 6)                           [Show more]
Type: Subroutine
Category: Drawing lines
Summary: Calculate the starting point and direction for our clipped vector
line
Context: See this subroutine in context in the source code
References: No direct references to this subroutine in this source file

\ In this part, the end-goal is to calculate the start
\ point and direction for an on-screen vector line,
\ which we can pass them to the DrawCanopyLine routine
\ later on
\
\ This means we want to calculate a pixel coordinate in
\ (R, S) and a direction in V, by clipping the current
\ start and end points to fit on-screen, if necessary

LDX L                  \ Set X to the point ID for the line's start point

LDY M                  \ Set Y to the point ID for the line's end point

LDA zPointHi,Y         \ If the z-coordinate for the line's end point is
BPL draw10             \ positive, then it's in front of us, so jump to draw10

LDA V                  \ The end point is behind us, so flip bits 6 and 7 in V
EOR #%11000000         \ to reverse the line direction in both axes
STA V

\ The end point is behind us, so we can't use the end
\ point as our vector line's starting point, so now we
\ check whether we can use the start point

LDA TT                 \ If TT is zero then the start point is on-screen, so
BEQ draw15             \ jump to draw15 to use the current values of (R, S) as
\ our pixel coordinate, which works because RR and SS
\ have to be zero for the start point to be on-screen,
\ so (RR R) = (0 R) = R and (SS S) = (0 S) = S, so we
\ can just use the low bytes as the two coordinates,
\ i.e. (R, S)

BNE draw12             \ TT is non-zero, so the start point is off-screen, so
\ jump to draw12 to clip the start of the line so it
\ fits on-screen (this BNE is effectively a JMP as A is
\ never zero)

.draw10

\ If we get here then the end point is in front of us

LDA zPointHi,X         \ If the z-coordinate for the line's start point is
BPL draw11             \ positive, which is in front of us, jump to draw11

JSR SwapLinePoints     \ The start point is behind us and the end point is in
\ front of us, so copy the end point's coordinates and
\ clipping information into the start point, so the
\ start point is now in front of us

LDA TT                 \ If TT is zero then the start point is on-screen, so

BNE draw12             \ TT is non-zero, so the start point is off-screen, so
\ jump to draw12 to clip the start of the line (this BNE
\ is effectively a JMP as A is never zero)

.draw11

\ If we get here then both the start and end points are
\ in front of us

LDA TT                 \ If TT is non-zero then the start point is off-screen,
BNE draw14             \ so jump to draw14 to potentially clip from the end of
\ the line (i.e. if the end point is off-screen)

LDA UU                 \ If UU is non-zero then the end point is off-screen, so
BNE draw15             \ jump to draw13 via draw15 to clip from the end of the
\ line

BEQ draw21             \ Both TT and UU are zero, so both points are on-screen
\ and we don't need to do any clipping, so jump to
\ draw21

.draw12

\ If we get here then the one point is off-screen but
\ in front of us, and the other point is behind us and
\ can't be used as our vector line starting point
\ point, and we've set the start point to be the point
\ that is in front of us

JSR ClipStartOfLine    \ Clip the line at the start to move the start point
\ on-screen, so we can use it as our vector line's
\ starting point

JMP draw15             \ We now have an on-screen pixel coordinate in (R, S),
\ so jump to draw15 to move on to the next stage

.draw13

\ If we get here then both the start and end points are
\ off-screen, but they are both in front of us, so we
\ need to clip both ends of the line

JSR ClipBestEndOfLine  \ Clip the line at either the start or end point,
\ whichever is best, so that it fits on-screen

JMP draw15             \ We now have an on-screen pixel coordinate in (R, S),
\ so jump to draw15 to move on to the next stage

.draw14

\ If we get here then both the start and end points are
\ in front of us and the start point is off-screen

LDA UU                 \ If UU is non-zero then the end point is also
BNE draw13             \ off-screen, so jump to draw13 to clip both ends of the
\ line

\ If we get here then the start point is off-screen but
\ the end point is on-screen, so we now use the end
\ point for our vector line's starting point

LDA V                  \ Flip bits 6 and 7 in V to reverse the line direction
EOR #%11000000
STA V

LDA W                  \ Set (R, S) to the end point's coordinate in (W, G)
STA R
LDA G
STA S
```