Author Topic: Here's a puzzler  (Read 3434 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Here's a puzzler
« on: December 27, 2020, 09:46:35 am »
So here's a really simple concept, that I'm ending up having a heckuva time with:  Instead of drawing a box of X thickness around an area, draw 4 triangular tabs at the edges instead.

So instead of:


**************************************
**************************************
**                                  **
**                                  **
**                                  **
**                                  **
**                                  **
**                                  **
**                                  **
**                                  **
**                                  **
**                                  **
**                                  **
**                                  **
**                                  **
**************************************
**************************************


We'd see something like:



******                         *******
******                         *******
**                                  **
**                                  **






***                                ***
****                              ****
** **                            ** **
**  **                          **  **
*******                        *******
*******                        *******



Now, ASCII art is too big of a PITA for me to sit and draw all 4 "tabs" in the above illustration, but I think the bottom half showcases the general idea.

Draw a triangle at each corner of a rectangle, and fix it so you can adjust to whatever variable width thickness you want for that triangle.

For a box, the concept is really simple:

FOR i = 0 to line_thickness
    LINE(x+i, y+ i)-STEP(wide - 2*i, high - 2*i), desired_color, B
NEXT

But for a set of 4 triangles??   My poor mind has been going crazy trying to sort out the math! 

So far, here's what I have (which does 3 of my 4 sides already:)

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(640, 480, 32)
  2. x = 1
  3. y = 1
  4.  
  5. FOR i = 0 TO 5
  6.     'top left
  7.     x1 = x * 1280 / 7 + i: y1 = y * 90 + TopOffset + i
  8.     x2 = x1 + 20 - i: y2 = y1
  9.     x3 = x1: y3 = y1 + 20 - i
  10.     Triangle x1, y1, x2, y2, x3, y3
  11.     'top right
  12.     x4 = (x + 1) * 1280 / 7 + 3 - i: y4 = y1
  13.     x5 = x4 - 20 + i: y5 = y4
  14.     x6 = x4: y6 = y4 + 20 - i
  15.     Triangle x4, y4, x5, y5, x6, y6
  16.     'bottom left
  17.     x7 = x * 1280 / 7 + i: y7 = (y + 1) * 90 + TopOffset + 3 - i
  18.     x8 = x7: y8 = y7 - 20 + i
  19.     x9 = x7 + 20 - i: y9 = y7
  20.     Triangle x7, y7, x8, y8, x9, y9
  21.  
  22. SUB Triangle (x1, y1, x2, y2, x3, y3)
  23.     LINE (x1, y1)-(x2, y2), Red
  24.     LINE -(x3, y3), Red
  25.     LINE -(x1, y1), Red
  26.  

Only four more cups of coffee and another three handfuls of hair, and I'll be finished!  :P

Anyone got a very simple little routine to do something like this that I can borrow (*cough*  *cough* *steal shamelessly*) in case I ever want to do something like this again in the future? 

The code above showcases the concept a lot better than my poor skills at ASCII art ever could. ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Here's a puzzler
« Reply #1 on: December 27, 2020, 09:58:00 am »
And here's the whole thing working now:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(640, 480, 32)
  2. x = 1
  3. y = 1
  4.  
  5. FOR i = 0 TO 5
  6.     'top left
  7.     x1 = x * 1280 / 7 + i: y1 = y * 90 + TopOffset + i
  8.     x2 = x1 + 20 - i: y2 = y1
  9.     x3 = x1: y3 = y1 + 20 - i
  10.     Triangle x1, y1, x2, y2, x3, y3
  11.     'top right
  12.     x4 = (x + 1) * 1280 / 7 + 3 - i: y4 = y1
  13.     x5 = x4 - 20 + i: y5 = y4
  14.     x6 = x4: y6 = y4 + 20 - i
  15.     Triangle x4, y4, x5, y5, x6, y6
  16.     'bottom left
  17.     x7 = x * 1280 / 7 + i: y7 = (y + 1) * 90 + TopOffset + 3 - i
  18.     x8 = x7: y8 = y7 - 20 + i
  19.     x9 = x7 + 20 - i: y9 = y7 '- 20 - i
  20.     Triangle x7, y7, x8, y8, x9, y9
  21.     'bottom right
  22.     x10 = x4: y10 = y7
  23.     x11 = x10 - 20 + i: y11 = y10
  24.     x12 = x10: y12 = y11 - 20 + i
  25.     Triangle x10, y10, x11, y11, x12, y12
  26.  
  27.  
  28. SUB Triangle (x1, y1, x2, y2, x3, y3)
  29.     LINE (x1, y1)-(x2, y2), Red
  30.     LINE -(x3, y3), Red
  31.     LINE -(x1, y1), Red

Change the x/y values, and you can see how our little tabs move around the screen for us.  Now, all I need is 57.5 bottles of rootbeer to dunk my head in to get my poor brain to stop smoldering.  :P

(You can also change the FOR i limit to change the thickness of the lines on the tabs, if you want to.)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Here's a puzzler
« Reply #2 on: December 27, 2020, 12:03:06 pm »
Here is 45 degree right triangles
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. _DELAY .25
  3. tri4 100, 50, 200, 100, 25, &HFFFF0000
  4.  
  5. SUB tri4 (x, y, w, h, tSide, c AS _UNSIGNED LONG)
  6.     ftri x, y, x + tSide, y, x, y + tSide, c
  7.     ftri x + w, y, x + w - tSide, y, x + w, y + tSide, c
  8.     ftri x + w, y + h, x + w - tSide, y + h, x + w, y + h - tSide, c
  9.     ftri x, y + h, x + tSide, y + h, x, y + h - tSide, c
  10.  
  11.  
  12. SUB ftri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  13.     DIM D AS LONG
  14.     STATIC a&
  15.     D = _DEST
  16.     IF a& = 0 THEN a& = _NEWIMAGE(1, 1, 32)
  17.     _DEST a&
  18.     _DONTBLEND a& '  '<<<< new 2019-12-16 fix
  19.     PSET (0, 0), K
  20.     _BLEND a& '<<<< new 2019-12-16 fix
  21.     _DEST D
  22.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  23.  
  24.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Here's a puzzler
« Reply #3 on: December 27, 2020, 02:53:12 pm »
Here it is fancied up with triangles within triangles:
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. _DELAY .25
  3. tri4 100, 50, 200, 250, 50, &HFFFF0000
  4. tri4 105, 55, 190, 240, 35, &HFFFFFF00
  5. tri4 110, 60, 180, 230, 20, &HFF0000FF
  6.  
  7. SUB tri4 (x, y, w, h, tSide, c AS _UNSIGNED LONG)
  8.     ftri x, y, x + tSide, y, x, y + tSide, c
  9.     ftri x + w, y, x + w - tSide, y, x + w, y + tSide, c
  10.     ftri x + w, y + h, x + w - tSide, y + h, x + w, y + h - tSide, c
  11.     ftri x, y + h, x + tSide, y + h, x, y + h - tSide, c
  12.  
  13.  
  14. SUB ftri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  15.     DIM D AS LONG
  16.     STATIC a&
  17.     D = _DEST
  18.     IF a& = 0 THEN a& = _NEWIMAGE(1, 1, 32)
  19.     _DEST a&
  20.     _DONTBLEND a& '  '<<<< new 2019-12-16 fix
  21.     PSET (0, 0), K
  22.     _BLEND a& '<<<< new 2019-12-16 fix
  23.     _DEST D
  24.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  25.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Here's a puzzler
« Reply #4 on: December 27, 2020, 03:11:00 pm »
Here 'tis 3D!
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. _DELAY .25
  3. FOR i = 0 TO 15
  4.     tri4 100 + i, 50 + i, 200 - 2 * i, 250 - 2 * i, 50 - 3 * i, _RGB32(0, i * 17, 0)
  5.  
  6. SUB tri4 (x, y, w, h, tSide, c AS _UNSIGNED LONG)
  7.     ftri x, y, x + tSide, y, x, y + tSide, c
  8.     ftri x + w, y, x + w - tSide, y, x + w, y + tSide, c
  9.     ftri x + w, y + h, x + w - tSide, y + h, x + w, y + h - tSide, c
  10.     ftri x, y + h, x + tSide, y + h, x, y + h - tSide, c
  11.  
  12.  
  13. SUB ftri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  14.     DIM D AS LONG
  15.     STATIC a&
  16.     D = _DEST
  17.     IF a& = 0 THEN a& = _NEWIMAGE(1, 1, 32)
  18.     _DEST a&
  19.     _DONTBLEND a& '  '<<<< new 2019-12-16 fix
  20.     PSET (0, 0), K
  21.     _BLEND a& '<<<< new 2019-12-16 fix
  22.     _DEST D
  23.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  24.  
  25.  
  26.  

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Here's a puzzler
« Reply #5 on: December 27, 2020, 11:29:14 pm »
Some of my attempts looked like spiderwebs, but here are 3 ordinary flavors:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(640, 480, 32)
  2.  
  3. s = 100 '                                                  size of corners
  4.     FOR r = 1 TO 3 '                                       type of corner
  5.         CLS
  6.         FOR q = 0 TO s STEP 10
  7.             RESTORE
  8.             q2 = s - q
  9.             FOR i = 1 TO 4 '                               4 corners
  10.                 READ xo, yo, xd1, yd1, xd2, yd2, xi, yi
  11.                 x0 = xo * (_WIDTH - 1) + xi * q / r
  12.                 y0 = yo * (_HEIGHT - 1) + yi * q / r
  13.                 LINE (x0, y0)-STEP(xd1 * q2, yd1 * q2)
  14.                 LINE -(x0 + xd2 * q2, y0 + yd2 * q2)
  15.                 LINE -(x0, y0)
  16.                 _DELAY .01 '                               let human see what's happening
  17.             NEXT i
  18.         NEXT q
  19.         WaitUntil! = TIMER + .5
  20.         DO: _LIMIT 10
  21.             IF LEN(INKEY$) THEN END
  22.         LOOP UNTIL TIMER > WaitUntil!
  23.     NEXT r
  24.  
  25. DATA 0,0,0,1,1,0,1,1
  26. DATA 1,0,-1,0,0,1,-1,1
  27. DATA 1,1,-1,0,0,-1,-1,-1
  28. DATA 0,1,0,-1,1,0,1,-1
  29.  
It works better if you plug it in.