November 25, 2020, 08:01:58 AM

Author Topic: [bb] Rounding Numbers by rolow [ 1+ years ago ]  (Read 580 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Rounding Numbers by rolow [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Rounding Numbers
Author : rolow
Posted : 1+ years ago

Description : I have modified the code example provided with Blitz Basic 3D to show a true X,Y coordinate system demonstrating the use of the CEIL, FLOOR and INT commands.

Code :
Code: BlitzBasic
  1. ; This example modified by RoLoW in September 2009
  2. ; is based upon the original Ceil / Floor / Int
  3. ; example from Blitz3D for three kinds of rounding.
  4. ;
  5. ; Resolutions and Divisor:
  6. ;      640 x 480 use with 32
  7. ;      800 x 600 use with 40
  8. ;     1024 x 768 use with 51
  9. ;
  10. Global ScreenWide = 800
  11. Global ScreenHigh = 600
  12. Global Divisor = 40
  13. ;
  14. Global Origin_X = ScreenWide/2
  15. Global Origin_Y = ScreenHigh/2
  16. ;
  17. Graphics ScreenWide, ScreenHigh
  18. ;
  19. SetBuffer BackBuffer()
  20. Origin Origin_X,Origin_Y
  21. ;
  22. MoveMouse Origin_X,Origin_Y : HidePointer
  23. ;
  24. While Not GetKey();press any key
  25.     ;
  26.     Cls
  27.     ;
  28.     Color 255,255,255
  29.     Text -Origin_X,-Origin_Y,   "         Blitz3D ROUNDING Example"
  30.     Text -Origin_X,-Origin_Y+20,"Demonstrates Functions CEIL, FLOOR and INT"
  31.     ;
  32.     Text -Origin_X,-Origin_Y+60, " CEIL rounds up to the next whole number"
  33.     Text -Origin_X,-Origin_Y+80, "FLOOR rounds down to the next whole number"
  34.     Text -Origin_X,-Origin_Y+100,"  INT rounds to the closest whole number"
  35.     ;
  36.     Text -Origin_X,-Origin_Y+150,"Move mouse around to check different values"
  37.     Text -Origin_X,-Origin_Y+170,"of X and Y on the coordinate system."
  38.     Text -Origin_X,-Origin_Y+210,"Press any key to exit."
  39.     ;
  40.     DrawNumberLineX
  41.     DrawNumberLineY
  42.     ;
  43.     my = MouseY() - Origin_Y
  44.     mx = MouseX() - Origin_X
  45.     ;
  46.     Color 255,100,100
  47.     Line -Origin_X, my, Origin_X-1, my
  48.     Line mx,-Origin_Y,mx,Origin_Y
  49.     ;
  50.     x# = Float( mx ) / Divisor
  51.     Color 100,100,0
  52.     Text -225, Divisor*2, " X = " + x
  53.     Text -225, Divisor*2+20, " Ceil( x ) = " + Ceil( x )
  54.     Text -225, Divisor*2+40, " Floor( x ) = " + Floor( x )
  55.     Text -225, Divisor*2+60, " Int( x ) = " + Int( x )
  56.     ;
  57.     y# = Float( -my ) / Divisor
  58.     Color 100,100,0
  59.     Text Divisor*2, Divisor*2,    " Y = " + y
  60.     Text Divisor*2, Divisor*2+20, " Ceil( y ) = " + Ceil( y )
  61.     Text Divisor*2, Divisor*2+40, " Floor( y ) = " + Floor( y )
  62.     Text Divisor*2, Divisor*2+60, " Int( y ) = " + Int( y )
  63.     ;
  64.     Flip
  65.     ;
  66. Wend
  67. ;
  68. End
  69. ;
  70. Function DrawNumberLineX( ) ; horizontal line with numeric labels
  71.     ;
  72.     Color 200,200,200
  73.     Line  -Origin_X, -1, Origin_X-1 , -1
  74.     Line  -Origin_X,  0, Origin_X-1 ,  0
  75.     Line  -Origin_X,  1, Origin_X-1 ,  1
  76.     ;
  77.     For n = -9 To 9
  78.         xn = -Divisor * n
  79.         Line xn, -4, xn, 4
  80.         Color 255,255,255
  81.         If n <> 0 Then Text xn-10, -20, RSet( -n, 2 )
  82.         If Abs(n) = 9 Then Text xn, 20, "X"
  83.     Next
  84.     ;
  85. End Function
  86. ;
  87. Function DrawNumberLineY( ) ; vertical line with numeric labels
  88.     ;
  89.     Color 200,200,200
  90.     Line -1, -Origin_Y, -1, Origin_Y-1
  91.     Line  0, -Origin_Y,  0, Origin_Y-1
  92.     Line  1, -Origin_Y,  1, Origin_Y-1
  93.     ;
  94.     For n = -7 To 7
  95.         yn = -Divisor * n
  96.         Line -4, yn, 4, yn
  97.         Color 255,255,255
  98.         If n <> 0 Then Text -30, yn - 6, RSet( n, 2 )
  99.         If Abs(n) = 7 Then Text 20, yn-6, "Y"
  100.     Next
  101.     ;
  102. End Function


Comments :


_PJ_(Posted 1+ years ago)

 A nicely done example.There's a small bug in there somewhere though (I'll try to narrow it down) but if you move the crosshair beyond the scale of the graph axes, it messes up the recorded values. This could cause problems in rare cases. . . Just as a warning :)


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal