Aviator on the BBC Micro

# Drawing lines: DrawHalfHorizon

```       Name: DrawHalfHorizon                                         [Show more]
Type: Subroutine
Category: Drawing lines
Summary: Draw half of the horizon line
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* DrawCanopyView calls DrawHalfHorizon

The horizon line is line ID 0, which is the line from point 31 to point 30.
This line is only half the actual horizon, so this routine calculates the
other half of the horizon into point 30, and draws the line from point 30 to
point 32.

Point 30 is therefore the mid-point of the two-segment horizon line, which
goes point 31 to point 30 to point 32.

The calculation looks like this:

+  (x_32, y_32)
.´ |
.´   |  h
.´     |
(x_30, y_30) .´_______|
.´|    w
.´  |
.´    | h
.´      |
(x_31, y_31)   ´--------+
w

We already know the coordinates of points 30 and 31, and we want to calculate
point 32. It's easy to calculate w and h in the bottom triangle:

w = x_30 - x_31
h = y_30 - y_31

so to extend the line to point 32, we simply add w and h to the coordinates
for point 30, like this:

x_32 = x_30 + w
= x_30 + x_30 - x_31
= (x_30 * 2) - x_31

y_32 = y_30 + h
= y_30 + y_30 - y_31
= (y_30 * 2) - y_31

This routine draws the horizon from point 30 to point 32, while the other half
from point 31 to 30 is drawn along with all the other lines in DrawCanopyView.

.DrawHalfHorizon

LDX #30                \ Set X = 30, to use as the point ID for point 30, which
\ is the start of the horizon line

LDY #32                \ Set Y = 32, to use as the point ID for point 32

LDA xPointHi,X         \ Set (T A) = x_30
STA T
LDA xPointLo,X

ASL A                  \ Set (T A) = (T A) * 2
ROL T                  \           = x_30 * 2

SEC                    \ Set x_32 = (T A) - x_31
SBC xPointLo+31        \          = (x_30 * 2) - x_31
STA xPointLo,Y
LDA T
SBC xPointHi+31
STA xPointHi,Y

LDA yPointHi,X         \ Set (T A) = y_30
STA T
LDA yPointLo,X

ASL A                  \ Set (T A) = (T A) * 2
ROL T                  \           = y_30 * 2

SEC                    \ Set y_32 = (T A) - y_31
SBC yPointLo+31        \          = (y_30 * 2) - y_31
STA yPointLo,Y
LDA T
SBC yPointHi+31
STA yPointHi,Y

\ So now we have:
\
\   x_32 = (x_30 * 2) - x_31
\   y_32 = (y_30 * 2) - y_31
\
\ which is what we want

STX L                  \ Set L to point 30, so we draw the line from this start
\ point

STY M                  \ Set L to point 32, so we draw the line to this end
\ point

JSR DrawClippedHorizon \ Draw the line from point 30 to 32, which is the first
\ half of the horizon line

RTS                    \ Return from the subroutine
```