? Compact coordinate conversion ● SmileBASIC Source

Sign In

Register
*Usernames are case-sensitive
Forgot my password

Compact coordinate conversion

Integer Implementation

DEF COMPACT(X0%, Y0%, X1%, Y1%, MXW%, MXH%) DIM C0% = Y0%*MXW% + X0% DIM C1% = Y1%*MXW% + X1% DIM C2% = C1%*MXW%*MXH% + C0% RETURN C2% END DEF EXTRACT C2%, MXW%, MXH% OUT X0%, Y0%, X1%, Y1% DIM C0% = C2% MOD MXW%*MXH% DIM C1% = C2% DIV MXW%*MXH% X0% = C0% MOD MXW% Y0% = C0% DIV MXW% X1% = C1% MOD MXW% Y1% = C1% DIV MXW% END

Floating-Point Implementation

DEF COMPACT(X0#, Y0#, X1#, Y1#, MXW#, MXH#) DIM C0# = Y0#*MXW# + X0# DIM C1# = Y1#*MXW# + X1# DIM C2# = C1#*MXW#*MXH# + C0# RETURN C2# END DEF EXTRACT C2#, MXW#, MXH# OUT X0#, Y0#, X1#, Y1# DIM C0# = C2#-(MXW#*MXH#)*FLOOR(C2#/(MXW#*MXH#)) DIM C1# = FLOOR(C2#/(MXW#*MXH#)) X0# = C0#-MXW#*FLOOR(C0#/MXW#) Y0# = FLOOR(C0#/MXW#) X1# = C1#-MXW#*FLOOR(C1#/MXW#) Y1# = FLOOR(C1#/MXW#) END What these functions essentially do is performing mathematical steps to re-represent the given data. COMPACT receives two sets of x-y coordinates (x0, y0, x1, y1) and converts them into a single value which may be stored, and perhaps later passed through EXTRACT, where it retrieves the original two sets of x-y coordinates given that it has the original MXW (maximum width [highest allowed x-value]) and MXH (maximum height [highest allowed y-value]). For the integer method of this, the highest values for MXW and MXH are {320-1, 145} and {320, 145-1}. For the floating-point method, these values are untested.
Author
MZ952
Updated
Rating
0 votes
Categories
Keywords
16 Comment(s) Y_ack Y_ack what advantages does this have over RG BREAD? dfrost2 dfrost2 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength I thought this was for defining points and putting them into variables TheV360 TheV360 Third Year My account is over 3 years old Website First Day Joined on the very first day of SmileBASIC Source Website Night Person I like the quiet night and sleep late. Express Yourself RGBREAD does the same thing, unpacking four 8-bit RGBA values from a 32-bit integer. It's just that (from my experience) it's consistently slower than just writing your own function for the same thing. MZ952 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Drawing I like to draw! Hobbies Reading I like to read books! Hobbies RGBREAD may be more effective for one's purposes if you want to go up to 248 on an x, y, and z parameter, however, this function works with 2 sets of X-Y coordinates, not a trio of values. I believe RGBREAD glosses over a lot of values due to lossy color conversion. Edit: forgot the alpha channel was a thing. 12Me21 12Me21 Head Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Second Year My account is over 2 years old Website RGBREAD/RGB just convert between four 8 bit values and one 32 bit value. You can increase the range of your system by using floats rather than integers (a 64 bit float can store all integers from 0 to 2^53 (and more, but with reduced precision), rather than 0 to 2^31-1 for a signed 32 bit integer) IAmRalsei IAmRalsei Video Games I like to play video games! Hobbies First Year My account is over 1 year old Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Are we just going to ignore the random space? MZ952 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Drawing I like to draw! Hobbies Reading I like to read books! Hobbies @12Me21 You know, this is probably the go-to method anyhow, considering that if you go higher than the parameters specified in the post, you'll get an Overflow error on line 4, where it is attempting to stow the resulting expression into C2%. I always thought that SB would just chop the value off at the highest allowed bit, but I guess it elects to error instead. Edit: considering that it's only working with 32-bit integers in the first place, I wonder how it managed an Overflow, unless it processes expressions in floating point, which I guess means that the Overflow happened somewhere in the process of converting float -> int. 12Me21 12Me21 Head Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Second Year My account is over 2 years old Website If you add 2 integers and the result is too big, it's converted to a float. IAmRalsei IAmRalsei Video Games I like to play video games! Hobbies First Year My account is over 1 year old Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength I'll take that as a yes. MZ952 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Drawing I like to draw! Hobbies Reading I like to read books! Hobbies What random space? IAmRalsei IAmRalsei Video Games I like to play video games! Hobbies First Year My account is over 1 year old Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength It says RG BREAD instead of RGBREAD dfrost2 dfrost2 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength red green bread dfrost2 dfrost2 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength you could store x in 10 bits and y in 10 bits. That lets it go up to 1023 on both x and y. dfrost2 dfrost2 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength heck, you could include z, too because ints are 32 - bit so you can distribute these evenly with two bits left over. You could use these as masks or something like that. Maybe a bit to signify that it is a point... MZ952 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Drawing I like to draw! Hobbies Reading I like to read books! Hobbies You're storing not only x and y, but x0, y0, x1, y1 inside of c, versus just an x, y, and z inside of c. dfrost2 dfrost2 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength well Y goes down to 239(on the screen) and 8 bits are 255 and X goes to 400, so you would need 9 bits for 512. You could use the fifth bit on X for the Y value. It would look like this: XXXXXXXXYYYYYYYYYXXXXXXXXYYYYYYYYY but that would be 34 bits, so you could do this: XXXXYXXXYYYYYYYYXXXXYXXXYYYYYYYY which would be 32 bit. Reread it to understand X needs 8 bits, but with 16 left over. You could use that bit for the y and nullify it for x Or you could use reals