Dark Basic/Functions

Just some miscellaneous functions that might come in handy while your developing your applications. You never know when you might need a Fibonacci series in your program or perform a factorial of a number. The list even contains some logarithmic functions that i think may already exist in the dark basic command list. Well anyways, it's always good to know where this stuff comes from, you can call it function appreciation if you like.

THE FUNCTIONS



FIBONACCI SERIES FUNCTION
A Fibonacci series is basically a set of numbers for which every number is derived from a sum between it's two immediate predecessors. Take the series of numbers 1,1,2,3,5,8,13 for example. Now for each number in that series, the value was calculated by summing the 2 numbers immediately before it. So 5 = 3 + 2, 13 = 8 + 5, and so on and so forth. What the function below does is to give you the value at a particular index within an array of Fibonacci values.

function FIB(n)

   if n = 1 or n = 2 then exitfunction 1
   a = FIB(n-2)
   b = FIB(n-1)
   c = a + b

endfunction c

FACTORIAL FUNCTION
The Factorial (symbol '!') of a number is calculated by simply multiplying the number by a series of values, each value being a continuous decrement of the original value by 1. Take the number 5 for example, the Factorial of 5, 5! would be 5 x 4 x 3 x 2 x 1 = 120. So basically keep multiplying the number by an ever decreasing version of itselft till you get to 1. For small values this is easy but for big values your results get huge quickly.

function FACT(n)

   if n = 1 then exitfunction 1
   a = n * FACT(n-1)

endfunction a

MODULUS FUNCTION
Everyone should know the Modulus function or maybe by its other name, the remainder function. It basically returns the remainder when one integer number is divided by another integer number. The function below makes use of another function called INT which returns the integer section of a number with a decimal point (does not round up).

function MOD(n1, n2)

   if n1 = n2 then exitfunction 0
   if n2 > n1 then exitfunction n2-n1
   a = n1 - (n2 * INT(n1/n2))

endfunction a

2D/3D DISTANCE CALCULATION FUNCTION
Some find it pretty straightforward others have problems remembering it. Either way below are a pair of functions that calculate the 2D and 3D distance between two points in space whe you supply the coordinate positions of these points.

function DIST2D(x1, y1, x2, y2)

   dx = (x1 - x2) * (x1 - x2)
   dy = (y1 - y2) * (y1 - y2)
   dt = sqrt(dx+dy)

endfunction dt
function DIST3D(x1, y1, z1, x2, y2, z2)

   dx = (x1 - x2) * (x1 - x2)
   dy = (y1 - y2) * (y1 - y2)
   dz = (z1 - z2) * (z1 - z2)
   dt = sqrt(dx+dy+dz)

endfunction dt

LOGARITHMIC FUNCTIONS
The logarithm of a number N to a given base B is the power to which that base must be raised in order to produce the number N. It has many uses in Mathematical calculations and as such is on this page. Many people might know what its used for and how to use it but bet you didn't know how to raw-code it. The natural logarithm function is also below. This code was supplied by Kevin Picone on the 15th of September, 2002.

function LOG(base#, numb#)

   log# = sqrt(numb#/base#)
   repeat
      log# = log# - (((base#^log#)-numb#)/(base#^log#*LN(base#)))
   until abs((base#^log#)-numb#) <= 0.001

endfunction log#
function LN(numb#)

   e# = 2.71
   ln# = sqrt(numb#/e#)
   repeat
      ln# = ln#-(((e#^ln#)-numb#)/(e#^ln#))
   until abs((e#^ln#)-numb#)#<=#0.001

endfunction ln#

TIME FACTOR FUNCTION
The time factor is basically a value most people calculate once every loop within a real- time application that gives an indication of how much time it took for the application to progress through one loop. It's normally represented in milliseconds i.e. 1/1000 seconds. Note that for this function, variables must be persistent to remember the internal system time from a previous program loop. In the example below, ot# must be persistent so it always contains the internal system time value. Using the fime factor value to adjust all motion related variables in your application would ensure that movement is the same regardless of the specifications of the host computer.

function TFACT()

   nt# = timer()
   dt# = (nt# - ot#)/1000.0
   if dt# > 0.05 then dt# = 0.05
   if dt# < 0.01 then dt# = 0.01
   ot# = timer()
   
endfunction dt#

CURVE (INTERPOLATION) FUNCTIONS
Dark Basic has some cool functions, curvevalue and curveangle that delivers smooth interpolated values in both linear and angular situations. The problem is however that these functions dont work with the time factor value presented above. This means that when it comes to using these functions, your program may run faster or slower than expected on different computers. A solution was found by Joseph Thomson and i present his modified versions of this code below.

function TCurveValue(dest#, current#, speed#, tfact#)

   diff# = dest# - current#
   value# = current# + (diff# * ((speed#^tfact#-(speed#-1.0)^tfact#)/(speed#^tfact#)))
   
endfunction value#
function TCurveAngle(dest#, current#, speed#, tfact#)

   tempCurrent# = wrapvalue(current# - dest#)

   if tempCurrent# <= 180.0
      diff# = -tempCurrent#
   else
      diff# = 360.0 - tempCurrent#
   endif

   temp# = current# + (diff# * ((speed#^tfact#-(speed#-1.0)^tfact#)/(speed#^tfact#)))
   value# = wrapvalue(temp#)
   
endfunction value#

updated 13.10.06