Author Topic: Scalar Field  (Read 3532 times)

0 Members and 1 Guest are viewing this topic.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Scalar Field
« on: February 14, 2022, 08:13:41 am »
Code: QB64: [Select]
  1.  
  2. _TITLE "Scalar Field"
  3.  
  4. SCREEN _NEWIMAGE(800, 600, 32)
  5.  
  6. TYPE Vector
  7.     x AS DOUBLE
  8.     y AS DOUBLE
  9.  
  10. DIM SHARED Position(10) AS Vector
  11. DIM SHARED Velocity(10) AS Vector
  12. DIM AS DOUBLE f, f1, f3, f7, f9, g
  13.  
  14. FOR j = 1 TO UBOUND(Position)
  15.     Position(j).x = _WIDTH * (RND - .5)
  16.     Position(j).y = _HEIGHT * (RND - .5)
  17.     Velocity(j).x = 5 * (RND - .5)
  18.     Velocity(j).y = 5 * (RND - .5)
  19.  
  20.     CLS
  21.  
  22.     FOR j = 1 TO UBOUND(Position)
  23.         Position(j).x = Position(j).x + Velocity(j).x
  24.         Position(j).y = Position(j).y + Velocity(j).y
  25.         IF (Position(j).x < -_WIDTH / 2) THEN Velocity(j).x = -Velocity(j).x
  26.         IF (Position(j).x > _WIDTH / 2) THEN Velocity(j).x = -Velocity(j).x
  27.         IF (Position(j).y < -_HEIGHT / 2) THEN Velocity(j).y = -Velocity(j).y
  28.         IF (Position(j).y > _HEIGHT / 2) THEN Velocity(j).y = -Velocity(j).y
  29.     NEXT
  30.  
  31.     FOR j = -_WIDTH / 2 TO _WIDTH / 2 STEP 10
  32.         FOR k = -_HEIGHT / 2 TO _HEIGHT / 2 STEP 10
  33.             f = 15 * CalcPotential#(j, k)
  34.             CALL CCircle(j, k, 4, _RGB(255 * f, 255 * f / 2, 255 * f / 4))
  35.             'NEXT
  36.             'NEXT
  37.  
  38.             'FOR j = -_WIDTH / 2 TO _WIDTH / 2 STEP 10
  39.             'FOR k = -_HEIGHT / 2 TO _HEIGHT / 2 STEP 10
  40.             f1 = 10 * CalcPotential#(j - 5, k - 5)
  41.             f3 = 10 * CalcPotential#(j + 5, k - 5)
  42.             f7 = 10 * CalcPotential#(j - 5, k + 5)
  43.             f9 = 10 * CalcPotential#(j + 5, k + 5)
  44.             FOR g = .15 TO .65 STEP .1
  45.                 f = .5
  46.                 IF (f1 > g) AND (f3 > g) AND (f7 > g) AND (f9 > g) THEN f = 1
  47.                 IF (f1 < g) AND (f3 < g) AND (f7 < g) AND (f9 < g) THEN f = 0
  48.                 IF (f = .5) THEN
  49.                     IF (f1 < g) AND (f3 < g) AND (f7 > g) AND (f9 > g) THEN CALL CLine(j - 5, k, j + 5, k, _RGB(255, 255, 255 / 2))
  50.                     IF (f1 > g) AND (f3 > g) AND (f7 < g) AND (f9 < g) THEN CALL CLine(j - 5, k, j + 5, k, _RGB(255, 255, 255 / 2))
  51.                     IF (f1 > g) AND (f3 < g) AND (f7 > g) AND (f9 < g) THEN CALL CLine(j, k - 5, j, k + 5, _RGB(255, 255, 255 / 2))
  52.                     IF (f1 < g) AND (f3 > g) AND (f7 < g) AND (f9 > g) THEN CALL CLine(j, k - 5, j, k + 5, _RGB(255, 255, 255 / 2))
  53.                     IF (f1 > g) AND (f3 < g) AND (f7 < g) AND (f9 < g) THEN CALL CLine(j - 5, k, j, k - 5, _RGB(255, 255, 255 / 2))
  54.                     IF (f1 < g) AND (f3 > g) AND (f7 < g) AND (f9 < g) THEN CALL CLine(j, k - 5, j + 5, k, _RGB(255, 255, 255 / 2))
  55.                     IF (f1 < g) AND (f3 < g) AND (f7 > g) AND (f9 < g) THEN CALL CLine(j - 5, k, j, k + 5, _RGB(255, 255, 255 / 2))
  56.                     IF (f1 < g) AND (f3 < g) AND (f7 < g) AND (f9 > g) THEN CALL CLine(j, k + 5, j + 5, k, _RGB(255, 255, 255 / 2))
  57.                 END IF
  58.             NEXT
  59.         NEXT
  60.     NEXT
  61.  
  62.     _DISPLAY
  63.     _LIMIT 60
  64.  
  65.  
  66. FUNCTION CalcPotential# (x0 AS DOUBLE, y0 AS DOUBLE)
  67.     DIM TheReturn AS DOUBLE
  68.     DIM AS INTEGER j
  69.     DIM AS DOUBLE dx, dy, r
  70.     TheReturn = 0
  71.     FOR j = 1 TO UBOUND(Position)
  72.         dx = x0 - Position(j).x
  73.         dy = y0 - Position(j).y
  74.         r = SQR(dx * dx + dy * dy)
  75.         TheReturn = TheReturn + 1 / r
  76.     NEXT
  77.     CalcPotential# = TheReturn
  78.  
  79. SUB CCircle (x0 AS DOUBLE, y0 AS DOUBLE, rad AS DOUBLE, shade AS _UNSIGNED LONG)
  80.     CIRCLE (_WIDTH / 2 + x0, -y0 + _HEIGHT / 2), rad, shade
  81.  
  82. SUB CLine (x0 AS DOUBLE, y0 AS DOUBLE, x1 AS DOUBLE, y1 AS DOUBLE, shade AS _UNSIGNED LONG)
  83.     LINE (_WIDTH / 2 + x0, -y0 + _HEIGHT / 2)-(_WIDTH / 2 + x1, -y1 + _HEIGHT / 2), shade
  84.  
