![]() |
|
Today I learned loops are slow - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Programs (https://qb64phoenix.com/forum/forumdisplay.php?fid=7) +---- Thread: Today I learned loops are slow (/showthread.php?tid=4159) Pages:
1
2
|
Today I learned loops are slow - CMR - 11-27-2025 I needed a quick way to calculated the position of a sprite on a sprite sheet if I knew what number sprite it was. Say you had a sheet that was 40x40 and you wanted the 22nd sprite on it. Something like that. I came up with two ways to get it, one was with a double for loop and the other was just using some multiplication. It doesn't do anything with the xpos, ypos variables other than print the last two calculated. To get the location of the sprite on the sheet you could just multiply these two numbers by the width and height of the sprite cells respectively. This was just a speed test. My second subroutine that relied on pure calculation won out. Code: (Select All)
RE: Today I learned loops are slow - bplus - 11-27-2025 Why would anyone use a loop to find a sprite on a sprite sheet? Its a direct calculation. And what does creating a bunch of random points have to do with locating a sprite? Direct Calculation: let SW = sprite width, SH sprite height, SPR sprites per row, SPC sprites per column let I be the sprite index of interest let SX, SY be the top left pixel of Sprite I then SX = (I mod SPC) * SW SY = int(I / SPR) * SH Simple (for someone who has done it dozens of times.)
RE: Today I learned loops are slow - CMR - 11-27-2025 I just generated a bunch of random numbers for the test. I thought the loops might be faster because it's just counting up to a number, and then you use the x and y values for the location. I'm also not that good at math.
RE: Today I learned loops are slow - bplus - 11-27-2025 S'OK live and learn, give my little forumlas a test, might be helpful to you ![]() The sprite index assumes numbering the sprites left to right, top to bottom starting at 0 for first sprite. I would prefer starting at 1 so the top index is the number of sprites but that complicates the math a bit. So top index is SPR * SPC -1, minus 1 because we start at 0. Mod is modulus = the remainder left after dividing index by SPC (sprites per... Dang I think I switched SPR with SPC, oops sorry SX = (I mod SPR) * SW SY = int(I / SPC) * SH Sprites per row, SPR, is how many sprites across the sheet Sprites per column, SPC, is how many sprites down the sheet I will dig up an old proggie to verify these... RE: Today I learned loops are slow - bplus - 11-27-2025 Yikes both forumlas were flawed, glad I checked my formulas. Turns out we only need SPR's Sprites per Row: sheetx = (index Mod SPRow) * SW sheety = Int(index / SPRow) * SH OK here is snap of the sprite sheet: We will display the top row of cards by index numbers from 0 to 13 Code: (Select All) _Title "Card Sprites by Index demo" 'bplus 2025-11-27Change index = 14 to 27 and see the next row... 28 to 41 next and 42 to 55 for last. zip with card sheet and code: RE: Today I learned loops are slow - CMR - 11-27-2025 Nice. Thanks for the tips. RE: Today I learned loops are slow - hsiangch_ong - 11-27-2025 nice demonstration. actually i downloaded the picture on the above forum post. didn't get the zip file instead. i was going to complain, "it doesn't work properly. it doesn't align well." if you live in the u.s.a. happy thanksgiving folks. RE: Today I learned loops are slow - bplus - 11-27-2025 Thanks CMR and hsiangch_ong. BTW do index from 0 to 55 and see all the cards in order, why didn't I think of that originally? ![]() I might follow this up with code for doing cards ie assigning values, suits, points, shuffling, dealing... for card games. I have code ready for gin rummy game worked out years ago awaiting card sprites. I also have code assessing poker hands awaiting card sprites as well. Yes happy Thanksgiving to all aboard! RE: Today I learned loops are slow - Pete - 11-27-2025 +1 to CMR for thinking outside the box. I mean if you're like me and think 1 + 1 = bigger 1, then you need to get creative! Pete
RE: Today I learned loops are slow - CMR - 11-28-2025 (11-27-2025, 10:52 PM)Pete Wrote: +1 to CMR for thinking outside the box. I mean if you're like me and think 1 + 1 = bigger 1, then you need to get creative! My solutions to simple problems involving math tend to be imaginatively over complicated.
|