QB64.org Forum

Active Forums => Programs => Topic started by: MasterGy on March 21, 2022, 08:12:03 pm

Title: Garland of pearls
Post by: MasterGy on March 21, 2022, 08:12:03 pm
in principle, if I create a cobweb of points and connections, it will be a real cobweb in 3d that takes over all the vibrations of the spider


Code: QB64: [Select]
  1.  
  2. CONST pip180 = 3.141592 / 180
  3. monx = 800: mony = 800: mon = _NEWIMAGE(monx, mony, 32): SCREEN mon
  4.  
  5. gravity = 1
  6. obj_c = 15 'number objects
  7. DIM obj(obj_c - 1, 9) '0,1 posX,Y 3fix/mover
  8.  
  9.  
  10.  
  11. cent_x = monx / 2: cent_y = 20: zoom = 2
  12.  
  13. 'install "garland of pearls"
  14.  
  15.  
  16. 'garland
  17. min_weight = 6
  18. FOR a = 0 TO obj_c - 1
  19.     obj(a, 0) = (RND(1) - .5) * 100
  20.     obj(a, 1) = (RND(1) - .5) * 100
  21.     obj(a, 2) = min_weight + min_weight / 2 * RND(1)
  22.     obj(a, 3) = 1 '<---- 0 if fix, 1 if fly
  23.  
  24. obj(0, 0) = -64.5: obj(0, 1) = 5: obj(0, 3) = 0
  25. obj(obj_c - 1, 0) = 183.5: obj(obj_c - 1, 1) = -.5: obj(obj_c - 1, 3) = 0
  26.  
  27. 'connections
  28. conn_c = 100: DIM conn(conn_c - 1, 4)
  29. FOR a = 0 TO obj_c - 2
  30.     conn(a, 0) = a
  31.     conn(a, 1) = a + 1
  32.     conn(a, 2) = 20 'rope width
  33.     conn(a, 3) = 1
  34.  
  35.  
  36.  
  37.  
  38.  
  39. DO: _LIMIT 100
  40.  
  41.     'show info
  42.     wch = -1: FOR a = 0 TO obj_c - 1
  43.         IF _MOUSEX < 200 AND INT(_MOUSEY / 16) = a THEN wch = a: COLOR _RGB32(255, 255, 255) ELSE COLOR _RGB32(100, 100, 100)
  44.         LOCATE a + 1, 1: PRINT a; "# weight:"; INT(obj(a, 2) * 10) / 10;: IF obj(a, 3) = 0 THEN PRINT "FIX";
  45.         IF wch = a THEN PRINT "<--- using mousewheel !"
  46.         IF wch <> -1 THEN obj(wch, 2) = obj(wch, 2) + mw / 10: IF obj(wch, 2) < min_weight THEN obj(wch, 2) = min_weight
  47.     NEXT a
  48.     LOCATE mony / 16 - 1, 1: IF moving THEN PRINT "press SPACE to FIX/FLY"; ELSE PRINT "grab the ball with the mouse and move it!";
  49.  
  50.     'draw objects
  51.     FOR a = 0 TO obj_c - 1
  52.         x = cent_x + obj(a, 0) * zoom
  53.         y = cent_y + obj(a, 1) * zoom
  54.         IF mouse_near = a THEN COLOR _RGB32(255, 0, 0) ELSE COLOR _RGB32(200, 200, 200)
  55.         CIRCLE (x, y), obj(a, 2) * zoom
  56.         _PRINTSTRING (x, y), LTRIM$(STR$(a))
  57.     NEXT a
  58.  
  59.     'draw connections
  60.  
  61.     COLOR _RGB32(150, 150, 150)
  62.     FOR a = 0 TO conn_c - 1: IF conn(a, 3) = 0 THEN _CONTINUE
  63.         x1 = obj(conn(a, 0), 0) * zoom + cent_x
  64.         y1 = obj(conn(a, 0), 1) * zoom + cent_y
  65.         x2 = obj(conn(a, 1), 0) * zoom + cent_x
  66.         y2 = obj(conn(a, 1), 1) * zoom + cent_y
  67.     LINE (x1, y1)-(x2, y2): NEXT a
  68.  
  69.     'calculate moving
  70.  
  71.  
  72.     FOR a = 0 TO obj_c - 1: IF obj(a, 3) = 0 OR (mouse_near = a AND _MOUSEBUTTON(1)) THEN _CONTINUE
  73.  
  74.         'connections vector
  75.         vec_x = 0: vec_y = 0: vec_c = 0
  76.         FOR t = 0 TO conn_c - 1: IF conn(t, 3) = 0 THEN _CONTINUE
  77.             x = -1: FOR t2 = 0 TO 1: IF conn(t, t2) = a THEN x = conn(t, t2 XOR 1)
  78.             NEXT t2
  79.             IF x <> -1 THEN
  80.                 disx = obj(a, 0) - obj(x, 0)
  81.                 disy = obj(a, 1) - obj(x, 1)
  82.                 ang = (-degree(disx, disy) + 0) * pip180
  83.                 dis = SQR(disx * disx + disy * disy)
  84.                 power = dis / obj(a, 2) / 5
  85.                 '  IF dis < (obj(a, 2) + obj(x, 2)) THEN power = -power / 10000
  86.                 vec_x = vec_x + SIN(ang) * power
  87.                 vec_y = vec_y - COS(ang) * power
  88.                 vec_c = vec_c + 1
  89.             END IF
  90.         NEXT t
  91.  
  92.         gravity = .1
  93.         lass = .98
  94.  
  95.         obj(a, 4) = (obj(a, 4) + vec_x / vec_c) * lass
  96.         obj(a, 5) = (obj(a, 5) + vec_y / vec_c) * lass + gravity
  97.  
  98.         lx = obj(a, 0) + obj(a, 4)
  99.         ly = obj(a, 1) + obj(a, 5)
  100.  
  101.         obj(a, 0) = obj(a, 0) + obj(a, 4)
  102.         obj(a, 1) = obj(a, 1) + obj(a, 5)
  103.  
  104.  
  105.  
  106.     NEXT a
  107.  
  108.     mw = 0: WHILE _MOUSEINPUT: mw = mw + _MOUSEWHEEL: WEND
  109.  
  110.     'mouse moving activing
  111.     IF moving = 0 THEN
  112.         mouse_near = -1
  113.         FOR a = 0 TO obj_c - 1
  114.             disx = (cent_x + obj(a, 0) * zoom) - _MOUSEX
  115.             disy = (cent_y + obj(a, 1) * zoom) - _MOUSEY
  116.             dis = SQR(disx * disx + disy * disy)
  117.             IF dis < 10 THEN mouse_near = a
  118.         NEXT a
  119.     END IF
  120.  
  121.     IF (_MOUSEBUTTON(1) AND mouse_near <> -1) OR moving THEN
  122.         moving = 1
  123.         obj(mouse_near, 0) = (_MOUSEX - cent_x) / zoom
  124.         obj(mouse_near, 1) = (_MOUSEY - cent_y) / zoom
  125.     END IF
  126.  
  127.     moving = moving AND _MOUSEBUTTON(1)
  128.  
  129.  
  130.     'change weights with mousewheel
  131.  
  132.  
  133.  
  134.     _DISPLAY: CLS
  135.  
  136.     'commands
  137.     inkey2$ = INKEY$: IF inkey2$ = CHR$(27) THEN END
  138.     IF inkey2$ = " " AND _MOUSEBUTTON(1) AND moving THEN obj(mouse_near, 3) = obj(mouse_near, 3) XOR 1
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. FUNCTION degree (a, b): degreex = ATN(a / (b + .00001)) / pip180: degreex = degreex - 180 * ABS(0 > b): degreex = degreex - 360 * (degreex < 0): degree = degreex: END FUNCTION
  146.  
Title: Re: Garland of pearls
Post by: bplus on March 21, 2022, 08:23:10 pm
Wow nice effect!
Title: Re: Garland of pearls
Post by: Richard Frost on March 22, 2022, 04:03:28 am
Very interesting!

Strange that you do something so difficult yet manage to plot the instructions off the screen.

I added this after the SCREEN is declared:
_ScreenMove (_DesktopWidth - _Width) \ 2, 20
Title: Re: Garland of pearls
Post by: STxAxTIC on March 22, 2022, 06:07:00 am
This program is incredibly fun to play with. Thanks very much for sharing MasterGy! This could most likely be used to study standing wave motion in flexible chains, both transverse and longitudinal waves. You always manage to pack so much action into your programs, it's impressive.

I'm pretty sure your program solves a thing called the "elastic catenary", which might just be a stretched-out version of a hyperbolic cosine. I'm not positive on that though, something tells me no closed solution exists, but I haven't tried the math by hand myself. Oh boy, here we go... (just kidding) No need to do the actual math, you've already got the simulation on hand!

I like that you mention spider webs. I think I can see what you're about to do next and it's very exciting!


Title: Re: Garland of pearls
Post by: Dav on March 22, 2022, 09:34:14 am
This is pretty amazing, @MasterGy!

- Dav
Title: Re: Garland of pearls
Post by: MasterGy on March 22, 2022, 10:11:44 am
Thanks for the comments. I honestly admit, I myself am amazed that this works. In games, I long ago figured it out to display text, animate, move when I want change to be flexible. Let A and B. Let "A" be the current value. Let B be the new value. By definition B = A. But this makes it inflexible in timing, animation, positioning work.
Therefore, I usually do it so that not B = A, but B = B + (A-B) x "all", for example. "All" must be between 0 and 1! This will be a constant value. If 0, no offset occurs. If 1, an inelastic, immediate displacement occurs. On the other hand, if I write e.g.
 The more times we perform it, the closer it gets to B = A. The larger the difference between A and B, the faster it makes the change, and when it is almost reached, it slows down. In fact, it never reaches.
Implementing STxAxTIC with the pendulum program returns the cleanest physics. Then came my idea of ​​what it would be like to forget that much complicated physics and use the method described above to move the ball. Ball "B" wants to be in place of "A" ... B = B + (A-B) x "all"
In principle, since this seems to work, this method could also be used to move, collide without the use of rigid-body physics or any more serious mathematics. In theory. Pendulum, brick wall collapses by bricks, car overturns, precise collision of two bodies with rotations and directions. It seems as if this simple method bypasses the part of physics where pulse retention, angular velocity, vectors should be taken into account. The system simply extracts from itself.
I’m very curious as to how far this can go, and is the latter suggestion true? does it work
I described these so that if anyone cares and wants to deal with it, sees opportunities in it like I do, then see how simple it is. try it !
I set up the program to eliminate flexibility. From here, the things described above should work in principle.
The program starts and you can turn flexibility on and off with the "S" key. If we turn it off, we get approximately the pendulum motion of STxAxTIC. When turned on, it will be flexible as if they were connected with rubber.

Code: QB64: [Select]
  1. CONST pip180 = 3.141592 / 180
  2. monx = 800: mony = 800: mon = _NEWIMAGE(monx, mony, 32): SCREEN mon
  3.  
  4. min_weight = 6
  5. gravity = 1
  6. obj_c = 10 'number objects
  7. DIM obj(obj_c - 1, 9) '0,1 posX,Y 3fix/mover
  8. cent_x = monx / 2: cent_y = mony * .2: zoom = 2
  9.  
  10. 'garland
  11. FOR a = 0 TO obj_c - 1
  12.     obj(a, 0) = (RND(1) - .5) * 200
  13.     obj(a, 1) = (RND(1) - .5) * 200
  14.     obj(a, 2) = min_weight + min_weight / 2 * RND(1)
  15.     obj(a, 3) = 1 '<---- 0 if fix, 1 if fly
  16.     obj(a, 4) = 1 'active point
  17.  
  18. obj(0, 0) = -64.5: obj(0, 1) = 5: obj(0, 3) = 0
  19. 'obj(obj_c - 1, 0) = 183.5: obj(obj_c - 1, 1) = -.5: obj(obj_c - 1, 3) = 0
  20.  
  21. 'connections
  22. conn_c = 100: DIM conn(conn_c - 1, 4)
  23. FOR a = 0 TO obj_c - 2
  24.     conn(a, 0) = a
  25.     conn(a, 1) = a + 1
  26.     conn(a, 2) = 15 'rope width
  27.     conn(a, 3) = 1 'active connect
  28.  
  29.  
  30.  
  31. stiff = 1
  32.  
  33. DO: _LIMIT 100
  34.  
  35.     'show info
  36.     wch = -1: FOR a = 0 TO obj_c - 1
  37.         IF _MOUSEX < 200 AND INT(_MOUSEY / 16) = a THEN wch = a: COLOR _RGB32(255, 255, 255) ELSE COLOR _RGB32(100, 100, 100)
  38.         LOCATE a + 1, 1: PRINT a; "# weight:"; INT(obj(a, 2) * 10) / 10;: IF obj(a, 3) = 0 THEN PRINT "FIX";
  39.         IF wch = a THEN PRINT "<--- using mousewheel !" 'change weights with mousewheel
  40.         IF wch <> -1 THEN obj(wch, 2) = obj(wch, 2) + mw / 10: IF obj(wch, 2) < min_weight THEN obj(wch, 2) = min_weight
  41.     NEXT a
  42.     LOCATE mony / 16 - 2, 1
  43.     IF moving THEN PRINT "press SPACE to FIX/FLY" ELSE PRINT "grab the ball with the mouse and move it!"
  44.     PRINT "S-button - rigid / flexible connections";
  45.     'draw objects
  46.     FOR a = 0 TO obj_c - 1: IF obj(a, 4) = 0 THEN _CONTINUE
  47.         x = cent_x + obj(a, 0) * zoom: y = cent_y + obj(a, 1) * zoom
  48.         IF mouse_near = a THEN COLOR _RGB32(255, 0, 0) ELSE COLOR _RGB32(200, 200, 200)
  49.         CIRCLE (x, y), obj(a, 2) * zoom: _PRINTSTRING (x, y), LTRIM$(STR$(a))
  50.     NEXT a
  51.  
  52.     'draw connections
  53.  
  54.     COLOR _RGB32(150, 150, 150)
  55.     FOR a = 0 TO conn_c - 1: IF conn(a, 3) = 0 THEN _CONTINUE
  56.         IF obj(conn(a, 0), 4) = 0 OR obj(conn(a, 1), 4) = 0 THEN _CONTINUE
  57.         x1 = obj(conn(a, 0), 0) * zoom + cent_x: y1 = obj(conn(a, 0), 1) * zoom + cent_y
  58.         x2 = obj(conn(a, 1), 0) * zoom + cent_x: y2 = obj(conn(a, 1), 1) * zoom + cent_y
  59.     LINE (x1, y1)-(x2, y2): NEXT a
  60.  
  61.  
  62.  
  63.     'calculate moving
  64.     FOR a = 0 TO obj_c - 1: obj(a, 6) = obj(a, 0): obj(a, 7) = obj(a, 1)
  65.         IF obj(a, 3) = 0 OR (mouse_near = a AND _MOUSEBUTTON(1)) OR obj(a, 4) = 0 THEN _CONTINUE
  66.         'connections vector
  67.         vec_x = 0: vec_y = 0: vec_c = 0
  68.         FOR t = 0 TO conn_c - 1: IF conn(t, 3) = 0 THEN _CONTINUE
  69.             x = -1: FOR t2 = 0 TO 1: IF conn(t, t2) = a THEN x = conn(t, t2 XOR 1)
  70.             NEXT t2
  71.             IF x <> -1 THEN
  72.                 IF obj(x, 4) THEN
  73.                     disx = obj(a, 0) - obj(x, 0)
  74.                     disy = obj(a, 1) - obj(x, 1)
  75.                     ang = (-degree(disx, disy) + 0) * pip180
  76.                     dis = SQR(disx * disx + disy * disy)
  77.                     power = (dis - (obj(a, 2) + obj(x, 2) + conn(t, 2))) * 1 ' obj(a, 2) ^ 2 '/ 20
  78.                     IF stiff THEN power = power / obj(a, 2) ^ 2 * 4
  79.                     vec_x = vec_x + SIN(ang) * power
  80.                     vec_y = vec_y - COS(ang) * power
  81.                     vec_c = vec_c + 1
  82.                 END IF
  83.             END IF
  84.         NEXT t
  85.  
  86.         gravity = .1: lass = .98
  87.         obj(a, 4) = (obj(a, 4) + vec_x / vec_c) * lass: obj(a, 5) = (obj(a, 5) + vec_y / vec_c) * lass + gravity
  88.         obj(a, 6) = obj(a, 0) + obj(a, 4): obj(a, 7) = obj(a, 1) + obj(a, 5)
  89.     NEXT a
  90.     FOR a = 0 TO obj_c - 1: FOR t = 0 TO 1: obj(a, t) = obj(a, 6 + t): NEXT t, a
  91.  
  92.  
  93.  
  94.  
  95.     'mouse moving activing
  96.     mw = 0: WHILE _MOUSEINPUT: mw = mw + _MOUSEWHEEL: WEND
  97.     IF moving = 0 THEN
  98.         mouse_near = -1
  99.         FOR a = 0 TO obj_c - 1
  100.             disx = (cent_x + obj(a, 0) * zoom) - _MOUSEX
  101.             disy = (cent_y + obj(a, 1) * zoom) - _MOUSEY
  102.             dis = SQR(disx * disx + disy * disy)
  103.             IF dis < 10 THEN mouse_near = a
  104.         NEXT a
  105.     END IF
  106.  
  107.     IF (_MOUSEBUTTON(1) AND mouse_near <> -1) OR moving THEN
  108.         moving = 1
  109.         obj(mouse_near, 0) = (_MOUSEX - cent_x) / zoom
  110.         obj(mouse_near, 1) = (_MOUSEY - cent_y) / zoom
  111.     END IF
  112.  
  113.     moving = moving AND _MOUSEBUTTON(1)
  114.  
  115.     stiff = stiff XOR ABS(inkey2$ = "s")
  116.  
  117.     _DISPLAY: CLS
  118.  
  119.     'commands
  120.     inkey2$ = INKEY$: IF inkey2$ = CHR$(27) THEN END
  121.     IF inkey2$ = " " AND _MOUSEBUTTON(1) AND moving THEN obj(mouse_near, 3) = obj(mouse_near, 3) XOR 1
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128. FUNCTION degree (a, b): degreex = ATN(a / (b + .00001)) / pip180: degreex = degreex - 180 * ABS(0 > b): degreex = degreex - 360 * (degreex < 0): degree = degreex: END FUNCTION
  129.  
  130. FUNCTION randx (x): randx = x * RND(1) - x / 2: END FUNCTION
Title: Re: Garland of pearls
Post by: MasterGy on March 22, 2022, 10:33:05 am
The trouble is, because of the translation, I can’t express myself the way I want to.
It doesn't matter what we measure. Distance, volume, size, speed, rotation, anything.
Quantity "A". This quantity wants to be "B".
Then use: B = B + (A-B) x "all"
This offers many opportunities.
I hope this thought, which I really want to go through, has gone through somewhat.
I wrote a simple illustration on it.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3.  
  4. CHANGING = .05
  5. 'if CHANGING 0:no move
  6. 'if CHANGING 1:immediate displacement
  7. 'if CHANGING between 0,1 elastic displacement
  8.  
  9.  
  10.     _LIMIT 50
  11.     x = x + (_MOUSEX - x) * CHANGING
  12.     y = y + (_MOUSEY - y) * CHANGING
  13.     _PRINTSTRING (x, y), "X"
  14.     _DISPLAY
  15.     CLS
  16.  

It shows movement. But the principle can be distance, volume, size, speed, rotation, anything ...
Title: Re: Garland of pearls
Post by: STxAxTIC on March 22, 2022, 10:52:40 am
Hey MasterGy,

I think I understand what you're saying. I use a similar piece of mathematics for mouse controls in my pendulum code:

(There may be a version of it floating around that shows a trail behind the mouse pointer.)

Code: QB64: [Select]
  1. SUB RePin
  2.     DIM AS DOUBLE n
  3.     n = .05
  4.     mx0 = n * mx + (1 - n) * mx0
  5.     my0 = n * my + (1 - n) * my0
Title: Re: Garland of pearls
Post by: MasterGy on March 22, 2022, 03:36:20 pm
grab the cobweb!

Code: QB64: [Select]
  1. CONST pip180 = 3.141592 / 180
  2. monx = 800: mony = 800: mon = _NEWIMAGE(monx, mony, 32): SCREEN mon
  3.  
  4. min_weight = 6
  5. gravity = 1
  6. obj_c = 1000 'number objects
  7. DIM obj(obj_c - 1, 9) '0,1 posX,Y 3fix/mover
  8. cent_x = monx / 2: cent_y = mony / 2: zoom = 1.8
  9.  
  10.  
  11.  
  12. 'cobweb
  13. ag0 = 320 'size
  14. ag1 = 18 'felbontas ag
  15. ag2 = 14 'felbontas net
  16. ag3 = ag0 * .18 / 2 'rad1
  17. ag4 = ag0 * .9 / 2 'rad2
  18.  
  19.  
  20. FOR t = 0 TO ag1 - 1
  21.     ang = (360 / ag1 * t + randx(360 / ag1 * .3)) * pip180
  22.     dis = ABS(ag3 - ag4)
  23.     x1 = SIN(ang) * ag3 + randx(ag3 * .1)
  24.     y1 = COS(ang) * ag3 + randx(ag3 * .1)
  25.     x2 = SIN(ang) * ag4 + randx(ag4 * .2)
  26.     y2 = COS(ang) * ag4 + randx(ag4 * .2)
  27.  
  28.     FOR t2 = 0 TO ag2 - 1
  29.         aobj = t2 + ag2 * t
  30.         obj(aobj, 0) = x1 + (x2 - x1) / (ag2 - 1) * t2
  31.         obj(aobj, 1) = y1 + (y2 - y1) / (ag2 - 1) * t2
  32.         obj(aobj, 2) = 1.9 'min_weight
  33.         obj(aobj, 3) = 1
  34.         '        IF t2 = ag2 - 1 THEN obj(aobj, 3) = 0
  35.  
  36.         obj(aobj, 4) = 1
  37.     NEXT t2
  38.  
  39.  
  40. 'connections
  41. conn_c = 1000: DIM conn(conn_c - 1, 4): ac = 0
  42.  
  43. FOR t = 0 TO ag2 - 2: FOR t2 = 0 TO ag1 - 1: conn(ac, 0) = t2 * ag2 + t: conn(ac, 1) = t2 * ag2 + ((t + 1) MOD ag2)
  44.         conn(ac, 2) = SQR((obj(conn(ac, 0), 0) - obj(conn(ac, 1), 0)) ^ 2 + (obj(conn(ac, 0), 1) - obj(conn(ac, 1), 1)) ^ 2)
  45.         conn(ac, 2) = conn(ac, 2) + randx(conn(ac, 2) * .1)
  46. conn(ac, 3) = 1: ac = ac + 1: NEXT t2, t
  47.  
  48. FOR t = 0 TO ag2 - 1: FOR t2 = 0 TO ag1 - 1: conn(ac, 0) = t + ag2 * t2: conn(ac, 1) = t + ag2 * ((t2 + 1) MOD ag1)
  49.         conn(ac, 2) = SQR((obj(conn(ac, 0), 0) - obj(conn(ac, 1), 0)) ^ 2 + (obj(conn(ac, 0), 1) - obj(conn(ac, 1), 1)) ^ 2)
  50.         conn(ac, 2) = conn(ac, 2) + randx(conn(ac, 2) * .1)
  51. conn(ac, 3) = 1: ac = ac + 1: NEXT t2, t
  52.  
  53. polar_p = ag1 * ag2
  54. FOR t = 0 TO 3
  55.     obj(polar_p + t, 0) = ((t AND 1) * 2 - 1) * ag0 / 2
  56.     obj(polar_p + t, 1) = (SGN(t AND 2) * 2 - 1) * ag0 / 2
  57.     obj(polar_p + t, 2) = 6 'min_weight
  58.     obj(polar_p + t, 3) = 0
  59.     obj(polar_p + t, 4) = 1
  60.     mindis = 999999999
  61.  
  62.     FOR a = 0 TO polar_p - 1
  63.         dis = SQR((obj(polar_p + t, 0) - obj(a, 0)) ^ 2 + (obj(polar_p + t, 1) - obj(a, 1)) ^ 2) * .8
  64.         IF dis < mindis THEN mindis = dis: conn_n = a
  65.     NEXT a
  66.     conn(ac, 0) = polar_p + t
  67.     conn(ac, 1) = conn_n
  68.     conn(ac, 2) = mindis
  69.     conn(ac, 3) = 1
  70.     ac = ac + 1
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. stiff = 1
  78.  
  79. DO: _LIMIT 100
  80.  
  81.     'show info
  82.     wch = -1: FOR a = 0 TO obj_c - 1
  83.         IF _MOUSEX < 200 AND INT(_MOUSEY / 16) = a THEN wch = a: COLOR _RGB32(255, 255, 255) ELSE COLOR _RGB32(100, 100, 100)
  84.         ' LOCATE a + 1, 1: PRINT a; "# weight:"; INT(obj(a, 2) * 10) / 10;: IF obj(a, 3) = 0 THEN PRINT "FIX";
  85.         'IF wch = a THEN PRINT "<--- using mousewheel !" 'change weights with mousewheel
  86.         IF wch <> -1 THEN obj(wch, 2) = obj(wch, 2) + mw / 10: IF obj(wch, 2) < min_weight THEN obj(wch, 2) = min_weight
  87.     NEXT a
  88.     LOCATE mony / 16 - 2, 1
  89.     IF moving THEN PRINT "press SPACE to FIX/FLY" ELSE PRINT "grab the ball with the mouse and move it!"
  90.     '    PRINT "S-button - rigid / flexible connections";
  91.     'draw objects
  92.     FOR a = 0 TO obj_c - 1: IF obj(a, 4) = 0 THEN _CONTINUE
  93.         x = cent_x + obj(a, 0) * zoom: y = cent_y + obj(a, 1) * zoom
  94.         IF mouse_near = a THEN COLOR _RGB32(255, 0, 0) ELSE COLOR _RGB32(200, 200, 200)
  95.         '        CIRCLE (x, y), obj(a, 2) * zoom: _PRINTSTRING (x, y), LTRIM$(STR$(a))
  96.     NEXT a
  97.  
  98.     'draw connections
  99.  
  100.     COLOR _RGB32(200, 200, 200)
  101.     FOR a = 0 TO conn_c - 1: IF conn(a, 3) = 0 THEN _CONTINUE
  102.         IF obj(conn(a, 0), 4) = 0 OR obj(conn(a, 1), 4) = 0 THEN _CONTINUE
  103.         x1 = obj(conn(a, 0), 0) * zoom + cent_x: y1 = obj(conn(a, 0), 1) * zoom + cent_y
  104.         x2 = obj(conn(a, 1), 0) * zoom + cent_x: y2 = obj(conn(a, 1), 1) * zoom + cent_y
  105.     LINE (x1, y1)-(x2, y2): NEXT a
  106.  
  107.  
  108.  
  109.     'calculate moving
  110.     FOR a = 0 TO obj_c - 1: obj(a, 6) = obj(a, 0): obj(a, 7) = obj(a, 1)
  111.         IF obj(a, 3) = 0 OR (mouse_near = a AND _MOUSEBUTTON(1)) OR obj(a, 4) = 0 THEN _CONTINUE
  112.         'connections vector
  113.         vec_x = 0: vec_y = 0: vec_c = 0
  114.         FOR t = 0 TO conn_c - 1: IF conn(t, 3) = 0 THEN _CONTINUE
  115.             x = -1: FOR t2 = 0 TO 1: IF conn(t, t2) = a THEN x = conn(t, t2 XOR 1)
  116.             NEXT t2
  117.             IF x <> -1 THEN
  118.                 IF obj(x, 4) THEN
  119.                     disx = obj(a, 0) - obj(x, 0)
  120.                     disy = obj(a, 1) - obj(x, 1)
  121.                     ang = (-degree(disx, disy) + 0) * pip180
  122.                     dis = SQR(disx * disx + disy * disy)
  123.                     power = (dis - conn(t, 2)) * .6 ' obj(a, 2) ^ 2 '/ 20
  124.                     IF stiff THEN power = power / obj(a, 2) ^ 2 * 4
  125.                     vec_x = vec_x + SIN(ang) * power
  126.                     vec_y = vec_y - COS(ang) * power
  127.                     vec_c = vec_c + 1
  128.                 END IF
  129.             END IF
  130.         NEXT t
  131.  
  132.         gravity = 0 '.1
  133.         lass = .90
  134.         obj(a, 4) = (obj(a, 4) + vec_x / vec_c) * lass
  135.         obj(a, 5) = (obj(a, 5) + vec_y / vec_c) * lass + gravity
  136.         obj(a, 6) = obj(a, 0) + obj(a, 4)
  137.         obj(a, 7) = obj(a, 1) + obj(a, 5)
  138.     NEXT a
  139.     FOR a = 0 TO obj_c - 1: FOR t = 0 TO 1: obj(a, t) = obj(a, 6 + t): NEXT t, a
  140.  
  141.  
  142.  
  143.  
  144.     'mouse moving activing
  145.     mw = 0: WHILE _MOUSEINPUT: mw = mw + _MOUSEWHEEL: WEND
  146.     mindis = 99999999
  147.     IF moving = 0 THEN
  148.         mouse_near = -1
  149.         FOR a = 0 TO obj_c - 1
  150.             disx = (cent_x + obj(a, 0) * zoom) - _MOUSEX
  151.             disy = (cent_y + obj(a, 1) * zoom) - _MOUSEY
  152.             dis = SQR(disx * disx + disy * disy)
  153.             IF dis < 60 AND mindis > dis THEN mouse_near = a:mindis = dis
  154.         NEXT a
  155.     END IF
  156.  
  157.     IF (_MOUSEBUTTON(1) AND mouse_near <> -1) OR moving THEN
  158.         moving = 1
  159.         obj(mouse_near, 0) = (_MOUSEX - cent_x) / zoom
  160.         obj(mouse_near, 1) = (_MOUSEY - cent_y) / zoom
  161.     END IF
  162.  
  163.     moving = moving AND _MOUSEBUTTON(1)
  164.  
  165.     stiff = stiff XOR ABS(inkey2$ = "s")
  166.  
  167.  
  168.     _DISPLAY: CLS
  169.  
  170.     'commands
  171.     inkey2$ = INKEY$: IF inkey2$ = CHR$(27) THEN END
  172.     IF inkey2$ = " " AND _MOUSEBUTTON(1) AND moving THEN obj(mouse_near, 3) = obj(mouse_near, 3) XOR 1
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. FUNCTION degree (a, b): degreex = ATN(a / (b + .00001)) / pip180: degreex = degreex - 180 * ABS(0 > b): degreex = degreex - 360 * (degreex < 0): degree = degreex: END FUNCTION
  180.  
  181. FUNCTION randx (x): randx = x * RND(1) - x / 2: END FUNCTION
Title: Re: Garland of pearls
Post by: MasterGy on March 22, 2022, 07:09:43 pm
enter a picture in the program "temp = _LOADIMAGE("pic.jpg", 32)"
crush the picture with your mouse!

Code: QB64: [Select]
  1. CONST pip180 = 3.141592 / 180
  2. monx = 800: mony = monx / _DESKTOPWIDTH * _DESKTOPHEIGHT: mon = _NEWIMAGE(monx, mony, 32): SCREEN mon: _DISPLAYORDER _HARDWARE , _SOFTWARE
  3.  
  4. min_weight = 6
  5. gravity = 1
  6. obj_c = 1000 'number objects
  7. DIM obj(obj_c - 1, 9) '0,1 posX,Y 3fix/mover
  8. cent_x = monx / 2: cent_y = mony / 2: zoom = 2.4
  9.  
  10.  
  11.  
  12. conn_c = 1000: DIM conn(conn_c - 1, 4)
  13.  
  14.  
  15. temp = _LOADIMAGE("pic.jpg", 32): _DEST temp: _SETALPHA 100: pic = _COPYIMAGE(temp, 33): _FREEIMAGE temp
  16.  
  17. sizex = 300: sizey = INT(sizex / _WIDTH(pic) * _HEIGHT(pic))
  18. felbx = 15: felby = INT(felbx / _WIDTH(pic) * _HEIGHT(pic))
  19. unitx = INT(_WIDTH(pic) / (felbx - 1))
  20. unity = INT(_HEIGHT(pic) / (felby - 1))
  21.  
  22.  
  23. FOR ty = 0 TO felby - 1
  24.     FOR tx = 0 TO felbx - 1
  25.         aobj = tx + ty * felbx
  26.         obj(aobj, 0) = sizex / felbx * tx - sizex / 2
  27.         obj(aobj, 1) = sizey / felby * ty - sizey / 2
  28.         obj(aobj, 2) = 5
  29.         obj(aobj, 3) = 1
  30.         IF (ty = 0 OR ty = felby - 1) AND (tx = 0 OR tx = felbx - 1) THEN obj(aobj, 3) = 0
  31.         obj(aobj, 4) = 1
  32.     NEXT tx
  33. NEXT ty
  34.  
  35.  
  36. 'connections
  37. FOR ty = 0 TO felby - 1: FOR tx = 0 TO felbx - 2: conn(ac, 0) = tx + ty * felbx: conn(ac, 1) = conn(ac, 0) + 1
  38.         conn(ac, 2) = SQR((obj(conn(ac, 0), 0) - obj(conn(ac, 1), 0)) ^ 2 + (obj(conn(ac, 0), 1) - obj(conn(ac, 1), 1)) ^ 2) * .8
  39. conn(ac, 3) = 1: ac = ac + 1: NEXT tx, ty
  40.  
  41. FOR tx = 0 TO felbx - 1: FOR ty = 0 TO felby - 2: conn(ac, 0) = tx + ty * felbx: conn(ac, 1) = conn(ac, 0) + felbx
  42.         conn(ac, 2) = SQR((obj(conn(ac, 0), 0) - obj(conn(ac, 1), 0)) ^ 2 + (obj(conn(ac, 0), 1) - obj(conn(ac, 1), 1)) ^ 2) * .8
  43. conn(ac, 3) = 1: ac = ac + 1: NEXT ty, tx
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. stiff = 1
  55. COLOR , 0
  56. DO: _LIMIT 30 '100
  57.  
  58.     'show info
  59.     wch = -1: FOR a = 0 TO obj_c - 1
  60.         IF _MOUSEX < 200 AND INT(_MOUSEY / 16) = a THEN wch = a: COLOR _RGB32(255, 255, 255) ELSE COLOR _RGB32(100, 100, 100)
  61.         ' LOCATE a + 1, 1: PRINT a; "# weight:"; INT(obj(a, 2) * 10) / 10;: IF obj(a, 3) = 0 THEN PRINT "FIX";
  62.         'IF wch = a THEN PRINT "<--- using mousewheel !" 'change weights with mousewheel
  63.         IF wch <> -1 THEN obj(wch, 2) = obj(wch, 2) + mw / 10: IF obj(wch, 2) < min_weight THEN obj(wch, 2) = min_weight
  64.     NEXT a
  65.     LOCATE mony / 16 - 2, 1
  66.     IF moving THEN PRINT "press SPACE to FIX/FLY" ELSE PRINT "grab the ball with the mouse and move it!"
  67.     '    PRINT "S-button - rigid / flexible connections";
  68.     'draw objects
  69.     FOR a = 0 TO obj_c - 1: IF obj(a, 4) = 0 THEN _CONTINUE
  70.         x = cent_x + obj(a, 0) * zoom: y = cent_y + obj(a, 1) * zoom
  71.         obj(a, 8) = x
  72.         obj(a, 9) = y
  73.         IF mouse_near = a THEN COLOR _RGB32(255, 0, 0) ELSE COLOR _RGB32(200, 200, 200)
  74.         '        CIRCLE (x, y), obj(a, 2) * zoom: _PRINTSTRING (x, y), LTRIM$(STR$(a))
  75.  
  76.     NEXT a
  77.  
  78.     'draw connections
  79.     COLOR _RGB32(200, 200, 200)
  80.     FOR a = 0 TO conn_c - 1: IF conn(a, 3) = 0 THEN _CONTINUE
  81.         IF obj(conn(a, 0), 4) = 0 OR obj(conn(a, 1), 4) = 0 THEN _CONTINUE
  82.         x1 = obj(conn(a, 0), 0) * zoom + cent_x: y1 = obj(conn(a, 0), 1) * zoom + cent_y
  83.         x2 = obj(conn(a, 1), 0) * zoom + cent_x: y2 = obj(conn(a, 1), 1) * zoom + cent_y
  84.         '    LINE (x1, y1)-(x2, y2), _RGBA32(0, 0, 0, 128)
  85.     NEXT a
  86.  
  87.  
  88.  
  89.     'calculate moving
  90.     FOR a = 0 TO obj_c - 1: obj(a, 6) = obj(a, 0): obj(a, 7) = obj(a, 1)
  91.         IF obj(a, 3) = 0 OR (mouse_near = a AND _MOUSEBUTTON(1)) OR obj(a, 4) = 0 THEN _CONTINUE
  92.         'connections vector
  93.         vec_x = 0: vec_y = 0: vec_c = 0
  94.         FOR t = 0 TO conn_c - 1: IF conn(t, 3) = 0 THEN _CONTINUE
  95.             x = -1: FOR t2 = 0 TO 1: IF conn(t, t2) = a THEN x = conn(t, t2 XOR 1)
  96.             NEXT t2
  97.             IF x <> -1 THEN
  98.                 IF obj(x, 4) THEN
  99.                     disx = obj(a, 0) - obj(x, 0)
  100.                     disy = obj(a, 1) - obj(x, 1)
  101.                     ang = (-degree(disx, disy) + 0) * pip180
  102.                     dis = SQR(disx * disx + disy * disy)
  103.                     power = (dis - conn(t, 2)) * 2
  104.                     IF stiff THEN power = power / obj(a, 2) ^ 2 * 4
  105.                     vec_x = vec_x + SIN(ang) * power
  106.                     vec_y = vec_y - COS(ang) * power
  107.                     vec_c = vec_c + 1
  108.                 END IF
  109.             END IF
  110.         NEXT t
  111.  
  112.         gravity = 0 '.1
  113.         lass = .93
  114.         obj(a, 4) = (obj(a, 4) + vec_x / vec_c) * lass
  115.         obj(a, 5) = (obj(a, 5) + vec_y / vec_c) * lass + gravity
  116.         obj(a, 6) = obj(a, 0) + obj(a, 4)
  117.         obj(a, 7) = obj(a, 1) + obj(a, 5)
  118.     NEXT a
  119.     FOR a = 0 TO obj_c - 1: FOR t = 0 TO 1: obj(a, t) = obj(a, 6 + t): NEXT t, a
  120.  
  121.  
  122.  
  123.  
  124.     'mouse moving activing
  125.     mw = 0: WHILE _MOUSEINPUT: mw = mw + _MOUSEWHEEL: WEND
  126.     mindis = 99999999
  127.     IF moving = 0 THEN
  128.         mouse_near = -1
  129.         FOR a = 0 TO obj_c - 1
  130.             disx = (cent_x + obj(a, 0) * zoom) - _MOUSEX
  131.             disy = (cent_y + obj(a, 1) * zoom) - _MOUSEY
  132.             dis = SQR(disx * disx + disy * disy)
  133.             IF dis < 60 AND mindis > dis THEN mouse_near = a: mindis = dis
  134.         NEXT a
  135.     END IF
  136.  
  137.     IF (_MOUSEBUTTON(1) AND mouse_near <> -1) OR moving THEN
  138.         moving = 1
  139.         obj(mouse_near, 0) = (_MOUSEX - cent_x) / zoom
  140.         obj(mouse_near, 1) = (_MOUSEY - cent_y) / zoom
  141.     END IF
  142.  
  143.     moving = moving AND _MOUSEBUTTON(1)
  144.  
  145.     stiff = stiff XOR ABS(inkey2$ = "s")
  146.  
  147.     'picture draw
  148.     FOR ty = 0 TO felby - 2: sy1 = INT(unity * ty): sy2 = sy1 + unity - 1
  149.         FOR tx = 0 TO felbx - 2: sx1 = INT(unitx * tx): sx2 = sx1 + unitx - 1: aobj = tx + ty * felbx
  150.             FOR t = 0 TO 7: p(t) = obj(aobj + (t AND 1) + SGN(t AND 2) * felbx, 8 + SGN(t AND 4)): NEXT t
  151.             _MAPTRIANGLE (sx1, sy1)-(sx2, sy1)-(sx1, sy2), pic TO(p(0), p(4))-(p(1), p(5))-(p(2), p(6))
  152.             _MAPTRIANGLE (sx2, sy2)-(sx2, sy1)-(sx1, sy2), pic TO(p(3), p(7))-(p(1), p(5))-(p(2), p(6))
  153.         NEXT tx
  154.     NEXT ty
  155.  
  156.     _DISPLAY: CLS
  157.  
  158.     'commands
  159.     inkey2$ = INKEY$: IF inkey2$ = CHR$(27) THEN END
  160.     IF inkey2$ = " " AND _MOUSEBUTTON(1) AND moving THEN obj(mouse_near, 3) = obj(mouse_near, 3) XOR 1
  161.  
  162.  
  163.  
  164.  
  165. FUNCTION degree (a, b): degreex = ATN(a / (b + .00001)) / pip180: degreex = degreex - 180 * ABS(0 > b): degreex = degreex - 360 * (degreex < 0): degree = degreex: END FUNCTION
  166. FUNCTION randx (x): randx = x * RND(1) - x / 2: END FUNCTION
  167.  
Title: Re: Garland of pearls
Post by: Dav on March 22, 2022, 09:59:26 pm
Wow -- that image version is about the coolest effect I've seen in QB64...

- Dav
Title: Re: Garland of pearls
Post by: STxAxTIC on March 22, 2022, 10:47:24 pm
Yeah, this really is something special!
Title: Re: Garland of pearls
Post by: bplus on March 23, 2022, 01:09:12 pm
MasterGy is on a roll!
Title: Re: Garland of pearls
Post by: FellippeHeitor on March 23, 2022, 01:17:20 pm
Wow, I only managed to run this now and I LOVE this spider web. Such cool work, MasterGy.

grab the cobweb!