You're not done when it works, you're done when it's right.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Scalar Field
« Reply #1 on: February 14, 2022, 09:41:45 am »
That' pretty cool. Like something you'd see in a droplet of water under a microscope. In your code you use vectors and I can appreciate why in terms of displaying the results but doesn't SCALAR imply no vectors?

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Scalar Field
« Reply #2 on: February 14, 2022, 10:38:08 am »
I'm still allowed to use TYPEs, aren't I? Haha.

Naw, the reason you know its a scalar thing is CalculatePotential returns one number, not two. Feel me on that? (Using concepts other than electricity, temperature is one number, so scalar. Wind is more than one number, so vector.)
You're not done when it works, you're done when it's right.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Scalar Field
« Reply #3 on: February 14, 2022, 01:16:15 pm »
Ya, I'm feeling it. As for TYPE meaning "a category of people or things having common characteristics" you by all means can use TYPE. In fact, given that all programmers fall into a TYPE, it should be mandatory usage in our coding.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Scalar Field
« Reply #4 on: February 14, 2022, 03:26:44 pm »
Not too different:
Code: QB64: [Select]
  1. _Title "Build Contours Rnd Pts 2- zzz... press any, esc to quit" ' b+ 2021-11-11
  2. ' 2022-02-14 mod to move points around
  3.  
  4. _ScreenMove 400, 60
  5. tile(1) = "."
  6. tile(2) = "~"
  7. tile(3) = "="
  8. tile(4) = "o"
  9. tile(5) = "#"
  10.  
  11. Width 80, 80
  12.  
  13. Re:
  14. nP = Rnd * 7 + 3
  15. ReDim pX(1 To nP), pY(1 To nP), dx(1 To nP), dy(1 To nP)
  16. For i = 1 To nP
  17.     pX(i) = Rnd * _Width * .9 + .05 * _Width
  18.     dx(i) = Rnd * 2 - 1
  19.     pY(i) = Rnd * _Height * .9 + .05 * _Height
  20.     dy(i) = Rnd * 1 - 1
  21.  
  22.     Cls
  23.     If InKey$ = " " Then GoTo Re
  24.     For level = 1 To 5
  25.         For i = 1 To nP
  26.             'move points
  27.             pX(i) = pX(i) + dx(i)
  28.             If pX(i) < 2 Or pX(i) > 78 Then dx(i) = -dx(i)
  29.             pY(i) = pY(i) + dy(i)
  30.             If pY(i) < 2 Or pY(i) > 78 Then dy(i) = -dy(i)
  31.  
  32.             If Rnd < .5 Then dir = -1 Else dir = 1
  33.             x = pX(i) + dir * (Rnd * (6 - level) * 1)
  34.             If Rnd < .5 Then dir = -1 Else dir = 1
  35.             y = pY(i) + dir * (Rnd * (6 - level) * 1)
  36.             R = (Rnd * (6 - level) * 3) + 5 * (6 - level)
  37.             For row = y - R To y + R
  38.                 For col = x - R To x + R
  39.                     If row >= 1 And row <= _Height Then
  40.                         If col > 1 And col <= _Width Then
  41.                             If Sqr(((col - x) ^ 2) + (row - y) ^ 2) <= R Then
  42.                                 Locate row, col: Print tile(level);
  43.                             End If
  44.                         End If
  45.                     End If
  46.                 Next
  47.             Next
  48.  
  49.         Next
  50.     Next
  51.     _Display
  52.     _Limit 5
  53.  
  54.  
  55.