Jeremy Leach
Senior Member
This is just a little alternative way of working with angles and Sin and Cos....
I'm working with vectors and typically you have to work out the X and Y components of a Vector, where X = VSinA and Y = VCosA
On the picaxes that support Sin and Cos, the angle argument is in degrees. However, code-wise I feel it's far easier to work in 0 to 256 ('binary degrees')instead of 0 to 360. If you are increasing or decreasing an angle it will 'wrap around' simply using binary degrees.
Also, to get best accuracy when performing vector maths, I think it's handy if Sin and Cos are expressed as N/255, where N is a number from a lookup table.
So, for instance, X = VSinA = V * N /255
If V is a byte value and N is a byte value then this calculation will never overflow in picaxe maths, but it also gets maximum accuracy without getting into 32 bit maths etc.
I call these N values Sin255 and Cos255.
Here's a 64 byte EEPROM lookup for the Sin255 values. (0 to 64 binary degrees = 0 to 90 normal degrees).
So to obtain the Sin255 of a binary angle, just do:
In degrees, SinA = Cos(90-A)
So in binary degrees, SinA = Cos(64 - A)
So to obtain the Cos of a binary angle, just do:
Example:
It's just my take on things though !
I'm working with vectors and typically you have to work out the X and Y components of a Vector, where X = VSinA and Y = VCosA
On the picaxes that support Sin and Cos, the angle argument is in degrees. However, code-wise I feel it's far easier to work in 0 to 256 ('binary degrees')instead of 0 to 360. If you are increasing or decreasing an angle it will 'wrap around' simply using binary degrees.
Also, to get best accuracy when performing vector maths, I think it's handy if Sin and Cos are expressed as N/255, where N is a number from a lookup table.
So, for instance, X = VSinA = V * N /255
If V is a byte value and N is a byte value then this calculation will never overflow in picaxe maths, but it also gets maximum accuracy without getting into 32 bit maths etc.
I call these N values Sin255 and Cos255.
Here's a 64 byte EEPROM lookup for the Sin255 values. (0 to 64 binary degrees = 0 to 90 normal degrees).
Code:
EEPROM 0,(0,6,13,19,25,31,37,44,50,56,62,68,74,80,86,92,98,103,109,115,120,126,131,136,142,147,152,157,162,
167,171,176,180,185,189,193,197,201,205,208,212,215,219,222,225,228,231,233,236,238,240,242,244,246,247,
249,250,251,252,253,254,254,255,255,255)
Code:
Read BinAngle,Sin255
So in binary degrees, SinA = Cos(64 - A)
So to obtain the Cos of a binary angle, just do:
Code:
Temp = 64 - BinAngle
Read Temp,Cos255
Code:
BinAngle = 20
Vector = 88
Read BinAngle,Sin255
Temp = 64 - BinAngle
Read Temp,Cos255
Xcomponent = 88 * Sin255 /255
Ycomponent = 88 * Cos255/255
Last edited: