Mod
Result = Mod(Number, Divisor)
 
Parameters:

    Number=numeric value whose remainder you want to find
    Divisor=the number used to divide the number parameter
Returns:

    Result
 

     The Mod function (modulus) returns the remainder after a number is divided by a divisor.

     There's a number handy usages for the MOD function in game programming, a couple that come to mind would be in wrapping movement coordinates, wrapping sprite animations, or even things like finding the locate of a point over a grid.


EXAMPLES





FACTS:


      * Mod is implemented as function in PlayBASIC, were it's an operator in some languages.

      * The Mod(Number,Divisor) function is the equivalent of this code, Result = Number-((Number/Divisor)*Divisor)



Example:


     Showing the use of the Mod function

  
; Divident and divisor
  Number = 42
  Divisor = 13
  
; Perform a division and a mod operation
  r = Number / Divisor
  m = Mod(Number, Divisor)
; Show the results
  Print Str$(Number) + " divided by " + Str$(Divisor) + " equals " + Str$(r)
  Print "The remainder is " + Str$(m)
  
; Display the Screen and wait for the user to press a key
  Sync
  WaitKey
  



This example would output.

  
  42 divided by 13 equals 3
  The remainder is 3
  



Top





Wrap Coordinates:


     This example uses the MOD function to keep the XPOS variable inside the 0 to 799 bounds. Making our object (a circle in this example) appear to move around the screen left to right forever

  
  
;limit fps to a peek of 100
  SetFPS 100
  
  
; start of do/loop
  Do
     
   ; clear the screen
     Cls
     
   ; Add 1 to Xpos, and wrap it within 0 to 799
     xpos=Mod(Xpos+1,800)
     
   ; draw circle at this position
     Circle Xpos,100,50,1
     
     
     Sync
  Loop
  
  


Top







 
Related Info: Operators | Sprites | WrapValue :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com