Code: QB64: [Select]
  1. CONST pip180 = 3.141592 / 180
  2. monx = 800: mony = 800: mon = _NEWIMAGE(monx, mony, 32): SCREEN mon
  3.  
  4. min_weight = 6
  5. gravity = 1
  6. obj_c = 1000 'number objects
  7. DIM obj(obj_c - 1, 9) '0,1 posX,Y 3fix/mover
  8. cent_x = monx / 2: cent_y = mony / 2: zoom = 1.8
  9.  
  10.  
  11.  
  12. 'cobweb
  13. ag0 = 320 'size
  14. ag1 = 18 'felbontas ag
  15. ag2 = 14 'felbontas net
  16. ag3 = ag0 * .18 / 2 'rad1
  17. ag4 = ag0 * .9 / 2 'rad2
  18.  
  19.  
  20. FOR t = 0 TO ag1 - 1
  21.     ang = (360 / ag1 * t + randx(360 / ag1 * .3)) * pip180
  22.     dis = ABS(ag3 - ag4)
  23.     x1 = SIN(ang) * ag3 + randx(ag3 * .1)
  24.     y1 = COS(ang) * ag3 + randx(ag3 * .1)
  25.     x2 = SIN(ang) * ag4 + randx(ag4 * .2)
  26.     y2 = COS(ang) * ag4 + randx(ag4 * .2)
  27.  
  28.     FOR t2 = 0 TO ag2 - 1
  29.         aobj = t2 + ag2 * t
  30.         obj(aobj, 0) = x1 + (x2 - x1) / (ag2 - 1) * t2
  31.         obj(aobj, 1) = y1 + (y2 - y1) / (ag2 - 1) * t2
  32.         obj(aobj, 2) = 1.9 'min_weight
  33.         obj(aobj, 3) = 1
  34.         '        IF t2 = ag2 - 1 THEN obj(aobj, 3) = 0
  35.  
  36.         obj(aobj, 4) = 1
  37.     NEXT t2
  38.  
  39.  
  40. 'connections
  41. conn_c = 1000: DIM conn(conn_c - 1, 4): ac = 0
  42.  
  43. FOR t = 0 TO ag2 - 2: FOR t2 = 0 TO ag1 - 1: conn(ac, 0) = t2 * ag2 + t: conn(ac, 1) = t2 * ag2 + ((t + 1) MOD ag2)
  44.         conn(ac, 2) = SQR((obj(conn(ac, 0), 0) - obj(conn(ac, 1), 0)) ^ 2 + (obj(conn(ac, 0), 1) - obj(conn(ac, 1), 1)) ^ 2)
  45.         conn(ac, 2) = conn(ac, 2) + randx(conn(ac, 2) * .1)
  46. conn(ac, 3) = 1: ac = ac + 1: NEXT t2, t
  47.  
  48. FOR t = 0 TO ag2 - 1: FOR t2 = 0 TO ag1 - 1: conn(ac, 0) = t + ag2 * t2: conn(ac, 1) = t + ag2 * ((t2 + 1) MOD ag1)
  49.         conn(ac, 2) = SQR((obj(conn(ac, 0), 0) - obj(conn(ac, 1), 0)) ^ 2 + (obj(conn(ac, 0), 1) - obj(conn(ac, 1), 1)) ^ 2)
  50.         conn(ac, 2) = conn(ac, 2) + randx(conn(ac, 2) * .1)
  51. conn(ac, 3) = 1: ac = ac + 1: NEXT t2, t
  52.  
  53. polar_p = ag1 * ag2
  54. FOR t = 0 TO 3
  55.     obj(polar_p + t, 0) = ((t AND 1) * 2 - 1) * ag0 / 2
  56.     obj(polar_p + t, 1) = (SGN(t AND 2) * 2 - 1) * ag0 / 2
  57.     obj(polar_p + t, 2) = 6 'min_weight
  58.     obj(polar_p + t, 3) = 0
  59.     obj(polar_p + t, 4) = 1
  60.     mindis = 999999999
  61.  
  62.     FOR a = 0 TO polar_p - 1
  63.         dis = SQR((obj(polar_p + t, 0) - obj(a, 0)) ^ 2 + (obj(polar_p + t, 1) - obj(a, 1)) ^ 2) * .8
  64.         IF dis < mindis THEN mindis = dis: conn_n = a
  65.     NEXT a
  66.     conn(ac, 0) = polar_p + t
  67.     conn(ac, 1) = conn_n
  68.     conn(ac, 2) = mindis
  69.     conn(ac, 3) = 1
  70.     ac = ac + 1
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. stiff = 1
  78.  
  79. DO: _LIMIT 100
  80.  
  81.     'show info
  82.     wch = -1: FOR a = 0 TO obj_c - 1
  83.         IF _MOUSEX < 200 AND INT(_MOUSEY / 16) = a THEN wch = a: COLOR _RGB32(255, 255, 255) ELSE COLOR _RGB32(100, 100, 100)
  84.         ' LOCATE a + 1, 1: PRINT a; "# weight:"; INT(obj(a, 2) * 10) / 10;: IF obj(a, 3) = 0 THEN PRINT "FIX";
  85.         'IF wch = a THEN PRINT "<--- using mousewheel !" 'change weights with mousewheel
  86.         IF wch <> -1 THEN obj(wch, 2) = obj(wch, 2) + mw / 10: IF obj(wch, 2) < min_weight THEN obj(wch, 2) = min_weight
  87.     NEXT a
  88.     LOCATE mony / 16 - 2, 1
  89.     IF moving THEN PRINT "press SPACE to FIX/FLY" ELSE PRINT "grab the ball with the mouse and move it!"
  90.     '    PRINT "S-button - rigid / flexible connections";
  91.     'draw objects
  92.     FOR a = 0 TO obj_c - 1: IF obj(a, 4) = 0 THEN _CONTINUE
  93.         x = cent_x + obj(a, 0) * zoom: y = cent_y + obj(a, 1) * zoom
  94.         IF mouse_near = a THEN COLOR _RGB32(255, 0, 0) ELSE COLOR _RGB32(200, 200, 200)
  95.         '        CIRCLE (x, y), obj(a, 2) * zoom: _PRINTSTRING (x, y), LTRIM$(STR$(a))
  96.     NEXT a
  97.  
  98.     'draw connections
  99.  
  100.     COLOR _RGB32(200, 200, 200)
  101.     FOR a = 0 TO conn_c - 1: IF conn(a, 3) = 0 THEN _CONTINUE
  102.         IF obj(conn(a, 0), 4) = 0 OR obj(conn(a, 1), 4) = 0 THEN _CONTINUE
  103.         x1 = obj(conn(a, 0), 0) * zoom + cent_x: y1 = obj(conn(a, 0), 1) * zoom + cent_y
  104.         x2 = obj(conn(a, 1), 0) * zoom + cent_x: y2 = obj(conn(a, 1), 1) * zoom + cent_y
  105.     LINE (x1, y1)-(x2, y2): NEXT a
  106.  
  107.  
  108.  
  109.     'calculate moving
  110.     FOR a = 0 TO obj_c - 1: obj(a, 6) = obj(a, 0): obj(a, 7) = obj(a, 1)
  111.         IF obj(a, 3) = 0 OR (mouse_near = a AND _MOUSEBUTTON(1)) OR obj(a, 4) = 0 THEN _CONTINUE
  112.         'connections vector
  113.         vec_x = 0: vec_y = 0: vec_c = 0
  114.         FOR t = 0 TO conn_c - 1: IF conn(t, 3) = 0 THEN _CONTINUE
  115.             x = -1: FOR t2 = 0 TO 1: IF conn(t, t2) = a THEN x = conn(t, t2 XOR 1)
  116.             NEXT t2
  117.             IF x <> -1 THEN
  118.                 IF obj(x, 4) THEN
  119.                     disx = obj(a, 0) - obj(x, 0)
  120.                     disy = obj(a, 1) - obj(x, 1)
  121.                     ang = (-degree(disx, disy) + 0) * pip180
  122.                     dis = SQR(disx * disx + disy * disy)
  123.                     power = (dis - conn(t, 2)) * .6 ' obj(a, 2) ^ 2 '/ 20
  124.                     IF stiff THEN power = power / obj(a, 2) ^ 2 * 4
  125.                     vec_x = vec_x + SIN(ang) * power
  126.                     vec_y = vec_y - COS(ang) * power
  127.                     vec_c = vec_c + 1
  128.                 END IF
  129.             END IF
  130.         NEXT t
  131.  
  132.         gravity = 0 '.1
  133.         lass = .90
  134.         obj(a, 4) = (obj(a, 4) + vec_x / vec_c) * lass
  135.         obj(a, 5) = (obj(a, 5) + vec_y / vec_c) * lass + gravity
  136.         obj(a, 6) = obj(a, 0) + obj(a, 4)
  137.         obj(a, 7) = obj(a, 1) + obj(a, 5)
  138.     NEXT a
  139.     FOR a = 0 TO obj_c - 1: FOR t = 0 TO 1: obj(a, t) = obj(a, 6 + t): NEXT t, a
  140.  
  141.  
  142.  
  143.  
  144.     'mouse moving activing
  145.     mw = 0: WHILE _MOUSEINPUT: mw = mw + _MOUSEWHEEL: WEND
  146.     mindis = 99999999
  147.     IF moving = 0 THEN
  148.         mouse_near = -1
  149.         FOR a = 0 TO obj_c - 1
  150.             disx = (cent_x + obj(a, 0) * zoom) - _MOUSEX
  151.             disy = (cent_y + obj(a, 1) * zoom) - _MOUSEY
  152.             dis = SQR(disx * disx + disy * disy)
  153.             IF dis < 60 AND mindis > dis THEN mouse_near = a:mindis = dis
  154.         NEXT a
  155.     END IF
  156.  
  157.     IF (_MOUSEBUTTON(1) AND mouse_near <> -1) OR moving THEN
  158.         moving = 1
  159.         obj(mouse_near, 0) = (_MOUSEX - cent_x) / zoom
  160.         obj(mouse_near, 1) = (_MOUSEY - cent_y) / zoom
  161.     END IF
  162.  
  163.     moving = moving AND _MOUSEBUTTON(1)
  164.  
  165.     stiff = stiff XOR ABS(inkey2$ = "s")
  166.  
  167.  
  168.     _DISPLAY: CLS
  169.  
  170.     'commands
  171.     inkey2$ = INKEY$: IF inkey2$ = CHR$(27) THEN END
  172.     IF inkey2$ = " " AND _MOUSEBUTTON(1) AND moving THEN obj(mouse_near, 3) = obj(mouse_near, 3) XOR 1
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. FUNCTION degree (a, b): degreex = ATN(a / (b + .00001)) / pip180: degreex = degreex - 180 * ABS(0 > b): degreex = degreex - 360 * (degreex < 0): degree = degreex: END FUNCTION
  180.  
  181. FUNCTION randx (x): randx = x * RND(1) - x / 2: END FUNCTION
Title: Re: Garland of pearls
Post by: MasterGy on March 23, 2022, 08:00:58 pm
There is much more to it than interesting!
I asked the question on a Hungarian physics forum on facebook. If there is a meaningful answer, I will inform you! It needs to be addressed!

Hello !
It will be a little longer now. I want an answer to a question I haven't been able to sleep in days :)
Anyone who follows the group may know that I am involved in programming. It used to be machine control, more recently the math behind games. (Interestingly, behind the game programming are the most outdated applications of mathematics)
My recent creations:


I have only considered it important to note this now because I have been dealing with them feverishly lately. The basic development environment, in which I program, can only draw 1 triangle on the screen. That's why I wrote the graphic "engine", the camera movement, the physical background of the movements myself. This developer is so unpopular that I don't think QB64 is known in Hungary. I like to be active in a foreign forum because there are about a handful of people (a handful on a global scale) who struggle with this outdated developer and appreciate it.
Here a forum member wrote a simulation of a pendulum motion. This should be imagined as a fixed point + rigid connector + some weight + rigid connector + some weight .... What happens if we push? He returned the thing quite realistically on the screen. It’s some crazy math, a lot of computation, and limited because it works with 2 “weights,” and with 3, but 4 or more has so much computation that it crumbles all over and can’t be shown in real time.
I had an idea then. What if we created points in space / plane without giving it fixed coordinates. All you need to know about that point is that it is located relative to another point. There should be nothing else for that point other than to strive to maintain its relation to the other point specified.
Do this: "current situation" = "current situation" + ("where you should be" - "current situation") x "all"
This is important here. If "all" is 0, no change is made. If 1, an immediate change occurs. If all are between 0 and 1, it gives a proportionality to the dynamics / flexibility / speed of change.
Now I want to express the point somehow. Imagine a "space." It contains objects. Objects are defined by point clouds. In point clouds, we make connections between points. To an adjacent, or object center, or just a smooth random point, we connect the 1 given point within the point cloud that we just want to define, "smart." We determine for him how far away he should be from that other point ... We will try to make as many such "connections" as possible at all points in that point and that object so that he is as smart as possible and will know "where he is. ".
If this is the case, start the "simulation"! Suppose thousands of points are defined as above. Each point begins to move at such a speed / direction that the connections assigned to it can be fulfilled. All right, we're waiting ... after a while, all movement will stop. "system balanced". The shapes are nicely outlined in front of us.
Now move only 1 out of position !!! that is, ONE point !!!!!!!! This will affect the points you are connected to ... and also the points you are connected to ... and those .... so everyone is running that 1 pulse. A mechanism is starting. Impulses give and take ...
The video shows how I programmed this principle differently. Chain, an image, and a cobweb. In each case, I gave relationships to the points as above. The thing works. And here comes the point. Smart people have already written the mathematical model for every event in Newtonian physics. Rigid-body, wave curves, collision of bodies, pendulum motion, centrifugal force .... what else to say ???????????????
This idea, which I did, completely bypasses the knowledge and use of these. The screen is fully functional. You don't need any physical knowledge or complications. We need a point and the strengths of its connections and connections (or how long we tie it to another point) (perhaps a metaphor for mass?)
Using the principle, we can in principle simulate all Newtonian physics by completely circumventing their use. Example..something .... if we create a brick wall based on the above principle, we can shoot a brick out of it so that the wall collapses in reality..etc ... that's just 1 of the many options.
Here's my question. Unfortunately, I did not study mathematics or physics at higher levels. I'm sure this thing isn't some kind of invention. This can only be new to me. This may be a known, existing theory that is also taught in good places. I would like to ask those who were so lucky to learn things like what it is? what is it used for where can i find writing about this
Thank you!

(just a small philosophical remark: if we hit the wall! the above principle would show the tiny vibration of every part of the wall. what if the above principle simulates the connection between molecules? since reality "renders" so fast that we can't even measure it with instruments, we're just amazed at the end result, because it happens so fast that we can't see, so we don't understand)
To download programs running under video (running under windows) with source code:
https://drive.google.com/file/d/1hkIqbFx_JGzhYpGdQjZ1h0lZYFtiw3Ce/view?usp=sharing

