Author Topic: Sin / Cos arrays  (Read 3527 times)

0 Members and 1 Guest are viewing this topic.

Offline Storm Trooper

  • C= 64
  • **
  • Posts: 45
  • Karma: 2
    • View Profile
Sin / Cos arrays
« on: July 06, 2010 »
you know when you use arrays to work out the sin and / or cos?

Code: [Select]
   
angle1 += 0.856f
angle2 += 0.252f

dim as single sinus, ii
for i as integer=0 to TOTALLY-1
ii=csng(i)   
sinus=(5.02f + (cos( angle1+(ii/1.27f) ) * sin( angle2+(ii*2.49f) )*0.444f))
next

im after a way to use that as an array, maybe I need two. im stuck with having the angle changing and it using the loop variable. im not sure if sin cos works 0-359.

if you can help, big thanks! :N

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Sin / Cos arrays
« Reply #1 on: July 06, 2010 »
Looks like you're coding in VB?  That means that the built in Sin and Cos functions take radians - a full circle is defined as 0 -> 2xPI.

Let's say you need a small amount of angles, say 1024 separate points round the circle, then you can define your arrays like this:
Code: [Select]
Dim Zin(1023) as single
Dim Coz(1023) as single
angle = 0.0f
for i as integer=0 to 1023
Zin(i) = Sin(angle)
Coz(i) = Cos(angle)
angle += 2.0f*3.1415f/1024.0f
next

Now you have the arrays, you need to be able to retrieve the angles.  You have two choices, either you can work in '1024s' - rework your code to use 1024 degrees in a circle and use the arrays directly - or you can continue to work in radians and scale everything up.
You also need to make sure that once the number is scaled it will be in the range 0-1023.
Code: [Select]
function getSin(byval radians as single) as single
  dim index as uinteger
  index = radians * 1024.0f / (2.0f * 3.1415f) : rem you probably want to store this constant somewhere
  index = index AND 1023
  return Zin(index)
end function
Do the same for cos.

I chose 1024 because it is a power of 2, which means we can use the AND trick you asked about before to do the remainder.  If you need more resolution, go to a bigger power of 2.  If you used 65536 then you can even get the AND for 'free'
Code: [Select]
function getSin(byval radians as single) as single
  dim index as ushort
  index = radians * 65536.0f / (2.0f * 3.1415f) : rem you probably want to store this constant somewhere
  return Zin(index)
end function
That's because 'ushort' can only fit in values of 0-65535
Jim






« Last Edit: July 06, 2010 by Jim »
Challenge Trophies Won: