BlitzCoder Essentials
•
Home Page
•
About BlitzCoder
•
Contributors
•
Terms of Use
•
Email Us
Main Areas
•
BlitzCoder Chat
•
Discussions
•
Articles/Tutorials
•
Code Database
•
Link Database
•
Showcase Area
•
Worklogs
•
Competitions
Special Areas
•
Undocumented
Other Blitz Sites
•
Blitz Basic Home
•
Blitz Showcase
•
BlitzPlay Library
Forum Login
Username:
Password:
•
Register Now!
BlitzCoder Code Archives Page
Main Codes Page
"Clipping using maths"
, by thechange
Demonstrating clipping values using maths only.
Code
Graphics 400 , 800 , 0 , 2 ;------------------------------------------------------------------------------; ; Using maths to cut a value is slower than using a simple couple of IFs. ; But it's a good training for your brain -and- to understand how maths work in the heart of the computer. ; Another thing is looping a value with specified offset, which can't be easily done by some IFs. ; Most commonly used is MOD - but you can also do this yourself, again for practice :) ; Suppose we have some money, and we want to round it down to the nearest dollar. ; Or any number of bytes and we want to know how many packages of 100 bytes will fit. ; As an example, 12 dollars and 59 cents or 1259 bytes. Amount = 1259 ; We want to round it down to each 100 units. Clip = 100 ; If we first divide the amount by the clipping value, we'd get 12.59. ; But we're not storing floating point values, so this would automatically be rounded down to 12. ; Then multiplying again by the same clipping value to get 1200. Result = Amount / Clip * Clip Print Result Print WaitKey ;------------------------------------------------------------------------------; ; You can also force the rounding to be odd instead of even. ; Here's an example of what you'd get. Const Range = 4 For Number = -10 To 10 ; Integers only Result1 = Number / Range * Range ; Floating division Result2 = Int ( Float ( Number ) / Range ) * Range ; Floating with precap Result3 = Int ( Float ( Number - Range ) / Range ) * Range + Range Print "n = " + Number + " int: " + Result1 + " fp: " + Result2 + " fpc: " + Result3 Next Print WaitKey ; In the end it comes down to what you need it for. ; And the type of precision and rounding that needs to be applied. ;------------------------------------------------------------------------------; ; Maths can also be used if you want to cut a value off beyond a specified range. ; So imagine numbers in the a range of 1 to 5 to remain intact, and zero all others. ; Starting number Local cStart% = -10 ; Minimum range value Local cMin% = 1 ; Maximum range value Local cMax% = 5 ; Ending number Local cEnd% = 10 ; Iterator variable Local cLoop% = cStart Repeat ; Iterate cLoop = cLoop + 1 ; STEP 1: Zero all values below the minimum range value cCalcMin = ( cLoop => cMin ) * cLoop ; STEP 2: Zero all values above the maximum range value cCalcMax = ( cLoop <= cMax ) * cLoop ; STEP 3: Combine the previous two cCalcCombi = ( ( ( cLoop => cMin ) * cLoop ) <= cMax ) * ( ( cLoop => cMin ) * cLoop ) ; Align results n$ = LSet( cLoop , 3 ) rMin$ = LSet( cCalcMin , 3 ) rMax$ = LSet( cCalcMax , 3 ) nT$ = LSet( cCalcCombi , 3 ) Print ( "n = " + n + " rMin: " + rMin + " rMax: " + rMax + " nT: " + nT ) Until cLoop = cEnd Print WaitKey End ; The above combined math statement can be replaced by 2 simple IF statements though :o) ;------------------------------------------------------------------------------; ; Any questions, just ask: TheChange@yahoo.com ; ; Have fun ;)
Copyright(c) 2000-2004, BlitzCoder. All Rights Reserved.
Code software created by Krylar's Kreations