Author Topic: Spring  (Read 4931 times)

0 Members and 1 Guest are viewing this topic.

FellippeHeitor

  • Guest
Spring
« on: July 17, 2018, 01:37:12 pm »
Grab the red handle and pull it in any direction and watch as the elastic string will pull it back to its original spot.

Change the resistance of the string using the slider.

I called this Spring because my original idea was to have a spring-like spiral go from the origin point to the handle, but that went beyond my math skills...
screenshot.png
* screenshot.png (Filesize: 34.26 KB, Dimensions: 806x691, Views: 370)
* Spring.zip (Filesize: 78.25 KB, Downloads: 238)
« Last Edit: July 17, 2018, 01:50:34 pm by FellippeHeitor »

Marked as best answer by on Yesterday at 05:13:58 pm

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Spring
« Reply #1 on: July 17, 2018, 09:15:25 pm »
  • Undo Best Answer
  • Hi Fellippe,

    I haven't checked code yet but already I am thinking there should be a paddle on the other end of the ball and "spring" or bungee cord... would that work with the mouse moving the paddle...? hmm...

    FellippeHeitor

    • Guest
    Re: Spring
    « Reply #2 on: July 18, 2018, 11:49:13 am »
    Replace just the contents of the .bas file in the zip above with the code below and turn the elastic band into a thicker version of itself, one that will actually resemble a real stretching elastic band that changes thickness according to how far it's stretched.

    Code: QB64: [Select]
    1. ': This program uses
    2. ': InForm - GUI library for QB64 - Beta version 8
    3. ': Fellippe Heitor, 2016-2018 - fellippe@qb64.org - @fellippeheitor
    4. ': https://github.com/FellippeHeitor/InForm
    5. '-----------------------------------------------------------
    6.  
    7. ': Controls' IDs: ------------------------------------------------------------------
    8. DIM SHARED Spring AS LONG
    9. DIM SHARED PictureBox1 AS LONG
    10. DIM SHARED ResistanceLB AS LONG
    11. DIM SHARED TrackBar1 AS LONG
    12. DIM SHARED ExitBT AS LONG
    13.  
    14. TYPE vector
    15.     x AS SINGLE
    16.     y AS SINGLE
    17.  
    18. DIM SHARED origin AS vector, handle AS vector, mouse AS vector
    19. DIM SHARED diff AS vector
    20.  
    21. origin.x = 400
    22. origin.y = 300
    23. handle = origin
    24.  
    25. ': External modules: ---------------------------------------------------------------
    26. '$INCLUDE:'InForm\InForm.ui'
    27. '$INCLUDE:'InForm\xp.uitheme'
    28. '$INCLUDE:'Spring.frm'
    29.  
    30. ': Event procedures: ---------------------------------------------------------------
    31. SUB __UI_BeforeInit
    32.  
    33.  
    34. SUB __UI_OnLoad
    35.  
    36.  
    37. SUB __UI_BeforeUpdateDisplay STATIC
    38.     'This event occurs at approximately 30 frames per second.
    39.     'You can change the update frequency by calling SetFrameRate DesiredRate%
    40.  
    41.     BeginDraw PictureBox1
    42.     CLS
    43.  
    44.     mouse.x = _MOUSEX
    45.     mouse.y = _MOUSEY
    46.  
    47.     d = distB(origin, handle)
    48.     m = distB(handle, mouse)
    49.  
    50.     PRINT d
    51.  
    52.         IF NOT mouseDown THEN
    53.             IF m < 30 THEN mouseDown = True
    54.         ELSE
    55.             handle = mouse
    56.         END IF
    57.     ELSE
    58.         IF mouseDown THEN
    59.             mouseDown = False
    60.         END IF
    61.     END IF
    62.  
    63.     IF NOT mouseDown THEN
    64.         IF d > 0 THEN
    65.             diff.y = handle.y - origin.y
    66.             diff.x = handle.x - origin.x
    67.             'a = _ATAN2(diff.x, diff.y)
    68.  
    69.             handle.x = handle.x - diff.x / map(d, 0, _WIDTH, 1, 32 - Control(TrackBar1).Value)
    70.             handle.y = handle.y - diff.y / map(d, 0, _HEIGHT, 1, 32 - Control(TrackBar1).Value)
    71.         END IF
    72.     END IF
    73.  
    74.     COLOR _RGB32(127, 127, 127)
    75.     thickLine origin.x, origin.y, handle.x, handle.y, map(d, 0, _WIDTH, 10, 1)
    76.     CircleFill origin.x, origin.y, 5, _RGB32(255, 0, 0)
    77.     CircleFill handle.x, handle.y, 30, _RGB32(255, 0, 0)
    78.     EndDraw PictureBox1
    79.  
    80. SUB __UI_BeforeUnload
    81.     'If you set __UI_UnloadSignal = False here you can
    82.     'cancel the user's request to close.
    83.  
    84.  
    85. SUB __UI_Click (id AS LONG)
    86.     SELECT CASE id
    87.         CASE Spring
    88.  
    89.         CASE PictureBox1
    90.  
    91.         CASE ResistanceLB
    92.  
    93.         CASE TrackBar1
    94.  
    95.         CASE ExitBT
    96.             SYSTEM
    97.     END SELECT
    98.  
    99. SUB __UI_MouseEnter (id AS LONG)
    100.     SELECT CASE id
    101.         CASE Spring
    102.  
    103.         CASE PictureBox1
    104.  
    105.         CASE ResistanceLB
    106.  
    107.         CASE TrackBar1
    108.  
    109.         CASE ExitBT
    110.  
    111.     END SELECT
    112.  
    113. SUB __UI_MouseLeave (id AS LONG)
    114.     SELECT CASE id
    115.         CASE Spring
    116.  
    117.         CASE PictureBox1
    118.  
    119.         CASE ResistanceLB
    120.  
    121.         CASE TrackBar1
    122.  
    123.         CASE ExitBT
    124.  
    125.     END SELECT
    126.  
    127. SUB __UI_FocusIn (id AS LONG)
    128.     SELECT CASE id
    129.         CASE TrackBar1
    130.  
    131.         CASE ExitBT
    132.  
    133.     END SELECT
    134.  
    135. SUB __UI_FocusOut (id AS LONG)
    136.     'This event occurs right before a control loses focus.
    137.     'To prevent a control from losing focus, set __UI_KeepFocus = True below.
    138.     SELECT CASE id
    139.         CASE TrackBar1
    140.  
    141.         CASE ExitBT
    142.  
    143.     END SELECT
    144.  
    145. SUB __UI_MouseDown (id AS LONG)
    146.     SELECT CASE id
    147.         CASE Spring
    148.  
    149.         CASE PictureBox1
    150.  
    151.         CASE ResistanceLB
    152.  
    153.         CASE TrackBar1
    154.  
    155.         CASE ExitBT
    156.  
    157.     END SELECT
    158.  
    159. SUB __UI_MouseUp (id AS LONG)
    160.     SELECT CASE id
    161.         CASE Spring
    162.  
    163.         CASE PictureBox1
    164.  
    165.         CASE ResistanceLB
    166.  
    167.         CASE TrackBar1
    168.  
    169.         CASE ExitBT
    170.  
    171.     END SELECT
    172.  
    173. SUB __UI_KeyPress (id AS LONG)
    174.     'When this event is fired, __UI_KeyHit will contain the code of the key hit.
    175.     'You can change it and even cancel it by making it = 0
    176.     SELECT CASE id
    177.         CASE TrackBar1
    178.  
    179.         CASE ExitBT
    180.  
    181.     END SELECT
    182.  
    183. SUB __UI_TextChanged (id AS LONG)
    184.     SELECT CASE id
    185.     END SELECT
    186.  
    187. SUB __UI_ValueChanged (id AS LONG)
    188.     SELECT CASE id
    189.         CASE TrackBar1
    190.  
    191.     END SELECT
    192.  
    193. SUB __UI_FormResized
    194.  
    195.  
    196. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
    197.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
    198.  
    199. SUB CircleFill (x AS LONG, y AS LONG, R AS LONG, C AS _UNSIGNED LONG)
    200.     x0 = R
    201.     y0 = 0
    202.     e = 0
    203.     DO WHILE y0 < x0
    204.         IF e <= 0 THEN
    205.             y0 = y0 + 1
    206.             LINE (x - x0, y + y0)-(x + x0, y + y0), C, BF
    207.             LINE (x - x0, y - y0)-(x + x0, y - y0), C, BF
    208.             e = e + 2 * y0
    209.         ELSE
    210.             LINE (x - y0, y - x0)-(x + y0, y - x0), C, BF
    211.             LINE (x - y0, y + x0)-(x + y0, y + x0), C, BF
    212.             x0 = x0 - 1
    213.             e = e - 2 * x0
    214.         END IF
    215.     LOOP
    216.     LINE (x - R, y)-(x + R, y), C, BF
    217.  
    218. FUNCTION dist! (x1!, y1!, x2!, y2!)
    219.     dist! = _HYPOT((x2! - x1!), (y2! - y1!))
    220.  
    221. FUNCTION distB! (v1 AS vector, v2 AS vector)
    222.     distB! = dist!(v1.x, v1.y, v2.x, v2.y)
    223.  
    224. SUB thickLine (x1 AS SINGLE, y1 AS SINGLE, x2 AS SINGLE, y2 AS SINGLE, lineWeight%)
    225.     DIM a AS SINGLE, x0 AS SINGLE, y0 AS SINGLE
    226.     DIM prevDest AS LONG, prevColor AS _UNSIGNED LONG
    227.     STATIC colorSample AS LONG
    228.  
    229.     IF colorSample = 0 THEN
    230.         colorSample = _NEWIMAGE(1, 1, 32)
    231.     END IF
    232.  
    233.     prevDest = _DEST
    234.     prevColor = _DEFAULTCOLOR
    235.     _DEST colorSample
    236.     PSET (0, 0), prevColor
    237.     _DEST prevDest
    238.  
    239.     a = _ATAN2(y2 - y1, x2 - x1)
    240.     a = a + _PI / 2
    241.     x0 = 0.5 * lineWeight% * COS(a)
    242.     y0 = 0.5 * lineWeight% * SIN(a)
    243.  
    244.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), colorSample TO(x1 - x0, y1 - y0)-(x1 + x0, y1 + y0)-(x2 + x0, y2 + y0), , _SMOOTH
    245.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), colorSample TO(x1 - x0, y1 - y0)-(x2 + x0, y2 + y0)-(x2 - x0, y2 - y0), , _SMOOTH