Title: Re: Garland of pearls
Post by: bplus on March 23, 2022, 08:30:34 pm
There is much more to it than interesting!
I asked the question on a Hungarian physics forum on facebook. If there is a meaningful answer, I will inform you! It needs to be addressed!

Hello !
It will be a little longer now. I want an answer to a question I haven't been able to sleep in days :)
Anyone who follows the group may know that I am involved in programming. It used to be machine control, more recently the math behind games. (Interestingly, behind the game programming are the most outdated applications of mathematics)
My recent creations:


I have only considered it important to note this now because I have been dealing with them feverishly lately. The basic development environment, in which I program, can only draw 1 triangle on the screen. That's why I wrote the graphic "engine", the camera movement, the physical background of the movements myself. This developer is so unpopular that I don't think QB64 is known in Hungary. I like to be active in a foreign forum because there are about a handful of people (a handful on a global scale) who struggle with this outdated developer and appreciate it.
Here a forum member wrote a simulation of a pendulum motion. This should be imagined as a fixed point + rigid connector + some weight + rigid connector + some weight .... What happens if we push? He returned the thing quite realistically on the screen. It’s some crazy math, a lot of computation, and limited because it works with 2 “weights,” and with 3, but 4 or more has so much computation that it crumbles all over and can’t be shown in real time.
I had an idea then. What if we created points in space / plane without giving it fixed coordinates. All you need to know about that point is that it is located relative to another point. There should be nothing else for that point other than to strive to maintain its relation to the other point specified.
Do this: "current situation" = "current situation" + ("where you should be" - "current situation") x "all"
This is important here. If "all" is 0, no change is made. If 1, an immediate change occurs. If all are between 0 and 1, it gives a proportionality to the dynamics / flexibility / speed of change.
Now I want to express the point somehow. Imagine a "space." It contains objects. Objects are defined by point clouds. In point clouds, we make connections between points. To an adjacent, or object center, or just a smooth random point, we connect the 1 given point within the point cloud that we just want to define, "smart." We determine for him how far away he should be from that other point ... We will try to make as many such "connections" as possible at all points in that point and that object so that he is as smart as possible and will know "where he is. ".
If this is the case, start the "simulation"! Suppose thousands of points are defined as above. Each point begins to move at such a speed / direction that the connections assigned to it can be fulfilled. All right, we're waiting ... after a while, all movement will stop. "system balanced". The shapes are nicely outlined in front of us.
Now move only 1 out of position !!! that is, ONE point !!!!!!!! This will affect the points you are connected to ... and also the points you are connected to ... and those .... so everyone is running that 1 pulse. A mechanism is starting. Impulses give and take ...
The video shows how I programmed this principle differently. Chain, an image, and a cobweb. In each case, I gave relationships to the points as above. The thing works. And here comes the point. Smart people have already written the mathematical model for every event in Newtonian physics. Rigid-body, wave curves, collision of bodies, pendulum motion, centrifugal force .... what else to say ???????????????
This idea, which I did, completely bypasses the knowledge and use of these. The screen is fully functional. You don't need any physical knowledge or complications. We need a point and the strengths of its connections and connections (or how long we tie it to another point) (perhaps a metaphor for mass?)
Using the principle, we can in principle simulate all Newtonian physics by completely circumventing their use. Example..something .... if we create a brick wall based on the above principle, we can shoot a brick out of it so that the wall collapses in reality..etc ... that's just 1 of the many options.
Here's my question. Unfortunately, I did not study mathematics or physics at higher levels. I'm sure this thing isn't some kind of invention. This can only be new to me. This may be a known, existing theory that is also taught in good places. I would like to ask those who were so lucky to learn things like what it is? what is it used for where can i find writing about this
Thank you!

(just a small philosophical remark: if we hit the wall! the above principle would show the tiny vibration of every part of the wall. what if the above principle simulates the connection between molecules? since reality "renders" so fast that we can't even measure it with instruments, we're just amazed at the end result, because it happens so fast that we can't see, so we don't understand)
To download programs running under video (running under windows) with source code:
https://drive.google.com/file/d/1hkIqbFx_JGzhYpGdQjZ1h0lZYFtiw3Ce/view?usp=sharing



