Skip to navigation

Revs on the BBC Micro

Maths (Geometry): HalveCoordinate

Name: HalveCoordinate [Show more] Type: Subroutine Category: Maths (Geometry) Summary: Halve a coordinate with three 16-bit axes
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * BuildCarObjects (Part 3 of 3) calls HalveCoordinate

Given a three-axis variable, this routine halves each axis in-place: [ (SS T) ] [ (SS T) ] [ (TT U) ] = [ (TT U) ] / 2 [ (UU V) ] [ (UU V) ]
Arguments: (SS T) The value of the coordinate's first axis (TT U) The value of the coordinate's second axis (UU V) The value of the coordinate's third axis
.HalveCoordinate LDX #2 \ We are about to right-shift the following 16-bit \ variables: \ \ (SS T) \ (TT U) \ (UU V) \ \ so set a counter in X to use as an index that loops \ through 2, 1 and 0, as: \ \ (TT U) = (SS+1 U+1) \ (UU V) = (SS+2 U+2) \ \ The following comments are for (SS T), but the same \ process applies for (TT U) and (UU V) .halc1 LDA SS,X \ Set A to the high byte of (SS T) CLC \ If A is negative, set the C flag, otherwise clear the BPL halc2 \ C flag, so this sets the C flag to the sign of (SS T) SEC .halc2 ROR SS,X \ Set (SS T) = (SS T) >> 1 ROR T,X DEX \ Decrement the loop counter to move on to the next \ variable BPL halc1 \ Loop back until we have shifted all three 16-bit \ variables to the right RTS \ Return from the subroutine