Author Topic: My latest fascination...  (Read 2813 times)

0 Members and 1 Guest are viewing this topic.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
My latest fascination...
« on: January 31, 2021, 05:07:05 pm »
is with this idea of "branchless" coding.

While writing a library routine to "fit" an image into a predefined area, and trying to see if I can break the development build, I tried to do it in a branchless way. Imagine my astonishment when when 16+ lines of SELECT CASE blocks and IF...THENs distilled into three expressions...

Oh yeah, and I'm liking the new DIM syntax, so long as I don't have to go back to 1.4.

Code: QB64: [Select]
  1. 'Image sizing in predefined space using branchless equations
  2. SCREEN _NEWIMAGE(800, 700, 32)
  3.  
  4. 'create test patterns, one wide & one tall
  5. W& = _NEWIMAGE(750, 480, 32)
  6. COLOR , &HFFFF0000
  7. FOR x = 100 TO 90 STEP -1
  8.     CIRCLE (150, 240), x, &HFF0000FF
  9.     CIRCLE (375, 240), x, &HFF0000FF
  10.     CIRCLE (600, 240), x, &HFF0000FF
  11.  
  12. T& = _NEWIMAGE(480, 750, 32)
  13. COLOR , &HFF008000
  14. FOR x = 1 TO 10
  15.     LINE (100 + x, 100 + x)-(380 - x, 380 - x), &HFFFF0000, B
  16.     LINE (100 + x, 470 + x)-(380 - x, 650 - x), &HFFFF0000, B
  17.  
  18. 'Or load images of your choosing in lieu of test patterns
  19.  
  20.  
  21. x% = 400: y% = 20: x1% = 650: y1% = 300 '                       use predetermined part of the screen (400, 20)-(650, 300)
  22. LINE (x%, y%)-(x1%, y1%), &H7F7F7F7F, B , &H0101 '              show target area
  23. Image_Resize x%, y%, x1%, y1%, W&, 0, "c", "u" '                justify up, wide image
  24.  
  25. LINE (x%, y%)-(x1%, y1%), &H7F7F7F7F, B , &H0101 '              show target area
  26. Image_Resize x%, y%, x1%, y1%, T&, 0, "r", "c" '                justify right, tall image
  27.  
  28. x% = 0: y% = 0: x1% = _WIDTH - 1: y1% = _HEIGHT - 1 '           use full screen
  29. Image_Resize x%, y%, x1%, y1%, W&, 0, "c", "u" '                justify up, wide image
  30.  
  31. Image_Resize x%, y%, x1%, y1%, T&, 0, "l", "c" '                justify left, tall image
  32.  
  33. Image_Resize x%, y%, x1%, y1%, W&, 0, "c", "d" '                justify down, wide image
  34.  
  35. x% = 25: y% = 450: x1% = 250: y1% = 580 '                       use predetermined part of the screen (25, 450)-(250, 580)
  36. LINE (x%, y%)-(x1%, y1%), &H7F7F7F7F, B , &H0101 '              show target area
  37. Image_Resize x%, y%, x1%, y1%, W&, 0, "c", "c" '                justify center, wide image
  38.  
  39. LINE (x%, y%)-(x1%, y1%), &H7F7F7F7F, B , &H0101 '              show target area
  40. Image_Resize x%, y%, x1%, y1%, T&, 0, "c", "c" '                justify center, tall image
  41.  
  42. 'and you get the idea...
  43.  
  44.  
  45. SUB Image_Resize (xpos AS INTEGER, ypos AS INTEGER, xlim AS INTEGER, ylim AS INTEGER, i AS LONG, d AS LONG, xj AS STRING, yj AS STRING)
  46.  
  47.     'Syntax: upper left x, upper left y, lower right x, lower right y, image handle, destination handle, horizontal justification, vertical justification
  48.     'horizontal justifications= "l" left, "c" center, "r" right
  49.     'vertical justifications= "u" up, "c" center, "d" down
  50.     DIM AS INTEGER xs, ys, xp, yp, xl, yl
  51.     xp = xpos: yp = ypos: xl = xlim: yl = ylim
  52.     DIM AS SINGLE rt, xrt, yrt
  53.     xrt = (xl - xp) / _WIDTH(i) '                               width of area divided by width of image
  54.     yrt = (yl - yp) / _HEIGHT(i) '                              height of area divided by height of image
  55.     rt = ABS((xrt * (xrt < yrt)) + (yrt * (yrt <= xrt))) '      pick the smaller of the two ratios to fit area
  56.     xs = _WIDTH(i) * rt '                                       final image size ratio in x
  57.     ys = _HEIGHT(i) * rt '                                      final image size ratio in y
  58.  
  59.     xp = xp * ABS(xj = "l") + (_SHR(xl - xp, 1) + xp - _SHR(xs, 1)) * ABS(xj = "c") + (xl - xs) * ABS(xj = "r")
  60.     xl = xp + xs
  61.     yp = yp * ABS(yj = "u") + (_SHR(yl - yp, 1) + yp - _SHR(ys, 1)) * ABS(yj = "c") + (yl - ys) * ABS(yj = "d")
  62.     yl = yp + ys
  63.     _PUTIMAGE (xp, yp)-(xl, yl), i, d
  64.  
  65. END SUB 'Image_Resize
  66.  

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: My latest fascination...
« Reply #1 on: January 31, 2021, 05:27:23 pm »
Impossible. How could you use your noodle, and not come up with spaghetti? :D

Nice concept, Andy. I've been thinking about ways to keep the cats in the herd, too. For me, it will probably never happen, because use run and gun coders can't stand a plan.

Thanks for posting this,

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: My latest fascination...
« Reply #2 on: January 31, 2021, 05:40:18 pm »
Impossible. How could you use your noodle, and not come up with spaghetti? :D

Nice concept, Andy. I've been thinking about ways to keep the cats in the herd, too. For me, it will probably never happen, because use run and gun coders can't stand a plan.

Thanks for posting this,

Pete

I saved a lot of typing in the code segment so that I'd have enough strength left to put that extra "when" in m post.... ;)

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: My latest fascination...
« Reply #3 on: January 31, 2021, 06:18:09 pm »
Well well, good for you!

Pete :D
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/