### Author Topic: Vector Field Simulator  (Read 188 times)

#### STxAxTIC

• Library Staff
• Forum Resident
• Posts: 692
• Savage.
##### Vector Field Simulator
« on: February 10, 2020, 02:00:40 AM »
Code: QB64: [Select]
1. CONST Aquamarine = _RGB32(127, 255, 212)
2. CONST Lime = _RGB32(0, 255, 0)
3.
4.
5. SCREEN _NEWIMAGE(600, 600, 32)
6. DIM SHARED XSize
7. DIM SHARED YSize
8. DIM SHARED XCells
9. DIM SHARED YCells
10. XSize = INT(_WIDTH / 25)
11. YSize = INT(_HEIGHT / 25)
12. XCells = INT(_WIDTH / XSize)
13. YCells = INT(_HEIGHT / YSize)
14. NPC = .1 * SQR(XCells * YCells)
15.
16. TYPE Vector
17.
18. TYPE FieldLine
19.     Center AS Vector
20.     Tangent AS Vector
21.
22. TYPE Particle
23.     Displacement AS Vector
24.     Velocity AS Vector
26.
27. TYPE Charge
28.     Center AS Vector
30.     Curl AS Vector
31.
32. DIM VectorField(XCells, YCells) AS FieldLine
33. DIM Particles(XCells, YCells, NPC) AS Particle
34. DIM Charges(100) AS Charge
35. DIM SHARED ChargeCount
36.
37. ChargeCount = 1
38. Charges(ChargeCount).Center.x = 0
39. Charges(ChargeCount).Center.y = 0
42. Charges(ChargeCount).Curl.x = 0
43. Charges(ChargeCount).Curl.y = 0
44.
45. FOR i = 1 TO XCells
46.     FOR j = 1 TO YCells
47.         VectorField(i, j).Center.x = (1 / 2) * XSize * (2 * i - XCells) - XSize / 2
48.         VectorField(i, j).Center.y = (1 / 2) * YSize * (2 * j - YCells) - YSize / 2
49.         FOR k = 1 TO NPC
50.             Particles(i, j, k).Shade = Lime
51.             Particles(i, j, k).Displacement.x = XSize * (RND - .5)
52.             Particles(i, j, k).Displacement.y = YSize * (RND - .5)
53.
54. GOSUB ResetVectorField
55.
56.         x = _MOUSEX
57.         y = _MOUSEY
58.         IF ((x > 0) AND (x < _WIDTH) AND (y > 0) AND (y < _HEIGHT)) THEN
59.             Charges(ChargeCount).Center.x = (x - _WIDTH / 2)
60.             Charges(ChargeCount).Center.y = (-y + _HEIGHT / 2)
61.             GOSUB ResetVectorField
62.
63.     k = _KEYHIT
64.         CASE 49
67.             Charges(ChargeCount).Curl.x = 0
68.             Charges(ChargeCount).Curl.y = 0
69.         CASE 50
72.             Charges(ChargeCount).Curl.x = 0
73.             Charges(ChargeCount).Curl.y = 0
74.         CASE 51
77.             Charges(ChargeCount).Curl.x = 0
78.             Charges(ChargeCount).Curl.y = 0
79.         CASE 52
82.             Charges(ChargeCount).Curl.x = 0
83.             Charges(ChargeCount).Curl.y = 0
84.         CASE 53
87.             Charges(ChargeCount).Curl.x = .05
88.             Charges(ChargeCount).Curl.y = -.05
89.         CASE 54
92.             Charges(ChargeCount).Curl.x = -.05
93.             Charges(ChargeCount).Curl.y = .05
94.         CASE 32
95.             ChargeCount = ChargeCount + 1
96.             Charges(ChargeCount).Center.x = Charges(ChargeCount - 1).Center.x
97.             Charges(ChargeCount).Center.y = Charges(ChargeCount - 1).Center.y
100.             Charges(ChargeCount).Curl.x = Charges(ChargeCount - 1).Curl.x
101.             Charges(ChargeCount).Curl.y = Charges(ChargeCount - 1).Curl.y
102.     IF (k <> 0) THEN
103.         GOSUB ResetVectorField
104.
105.     LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA(0, 0, 0, 20), BF
106.     CALL cline(0, 0, _WIDTH / 2, 0, Aquamarine)
107.     CALL cline(0, 0, -_WIDTH / 2, 0, Aquamarine)
108.     CALL cline(0, 0, 0, _HEIGHT / 2, Aquamarine)
109.     CALL cline(0, 0, 0, -_HEIGHT / 2, Aquamarine)
110.
111.     LOCATE 1, 1: PRINT "Press 1-6 to change charge type. Press space to fix charge."
112.     FOR i = 1 TO XCells
113.         FOR j = 1 TO YCells
114.             xc = VectorField(i, j).Center.x
115.             yc = VectorField(i, j).Center.y
116.             FOR k = 1 TO NPC
117.                 xd = 0
118.                 yd = 0
119.                 xx = Particles(i, j, k).Displacement.x + .1 * Particles(i, j, k).Velocity.x
120.                 yy = Particles(i, j, k).Displacement.y + .1 * Particles(i, j, k).Velocity.y
121.                 IF (xx < -XSize / 2) THEN
122.                     xd = -xx + XSize / 2
123.                 IF (xx > XSize / 2) THEN
124.                     xd = -xx - XSize / 2
125.                 IF (yy < -YSize / 2) THEN
126.                     yd = -yy + YSize / 2
127.                 IF (yy > YSize / 2) THEN
128.                     yd = -yy + -YSize / 2
129.                 Particles(i, j, k).Displacement.x = xx + xd
130.                 Particles(i, j, k).Displacement.y = yy + yd
131.                 CALL cpset(xc + Particles(i, j, k).Displacement.x, yc + Particles(i, j, k).Displacement.y, Particles(i, j, k).Shade)
132.     _LIMIT 60
133.
134. ResetVectorField:
135. FOR i = 1 TO XCells
136.     FOR j = 1 TO YCells
137.         xx = 0
138.         yy = 0
139.         FOR k = 1 TO ChargeCount
140.             dx = VectorField(i, j).Center.x - Charges(k).Center.x
141.             dy = VectorField(i, j).Center.y - Charges(k).Center.y
142.             d2 = 5000 / (dx * dx + dy * dy)
143.             xx = xx + (Charges(k).Gradient.x * dx * d2) + (Charges(k).Curl.x * dy * d2)
144.             yy = yy + (Charges(k).Gradient.y * dy * d2) + (Charges(k).Curl.y * dx * d2)
145.         VectorField(i, j).Tangent.x = xx
146.         VectorField(i, j).Tangent.y = yy
147.         FOR k = 1 TO NPC
148.             Particles(i, j, k).Velocity.x = VectorField(i, j).Tangent.x
149.             Particles(i, j, k).Velocity.y = VectorField(i, j).Tangent.y
150.
151.
152. SUB cline (x1, y1, x2, y2, col)
153.     LINE (_WIDTH / 2 + x1, -y1 + _HEIGHT / 2)-(_WIDTH / 2 + x2, -y2 + _HEIGHT / 2), col
154.
155. SUB cpset (x1, y1, col)
156.     PSET (_WIDTH / 2 + x1, -y1 + _HEIGHT / 2), col
157.
« Last Edit: February 10, 2020, 02:04:49 AM by STxAxTIC »
An ounce of theory outweighs a pound of code.

#### EricE

• Newbie
• Posts: 77
##### Re: Vector Field Simulator
« Reply #1 on: February 10, 2020, 08:50:12 AM »
Hello STxAxTIC,
I like this program!
Sources, sinks, and rotational motion displayed very nicely.
A good demonstrator for Div, Grad and Curl.
EricE

#### _vince

• Forum Regular
• Posts: 196
##### Re: Vector Field Simulator
« Reply #2 on: February 16, 2020, 08:46:15 PM »
This kind of thing can look nice with a bunch of little propellers

#### STxAxTIC

• Library Staff
• Forum Resident
• Posts: 692
• Savage.
##### Re: Vector Field Simulator
« Reply #3 on: February 16, 2020, 09:52:56 PM »
Funny thing vince - I thought about doing that to demonstrate the curl... and for that matter i thought about an inflatable membrane that would respond to divergence, but I digress... What I *can* see is maybe making a silly game where you have to get a particle from one part of the screen to the other, along field lines caused by charges. Harder levels would have obstacles so you'd have to get the particle to suck into a magnet and out the other end or whatever... Maybe instead of one particle, you have to take care of a fleet of them like lemmings. Hmm.
An ounce of theory outweighs a pound of code.