Ha! I don't think a spring connects every particle with every other but makes an interesting computer experiment.
Code: QB64: [Select]
  1. _Title "Mouse down, drag ball, release...  Boing" 'B+ 2019-01-08 from
  2. 'boing.bas for SmallBASIC 2015-07-25 MGA/B+
  3. 'coloring mods
  4.  
  5. Const xmax = 1200
  6. Const ymax = 700
  7. Screen _NewImage(xmax, ymax, 32)
  8.  
  9. Dim s(1 To 4, 1 To 2)
  10. s(1, 1) = 0: s(1, 2) = 50
  11. s(2, 1) = 0: s(2, 2) = ymax - 50
  12. s(3, 1) = xmax + 30: s(3, 2) = 50
  13. s(4, 1) = xmax + 30: s(4, 2) = ymax - 50
  14. oldtx = 0: oldtyty = 0: da = .03
  15. boingx = 0: boingy = 0
  16.     mb = _MouseButton(1)
  17.     If mb Then
  18.         tx = _MouseX + 20
  19.         ty = _MouseY
  20.     Else
  21.         tx = xmax / 2
  22.         ty = ymax / 2
  23.         If tx <> oldtx Or ty <> oldty Then
  24.             boingx = 3 * (tx - oldtx) / 4
  25.             boingy = 3 * (ty - oldty) / 4
  26.         Else
  27.             boingx = -3 * boingx / 4
  28.             boingy = -3 * boingy / 4
  29.         End If
  30.         tx = tx + boingx
  31.         ty = ty + boingy
  32.     End If
  33.     a = 0
  34.     oldtx = tx
  35.     oldty = ty
  36.     Cls
  37.     For corner = 1 To 4
  38.         s1x = s(corner, 1)
  39.         s1y = s(corner, 2)
  40.         dx = (tx - s1x) / 2000
  41.         dy = (ty - s1y) / 2000
  42.         x = tx - 20
  43.         y = ty
  44.         For i = 1 To 2000
  45.             sx = 20 * Cos(a) + x
  46.             sy = 20 * Sin(a) + y
  47.             Line (sx, sy + 5)-(sx + 4, sy + 5), _RGB32(118, 118, 118), BF
  48.             Line (sx, sy + 4)-(sx + 4, sy + 4), _RGB32(148, 148, 148), BF
  49.             Line (sx, sy + 3)-(sx + 4, sy + 3), _RGB32(238, 238, 238), BF
  50.             Line (sx, sy + 2)-(sx + 4, sy + 3), _RGB32(208, 208, 208), BF
  51.             Line (sx, sy + 1)-(sx + 4, sy + 1), _RGB32(168, 168, 168), BF
  52.             Line (sx, sy)-(sx + 4, sy), _RGB32(108, 108, 108), BF
  53.             Line (sx, sy - 1)-(sx + 4, sy - 1), _RGB32(68, 68, 68), BF
  54.             x = x - dx: y = y - dy
  55.             a = a + da
  56.         Next
  57.     Next
  58.     For r = 50 To 1 Step -1
  59.         g = (50 - r) * 5 + 5
  60.         Color _RGB32(g, g, g)
  61.         fcirc tx - 20, ty, r
  62.     Next
  63.     _Display
  64.     _Limit 15
  65.  
  66. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  67. Sub fcirc (CX As Long, CY As Long, R As Long)
  68.     Dim subRadius As Long, RadiusError As Long
  69.     Dim X As Long, Y As Long
  70.  
  71.     subRadius = Abs(R)
  72.     RadiusError = -subRadius
  73.     X = subRadius
  74.     Y = 0
  75.  
  76.     If subRadius = 0 Then PSet (CX, CY): Exit Sub
  77.  
  78.     ' Draw the middle span here so we don't draw it twice in the main loop,
  79.     ' which would be a problem with blending turned on.
  80.     Line (CX - X, CY)-(CX + X, CY), , BF
  81.  
  82.     While X > Y
  83.         RadiusError = RadiusError + Y * 2 + 1
  84.         If RadiusError >= 0 Then
  85.             If X <> Y + 1 Then
  86.                 Line (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  87.                 Line (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  88.             End If
  89.             X = X - 1
  90.             RadiusError = RadiusError - X * 2
  91.         End If
  92.         Y = Y + 1
  93.         Line (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  94.         Line (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  95.     Wend
  96.  
  97.  

@MasterGy try and get some sleep.
Title: Re: Garland of pearls
Post by: jack on March 24, 2022, 09:28:11 am
To download programs running under video (running under windows) with source code:
https://drive.google.com/file/d/1hkIqbFx_JGzhYpGdQjZ1h0lZYFtiw3Ce/view?usp=sharing
I get an error from google not allowing me to download, is there another way you can share your creations?
btw, the programs you presented in this thread are fantastic! :)
Title: Re: Garland of pearls
Post by: MasterGy on April 03, 2022, 02:57:42 pm
I'm very interested in this thing. Unfortunately I had little time, the season started. The above programs work in 2d. I started simulating the behavior of points in 3d. The goal would be collisions. I would like to see what I have described above. For now, you can see that it works nicely in 3D as well. Stretched mesh, gravity, WASD + mouse like in games. Use the space bar to move 1 point out of place. All other points will then respond and the system will attempt to return to the idle state.

Code: QB64: [Select]
  1. mouse_sens = .5
  2. tightness = 2
  3. gravity = .005
  4. lass = .94
  5. power = -0.3
  6.  
  7.  
  8.  
  9. CONST pip180 = 3.141592 / 180
  10. DIM SHARED monx, mony: monx = 800: mony = monx / _DESKTOPWIDTH * _DESKTOPHEIGHT: mon = _NEWIMAGE(monx, mony, 32): SCREEN mon: _FULLSCREEN: _MOUSEHIDE
  11.  
  12. DIM SHARED points_c_arr, conn_c_arr
  13. points_c_arr = 2000
  14. conn_c_arr = 2000
  15.  
  16. DIM SHARED points(points_c_arr, 19), conn(conn_c_arr, 9), cam(19), me(19), conn_c, points_c
  17.  
  18.  
  19. make_grid 1, -10, -10, 10, 10, -10, 0, -10, 10, 0, 10, 10, 0, 1.2
  20.  
  21.  
  22.  
  23.  
  24.  
  25. 'make_sphere 2, 0, 10, 0, 5, 8 'ident,X,Y,Z,RAD,POINTS
  26.  
  27.     'control
  28.     mousex = 0: mousey = 0: mw = 0: WHILE _MOUSEINPUT: mousex = mousex + _MOUSEMOVEMENTX: mousey = mousey + _MOUSEMOVEMENTY: mw = mw + _MOUSEWHEEL: WEND
  29.     k_left = _KEYDOWN(19200) OR _KEYDOWN(ASC("a")): k_right = _KEYDOWN(19712) OR _KEYDOWN(ASC("d"))
  30.     k_up = _KEYDOWN(18432) OR _KEYDOWN(ASC("w")): k_down = _KEYDOWN(20480) OR _KEYDOWN(ASC("s"))
  31.     me(2) = me(2) + mw: me(3) = me(3) - mousex * mouse_sens: me(4) = me(4) + mousey * mouse_sens: IF ABS(me(4)) > 80 THEN me(4) = 80 * SGN(me(4))
  32.     go = -.5 * ABS(k_left OR k_right OR k_up OR k_down): go_ang = -90 * (k_left - k_right) + (me(3)) - 90
  33.     ang1 = go_ang * pip180: ang2 = (90 - me(4)) * pip180: IF k_down THEN go = -go
  34.     me(0) = me(0) + SIN(ang2) * COS(ang1) * go: me(1) = me(1) + SIN(ang2) * SIN(ang1) * go: me(2) = me(2) + COS(ang2) * go
  35.     FOR t = 0 TO 9: cam(t) = me(t): NEXT t: cam(13) = cam(3) * pip180: cam(14) = cam(4) * pip180
  36.  
  37.  
  38.  
  39.     'calculate moving
  40.  
  41.     FOR a = 0 TO points_c_arr - 1: FOR t = 0 TO 2: points(a, 11 + t) = points(a, t): NEXT t: IF (points(a, 14) = 0 OR points(a, 6) = 0) THEN _CONTINUE
  42.  
  43.         'connections vector
  44.         vec(0) = 0: vec(1) = 0: vec(2) = 0: vec_c = 0
  45.         FOR t = 0 TO conn_c - 1: IF conn(t, 3) = 0 THEN _CONTINUE
  46.             x = -1: FOR t2 = 0 TO 1: IF conn(t, t2) = a THEN x = conn(t, t2 XOR 1)
  47.             NEXT t2
  48.             IF x <> -1 THEN
  49.                 FOR av = 0 TO 2: disv = points(a, av) - points(x, av): vec(av) = vec(av) + ((ABS(disv) - conn(t, 4 + av))) * SGN(disv) * power
  50.                 NEXT av: vec_c = vec_c + 1
  51.             END IF
  52.         NEXT t
  53.  
  54.         FOR av = 0 TO 2: points(a, 3 + av) = (points(a, 3 + av) + vec(av) / vec_c) * lass: NEXT av: points(a, 4) = points(a, 4) - gravity
  55.         FOR av = 0 TO 2: points(a, 11 + av) = points(a, av) + points(a, 3 + av): NEXT av
  56.     NEXT a
  57.  
  58.     FOR a = 0 TO points_c_arr - 1: FOR t = 0 TO 2: points(a, t) = points(a, 11 + t): NEXT t, a
  59.  
  60.     inkey2$ = INKEY$
  61.     IF inkey2$ = " " THEN
  62.         DO: a = INT(points_c_arr * RND(1)): LOOP UNTIL points(a, 14) AND points(a, 6)
  63.         rand = 30: FOR t = 0 TO 2: points(a, t) = points(a, t) + rand * RND(1) - rand / 2: NEXT t
  64.     END IF
  65.  
  66.     'calculating points
  67.     FOR ap = 0 TO points_c_arr - 1: IF points(ap, 6) = 0 THEN _CONTINUE
  68.         points(ap, 8) = points(ap, 0): points(ap, 9) = points(ap, 1): points(ap, 10) = points(ap, 2)
  69.     rotate_display points(ap, 8), points(ap, 9), points(ap, 10): NEXT ap
  70.  
  71.     'draw connections
  72.     FOR ac = 0 TO conn_c_arr - 1: IF conn(ac, 3) = 0 THEN _CONTINUE
  73.         FOR t1 = 0 TO 1: FOR t2 = 0 TO 2: coo(t1, t2) = points(conn(ac, t1), 8 + t2): NEXT t2, t1
  74.         line_3d coo(0, 0), coo(0, 1), coo(0, 2), coo(1, 0), coo(1, 1), coo(1, 2)
  75.     NEXT ac
  76.  
  77.     'fix points draw signal
  78.     FOR ap = 0 TO points_c_arr - 1: IF points(ap, 6) = 0 OR points(ap, 14) OR points(ap, 10) > -1 THEN _CONTINUE
  79.         FOR t2 = 0 TO 2: coo(0, t2) = points(ap, 8 + t2): NEXT t2
  80.         point3d_to_pset coo(0, 0), coo(0, 1), coo(0, 2): CIRCLE (coo(0, 0), coo(0, 1)), 12000 / coo(0, 2)
  81.     NEXT ap
  82.  
  83.     COLOR _RGB32(200, 200, 200): LOCATE mony / 16 - 2: PRINT "WASD+mouse      SPACE - impulse to a random point ";
  84.     _DISPLAY
  85.     CLS
  86.  
  87.  
  88. SUB line_3d (x0, y0, z0, x1, y1, z1)
  89.     IF z0 < -1 AND z1 < -1 THEN point3d_to_pset x0, y0, z0: point3d_to_pset x1, y1, z1: LINE (x0, y0)-(x1, y1)
  90.  
  91. SUB point3d_to_pset (x, y, z): multi = 400: zlimit = -2: x = monx / 2 + (x + zlimit) / -z * multi: y = mony / 2 + (y + zlimit) / z * multi
  92.     grey = 255 - ABS(z) * .1: IF grey < 10 THEN grey = 10
  93.     COLOR _RGB32(grey, grey, grey)
  94.  
  95. SUB make_grid (acc, x0, y0, z0, x1, y1, z1, x2, y2, z2, x3, y3, z3, res): p_start = points_c
  96.     resA = INT((dis(x0 - x1, y0 - y1, z0 - z1) + dis(x2 - x3, y2 - y3, z2 - z3)) / 2 / res)
  97.     resB = INT((dis(x0 - x2, y0 - y2, z0 - z2) + dis(x1 - x3, y1 - y3, z1 - z3)) / 2 / res)
  98.     FOR a = 0 TO resA - 1: n = 1 / resA * a
  99.         tx1 = scale(x0, x1, n): ty1 = scale(y0, y1, n): tz1 = scale(z0, z1, n): tx2 = scale(x2, x3, n): ty2 = scale(y2, y3, n): tz2 = scale(z2, z3, n)
  100.         FOR b = 0 TO resB - 1: n = 1 / resB * b: l = p_start + b * resA + a
  101.             points(l, 0) = scale(tx1, tx2, n): points(l, 2) = scale(ty1, ty2, n): points(l, 1) = scale(tz1, tz2, n)
  102.             points(l, 6) = 1: points(l, 7) = acc: points(l, 14) = 1: IF (a = 0 OR a = resA - 1) AND (b = 0 OR b = resB - 1) THEN points(l, 14) = 0
  103.     NEXT b, a
  104.     FOR ty = 0 TO resB - 1: FOR tx = 0 TO resA - 2: p1 = tx + ty * resA: conn_add p1, p1 + 1, tightness: NEXT tx, ty
  105.     FOR tx = 0 TO resA - 1: FOR ty = 0 TO resB - 2: p1 = tx + ty * resA: conn_add p1, p1 + resA, tightness: NEXT ty, tx
  106.     points_c = points_c + resA * resB
  107.  
  108. FUNCTION dis (a, b, c): dis = SQR(a * a + b * b + c * c): END FUNCTION
  109. FUNCTION scale (a, b, n): scale = a + (b - a) * n: END FUNCTION
  110. SUB conn_add (p1, p2, tightness): IF p1 = p2 THEN EXIT SUB
  111.     FOR t = 0 TO conn_c_arr - 1: IF (conn(t, 0) = p1 AND conn(t, 1) = p2) OR (conn(t, 0) = p2 AND conn(t, 1) = p1) THEN EXIT SUB
  112.     NEXT t: REDIM disx(2): conn(conn_c, 0) = p1: conn(conn_c, 1) = p2
  113.     FOR t = 0 TO 2: conn(conn_c, 4 + t) = (ABS(points(p1, t) - points(p2, t))) * tightness: NEXT t
  114.     conn(conn_c, 2) = dis(conn(conn_c, 4), conn(conn_c, 5), conn(conn_c, 6)): conn(conn_c, 3) = 1: conn_c = conn_c + 1
  115. SUB rotate_2d (x, y, ang): x1 = x * COS(ang) - y * SIN(ang): y1 = x * SIN(ang) + y * COS(ang): x = x1: y = y1: END SUB
  116. SUB rotate_display (x, y, z)
  117.     x = (x - cam(0)) * 100: y = (y - cam(2)) * 100: z = -(z - cam(1)) * 100: rotate_2d x, z, cam(13): rotate_2d y, z, cam(14)
  118. FUNCTION degree (a, b): degreex = ATN(a / (b + .00001)) / pip180: degreex = degreex - 180 * ABS(0 > b): degreex = degreex - 360 * (degreex < 0): degree = degreex: END FUNCTION
  119.  
  120.  
  121.  
  122.  
  123.  
  124. 'POINTS array
  125. '0,1,2 : X,Y,Z
  126. '3,4,5 : X,Y,Z vector
  127. '6:active 7:identifier
  128. '8,9,10 :X,Y,Z calculated to maptriangle
  129. '11,12,13 :X,Y,Z moving temporary calculating
  130. '14: 0:FIX 1:MOVING
  131.  
  132.  
  133. 'CONN array
  134. '0:point#1
  135. '1:point#2
  136. '2:distance
  137. '3:active
  138. '4:disX
  139. '5:disY
  140. '6:disZ
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
Title: Re: Garland of pearls
Post by: MasterGy on April 05, 2022, 03:32:44 pm
I added another curiosity. (but I haven't covered the point yet)
I recently had a question with _MAPTRIANGLE that calculates 2d points. I wanted to know so I could move with a mouse in 3d. This has now been achieved with a simple solution.
Stretched mesh in 3d. Walkable with WASD + mouse like in 3D games. If you press the Q key, you can grab the point with the mouse and move it. You can nail it with the SPACE key.



Code: QB64: [Select]
  1. mouse_sens = .5
  2. tightness = 1
  3. gravity = .005
  4. lass = .94
  5. power = -0.3
  6. adj = .8 'moving speed 3d point mouse
  7.  
  8.  
  9. CONST pip180 = 3.141592 / 180
  10. DIM SHARED monx, mony: monx = 800: mony = monx / _DESKTOPWIDTH * _DESKTOPHEIGHT: mon = _NEWIMAGE(monx, mony, 32): SCREEN mon: _FULLSCREEN: _MOUSEHIDE
  11.  
  12. DIM SHARED points_c_arr, conn_c_arr
  13. points_c_arr = 2000
  14. conn_c_arr = 2000
  15.  
  16. DIM SHARED points(points_c_arr, 19), conn(conn_c_arr, 9), cam(19), me(19), conn_c, points_c
  17.  
  18.  
  19. make_grid 1, -10, -10, 10, 10, -10, 0, -10, 10, 0, 10, 10, 0, 1.5
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.     'control
  27.     mousex = 0: mousey = 0: mw = 0: WHILE _MOUSEINPUT: mousex = mousex + _MOUSEMOVEMENTX: mousey = mousey + _MOUSEMOVEMENTY: mw = mw + _MOUSEWHEEL: WEND
  28.     k_left = _KEYDOWN(19200) OR _KEYDOWN(ASC("a")): k_right = _KEYDOWN(19712) OR _KEYDOWN(ASC("d")): inkey2$ = INKEY$: IF inkey2$ = CHR$(27) THEN END
  29.     k_up = _KEYDOWN(18432) OR _KEYDOWN(ASC("w")): k_down = _KEYDOWN(20480) OR _KEYDOWN(ASC("s")): key_take = _KEYDOWN(ASC("q"))
  30.     me(2) = me(2) + mw: IF key_take THEN _MOUSESHOW ELSE _MOUSEHIDE
  31.     IF key_take = 0 THEN me(3) = me(3) - mousex * mouse_sens: me(4) = me(4) + mousey * mouse_sens: IF ABS(me(4)) > 80 THEN me(4) = 80 * SGN(me(4))
  32.     go = -.5 * ABS(k_left OR k_right OR k_up OR k_down): go_ang = -90 * (k_left - k_right) + (me(3)) - 90
  33.     ang1 = go_ang * pip180: ang2 = (90 - me(4)) * pip180: IF k_down THEN go = -go
  34.     me(0) = me(0) + SIN(ang2) * COS(ang1) * go: me(1) = me(1) + SIN(ang2) * SIN(ang1) * go: me(2) = me(2) + COS(ang2) * go
  35.     FOR t = 0 TO 9: cam(t) = me(t): NEXT t: cam(13) = cam(3) * pip180: cam(14) = cam(4) * pip180
  36.  
  37.  
  38.  
  39.     'calculate moving
  40.     FOR a = 0 TO points_c_arr - 1: FOR t = 0 TO 2: points(a, 11 + t) = points(a, t): NEXT t
  41.         IF (points(a, 14) = 0 OR points(a, 6) = 0) OR a = take_moving THEN _CONTINUE
  42.  
  43.         'connections vector
  44.         vec(0) = 0: vec(1) = 0: vec(2) = 0: vec_c = 0
  45.         FOR t = 0 TO conn_c - 1: IF conn(t, 3) = 0 THEN _CONTINUE
  46.             x = -1: FOR t2 = 0 TO 1: IF conn(t, t2) = a THEN x = conn(t, t2 XOR 1)
  47.             NEXT t2
  48.             IF x <> -1 THEN
  49.                 FOR av = 0 TO 2: disv = points(a, av) - points(x, av): vec(av) = vec(av) + ((ABS(disv) - conn(t, 4 + av))) * SGN(disv) * power
  50.                 NEXT av: vec_c = vec_c + 1
  51.             END IF
  52.         NEXT t
  53.  
  54.         FOR av = 0 TO 2: points(a, 3 + av) = (points(a, 3 + av) + vec(av) / vec_c) * lass: NEXT av: points(a, 4) = points(a, 4) - gravity
  55.         FOR av = 0 TO 2: points(a, 11 + av) = points(a, av) + points(a, 3 + av): NEXT av
  56.     NEXT a
  57.  
  58.     FOR a = 0 TO points_c_arr - 1: FOR t = 0 TO 2: points(a, t) = points(a, 11 + t): NEXT t, a
  59.  
  60.  
  61.  
  62.     'calculating points
  63.     FOR ap = 0 TO points_c_arr - 1: IF points(ap, 6) = 0 THEN _CONTINUE
  64.         points(ap, 8) = points(ap, 0): points(ap, 9) = points(ap, 1): points(ap, 10) = points(ap, 2)
  65.         rotate_display points(ap, 8), points(ap, 9), points(ap, 10)
  66.         FOR t = 0 TO 2: points(ap, 15 + t) = points(ap, 8 + t): NEXT t: point3d_to_pset points(ap, 15), points(ap, 16), points(ap, 17)
  67.     NEXT ap
  68.  
  69.     'draw connections
  70.     FOR ac = 0 TO conn_c_arr - 1: IF conn(ac, 3) = 0 THEN _CONTINUE
  71.         FOR t1 = 0 TO 1: FOR t2 = 0 TO 2: coo(t1, t2) = points(conn(ac, t1), 15 + t2): NEXT t2, t1
  72.         IF points(conn(ac, 0), 10) < -1 AND points(conn(ac, 1), 10) < -1 THEN
  73.             LINE (coo(0, 0), coo(0, 1))-(coo(1, 0), coo(1, 1)), _RGB32(coo(0, 2), coo(0, 2), coo(0, 2))
  74.         END IF
  75.     NEXT ac
  76.  
  77.     'fix points draw signal
  78.     FOR ap = 0 TO points_c_arr - 1: IF points(ap, 6) = 0 OR points(ap, 14) OR points(ap, 10) > -1 THEN _CONTINUE
  79.         CIRCLE (points(ap, 15), points(ap, 16)), 12000 / points(ap, 10), _RGB32(points(ap, 17), points(ap, 17), points(ap, 17))
  80.     NEXT ap
  81.  
  82.     'take point with mouse
  83.     IF key_take AND take_moving = -1 THEN
  84.         take_sens = monx * 0.01: mindis = 999999: take = -1
  85.         FOR ap = 0 TO points_c_arr - 1: IF points(ap, 6) = 0 OR points(ap, 10) > -1 THEN _CONTINUE
  86.             disx = ABS(_MOUSEX - points(ap, 15)): IF disx > take_sens THEN _CONTINUE
  87.             disy = ABS(_MOUSEY - points(ap, 16)): IF disy > take_sens THEN _CONTINUE
  88.             disv = dis(disx, disy, 0): IF disv > take_sens OR disv > mindis THEN _CONTINUE
  89.         take = ap: mindis = disv: NEXT ap: IF take <> -1 THEN CIRCLE (points(take, 15), points(take, 16)), 5000 / points(take, 10), _RGB32(255, 0, 0)
  90.     END IF
  91.     IF _MOUSEBUTTON(1) = 0 OR key_take = 0 THEN take_moving = -1
  92.     IF key_take AND _MOUSEBUTTON(1) AND take_moving = -1 AND take <> -1 THEN take_moving = take: dis_take = dis(points(take, 8), points(take, 9), points(take, 10))
  93.     IF take <> -1 AND inkey2$ = " " THEN points(take, 14) = points(take, 14) XOR 1
  94.  
  95.     'move with mouse 3d point
  96.     IF take_moving <> -1 THEN
  97.         IF dis(_MOUSEX - points(take_moving, 15), _MOUSEY - points(take_moving, 16), 0) > 20 THEN
  98.             mindis = 99999: FOR ang1 = 0 TO 360 * pip180 STEP 10 * pip180: FOR ang2 = 0 TO 180 * pip180 STEP 10 * pip180
  99.                     px = points(take_moving, 0) + adj * SIN(ang2) * COS(ang1): py = points(take_moving, 1) + adj * SIN(ang2) * SIN(ang1)
  100.                     pz = points(take_moving, 2) + adj * COS(ang2): vx = px: vy = py: vz = pz
  101.                     rotate_display vx, vy, vz: IF ABS(dis(vx, vy, vz) - dis_take) > 300 THEN _CONTINUE
  102.                     point3d_to_pset vx, vy, vz: disx = dis(_MOUSEX - vx, _MOUSEY - vy, 0)
  103.                     IF disx < mindis THEN mindis = disx: cx = px: cy = py: cz = pz
  104.             NEXT ang2, ang1: points(take_moving, 0) = cx: points(take_moving, 1) = cy: points(take_moving, 2) = cz
  105.         END IF
  106.     END IF
  107.  
  108.     mess$ = "moving WASD+mouse      Q-key fixing and moving point"
  109.     IF key_take THEN mess$ = "SPACE-fix/float point  MOUSEBUTTON - hold point"
  110.     IF take_moving <> -1 THEN mess$ = "moving the point with mouse"
  111.     COLOR _RGB32(200, 200, 200): LOCATE mony / 16 - 2: PRINT mess$;
  112.     _DISPLAY
  113.     CLS
  114.  
  115.  
  116. SUB point3d_to_pset (x, y, z): zlimit = -2: x = monx * .5 + (x + zlimit) / -z * 400: y = mony * .5 + (y + zlimit) / z * 400
  117.     z = 255 - ABS(z) * .1: IF z < 10 THEN grey = 10
  118.  
  119. SUB make_grid (acc, x0, y0, z0, x1, y1, z1, x2, y2, z2, x3, y3, z3, res): p_start = points_c
  120.     resA = INT((dis(x0 - x1, y0 - y1, z0 - z1) + dis(x2 - x3, y2 - y3, z2 - z3)) / 2 / res)
  121.     resB = INT((dis(x0 - x2, y0 - y2, z0 - z2) + dis(x1 - x3, y1 - y3, z1 - z3)) / 2 / res)
  122.     FOR a = 0 TO resA - 1: n = 1 / resA * a
  123.         tx1 = scale(x0, x1, n): ty1 = scale(y0, y1, n): tz1 = scale(z0, z1, n): tx2 = scale(x2, x3, n): ty2 = scale(y2, y3, n): tz2 = scale(z2, z3, n)
  124.         FOR b = 0 TO resB - 1: n = 1 / resB * b: l = p_start + b * resA + a
  125.             points(l, 0) = scale(tx1, tx2, n): points(l, 2) = scale(ty1, ty2, n): points(l, 1) = scale(tz1, tz2, n)
  126.             points(l, 6) = 1: points(l, 7) = acc: points(l, 14) = 1: IF (a = 0 OR a = resA - 1) AND (b = 0 OR b = resB - 1) THEN points(l, 14) = 0
  127.     NEXT b, a
  128.     FOR ty = 0 TO resB - 1: FOR tx = 0 TO resA - 2: p1 = tx + ty * resA: conn_add p1, p1 + 1, tightness: NEXT tx, ty
  129.     FOR tx = 0 TO resA - 1: FOR ty = 0 TO resB - 2: p1 = tx + ty * resA: conn_add p1, p1 + resA, tightness: NEXT ty, tx
  130.     points_c = points_c + resA * resB
  131.  
  132. FUNCTION dis (a, b, c): dis = SQR(a * a + b * b + c * c): END FUNCTION
  133. FUNCTION scale (a, b, n): scale = a + (b - a) * n: END FUNCTION
  134. SUB conn_add (p1, p2, tightness): IF p1 = p2 THEN EXIT SUB
  135.     FOR t = 0 TO conn_c_arr - 1: IF (conn(t, 0) = p1 AND conn(t, 1) = p2) OR (conn(t, 0) = p2 AND conn(t, 1) = p1) THEN EXIT SUB
  136.     NEXT t: REDIM disx(2): conn(conn_c, 0) = p1: conn(conn_c, 1) = p2
  137.     FOR t = 0 TO 2: conn(conn_c, 4 + t) = (ABS(points(p1, t) - points(p2, t))) * tightness: NEXT t
  138.     conn(conn_c, 2) = dis(conn(conn_c, 4), conn(conn_c, 5), conn(conn_c, 6)): conn(conn_c, 3) = 1: conn_c = conn_c + 1
  139. SUB rotate_2d (x, y, ang): x1 = x * COS(ang) - y * SIN(ang): y1 = x * SIN(ang) + y * COS(ang): x = x1: y = y1: END SUB
  140. SUB rotate_display (x, y, z)
  141.     x = (x - cam(0)) * 100: y = (y - cam(2)) * 100: z = -(z - cam(1)) * 100: rotate_2d x, z, cam(13): rotate_2d y, z, cam(14)
  142. FUNCTION degree (a, b): degreex = ATN(a / (b + .00001)) / pip180: degreex = degreex - 180 * ABS(0 > b): degreex = degreex - 360 * (degreex < 0): degree = degreex: END FUNCTION
  143.  
  144.  
  145.  
  146.  
  147.  
  148. 'POINTS array
  149. '0,1,2 : XYZ
  150. '3,4,5 : XYZ vector
  151. '6:active 7:identifier
  152. '8,9,10 :XYZ calculated to maptriangle
  153. '11,12,13 :XYZ moving temporary calculating
  154. '14: 0:FIX 1:MOVING
  155. '15,16,17 2D-X,Y, greyscale
  156.  
  157.  
  158. 'CONN array
  159. '0:point#1
  160. '1:point#2
  161. '2:distance
  162. '3:active
  163. '4:disX
  164. '5:disY
  165. '6:disZ
  166.  
  167.  
  168.  
  169.  
  170.  
  171.