# Maths: ScaleDown (Part 1 of 4)

```       Name: ScaleDown (Part 1 of 4)                                 [Show more]
Type: Subroutine
Category: Maths
Summary: Scale down the results of divisions done using the ScaleUp and
DivideScaled routines
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* ProjectPoint (Part 2 of 3) calls ScaleDown

The commentary in this routine is a work in progress.

Arguments:

UU                   Scale factor for the z-coordinate from ScaleUp routine

VV                   Scale factor for the x-coordinate from ScaleUp routine

WW                   Scale factor for the y-coordinate from ScaleUp routine

(QQ PP)              Result from DivideScaled routine for x-coordinate

(Q P)                Result from DivideScaled routine for y-coordinate

Returns:

(SS QQ)              The correctly scaled x-coordinate

(RR Q)               The correctly scaled y-coordinate

.ScaleDown

LDA #0                 \ Set RR = 0, to use as the high byte in the scaled
STA RR                 \ y-coordinate (RR Q P), where (RR Q) in the final
\ result is the integer, and P is the fractional part
\ (which we discard)

STA SS                 \ Set SS = 0, to use as the high byte in the scaled
\ x-coordinate (SS QQ PP), where (SS QQ) in the final
\ result is the integer, and PP is the fractional part
\ (which we discard)

LDA #7                 \ Set T = 0
STA T

LDA VV                 \ Set X = VV - UU + 1
SEC                    \
SBC UU                 \ so X is the imbalance between the x- and z-coordinates
TAX                    \ in terms of scale factors, and because the division
INX                    \ was x / z, this is the scale factor we need to apply
\ to the x-coordinate, as 2^VV / 2^UU = 2^(VV - UU)

LDA WW                 \ Set Y = WW - UU + 1
SEC                    \
SBC UU                 \ so Y is the imbalance between the y- and z-coordinates
TAY                    \ in terms of scale factors, and because the division
INY                    \ was y / z, this is the scale factor we need to apply
\ to the y-coordinate, as 2^WW / 2^UU = 2^(WW - UU)

CPY #7                 \ If Y < 7, jump to down1
BCC down1

JMP down2              \ Y >= 7, so jump to down2

.down1

CPX #7                 \ If X < 7, jump to down6 to do the scaling with T = 0
BCC down6

.down2

\ If we get here then at least one of X and Y is >= 7

LDA VV                 \ Set A = VV - WW
SEC                    \
SBC WW                 \ so A is the imbalance between the x- and y-coordinates
\ in terms of scale factors

BEQ down3              \ If A = 0, i.e. VV = WW, jump to down3

BPL down4              \ If A > 0, i.e. VV > WW, jump to down4

\ At this point, VV < WW

LDA Q                  \ If (Q P) < 0, jump to down6 to do the scaling with
BMI down6              \ T = 0

JMP down5              \ (Q P) >= 0, so jump to down5

.down3

LDA Q                  \ If (Q P) < 0, jump to down6 to do the scaling with
BMI down6              \ T = 0

.down4

LDA QQ                 \ If (QQ PP) < 0, jump to down6 to do the scaling with
BMI down6              \ T = 0

.down5

\ We get here if at least one of X and Y is >= 7, and
\ any of the following are true:
\
\   * VV = WW and (Q P) >= 0 and (QQ PP) >= 0
\     i.e. x-scale = y-scale and x-coord >= 0 and
\          y-coord >= 0
\
\   * VV > WW and (QQ PP) >= 0
\     i.e. x-scale > y-scale and x-coord >= 0
\
\   * VV < WW and (Q P) >= 0
\     i.e. x-scale < y-scale and y-coord >= 0

INC T                  \ Increment T to 1, so we decrease X and Y by one less
\ in the next part
```