Revs on the BBC Micro

```       Name: AddScaledVector                                         [Show more]
Type: Subroutine
Category: Maths (Geometry)
Summary: Add a scaled vector to another vector, one axis at a time
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:

This routine does the following calculation, one axis at a time (starting with
the z-axis, then the y-axis and x-axis):

[ xRoadSignCoord ]   [ xPlayerCoord ]   [ xTrackSignVector * 2 ^ (8 - Y) ]
[ yRoadSignCoord ] = [ yPlayerCoord ] - [ yTrackSignVector * 2 ^ (8 - Y) ]
[ zRoadSignCoord ]   [ zPlayerCoord ]   [ zTrackSignVector * 2 ^ (8 - Y) ]

The value of Y can be varied between calls to change the scale factor on a
per-axis basis.

Arguments:

A                    The relevant from xTrackSignVector, yTrackSignVector,
zTrackSignVector

W                    Set to 2 for the first call

Y                    The scale factor for this axis

PHA                    \ Set (V A T) = (0 A 0)
LDA #0                 \             = A * 256
STA T
STA V
PLA

BPL addv1              \ If A is positive then V is already the correct high

DEC V                  \ Otherwise, decrement V to &FF so it's the correct
\ high byte for (V A T)

\ We now shift (V A T) right by Y places

LSR V                  \ Set (V A T) = (V A T) >> 1
ROR A
ROR T

DEY                    \ Decrement the shift counter

BNE addv1              \ Loop back until we have right-shifted Y times

STA U                  \ Set (V U T) = (V A T)
\             = A * 256 >> Y
\             = A * 2 ^ (8 - Y)
\
\ We know that V doesn't contain any data, just sign
\ bits, so this means:
\
\   (U T) = A * 2 ^ (8 - Y)
\         = xTrackSignVector * 2 ^ (8 - Y)

LDY W                  \ Fetch the axis index from W

DEC W                  \ Decrement the axis index in W, ready for the next call