# Drawing lines: DrawClippedLine (Part 1 of 6)

```       Name: DrawClippedLine (Part 1 of 6)                           [Show more]
Type: Subroutine
Category: Drawing lines
Summary: Clip a line to fit on-screen, starting with the line deltas
Deep dive: Line buffers
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* DrawCanopyView calls DrawClippedLine
* DrawHalfHorizon calls via DrawClippedHorizon

Arguments:

L                    The point ID for the line's start point

M                    The point ID for the line's end point

Other entry points:

DrawClippedHorizon   Set bit 1 of the line direction in V (for the horizon)

.DrawClippedHorizon

LDA #%00000010         \ For the horizon, set bit 1 of A to use as the starting
BNE draw1              \ value for the line direction in V (so bit 1 of V gets
\ set)

.DrawClippedLine

LDA #0                 \ Set A = 0 to use as the starting value for the line
\ direction in V

.draw1

STA V                  \ Set V = A to set the starting value for the line
\ direction in V (we will set bits 6 and 7 later)

LDA #0                 \ Set TT = 0, which we will use to store information
STA TT                 \ about whether the line's start point fits on screen

STA UU                 \ Set UU = 0, which we will use to store information
\ about whether the line's end point fits on screen

\ The first step is to calculate the line's |x-delta|
\ and |y-delta| values, updating the line direction
\ information in V as we do so

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 xPointLo,X         \ Set (RR R) = the X-th entry from (xPointHi xPointLo)
STA R                  \
LDA xPointHi,X         \ i.e. the x-coordinate of the line's start point
STA RR

LDA yPointLo,X         \ Set (SS S) = the X-th entry from (yPointHi yPointLo)
STA S                  \
LDA yPointHi,X         \ i.e. the y-coordinate of the line's start point
STA SS

LDA xPointLo,Y         \ Set (QQ W) = the Y-th entry from (xPointHi xPointLo)
STA W                  \
\ starting with the low byte in W

SEC                    \ Set T = W - R
SBC R                  \
STA T                  \ which is the low byte of the calculation:
\
\   (I T) = (QQ W) - (RR R)

LDA xPointHi,Y         \ Set (QQ W) = the Y-th entry from (xPointHi xPointLo)
STA QQ                 \
\ i.e. the x-coordinate of the line's end point

SBC RR                 \ Set I = QQ - RR
STA I                  \
\ so we now have:
\
\   (I T) = (QQ W) - (RR R)
\         = x-coordinate of the line's end point
\           - x-coordinate of the line's start point
\
\ so (I T) is the line's x-delta

BPL draw2              \ If the high byte in I is positive, then so is (I T),
\ so jump to draw2 to skip the following, as the x-delta
\ is positive

\ Otherwise the x-delta is negative, so we need to set
\ the x-direction in V and negate x-delta so it is
\ positive (as we want to calculate |x-delta|)

LDA #%10000000         \ Set bit 7 of V to indicate a negative x-delta in the
ORA V                  \ line direction in V
STA V

LDA #0                 \ Negate (I T), starting with the low bytes
SEC
SBC T
STA T

LDA #0                 \ And then negating the high bytes
SBC I                  \
STA I                  \ so now (I T) is positive, and contains |x-delta|

.draw2

LDA yPointLo,Y         \ Set (H G) = the Y-th entry from (yPointHi yPointLo)
STA G                  \
\ starting with the low byte in G

SEC                    \ Set U = G - S
SBC S                  \
STA U                  \ which is the low byte of the calculation:
\
\   (J U) = (H G) - (SS S)

LDA yPointHi,Y         \ Set (H G) = the Y-th entry from (yPointHi yPointLo)
STA H                  \
\ i.e. the y-coordinate of the line's end point

SBC SS                 \ Set J = H - SS
STA J                  \
\ so we now have:
\
\   (J U) = (H G) - (SS S)
\         = y-coordinate of the line's end point
\           - y-coordinate of the line's start point
\
\ so (J U) is the line's y-delta

BPL draw3              \ If the high byte in J is positive, then so is (J U),
\ so jump to draw3 to skip the following, as the y-delta
\ is positive

\ Otherwise the y-delta is negative, so we need to set
\ the y-direction in V and negate y-delta so it is
\ positive (as we want to calculate |y-delta|)

LDA #%01000000         \ Set bit 6 of V to indicate a negative y-delta in the
ORA V                  \ line direction in V
STA V

LDA #0                 \ Negate (J U), starting with the low bytes
SEC
SBC U
STA U

LDA #0                 \ And then negating the high bytes
SBC J                  \
STA J                  \ so now (J U) is positive, and contains |y-delta|
```