kedaman
Mar 8th, 2001, 06:34 PM
Put it all in a form, run and enjoy:
Option Explicit
'Kedamans Charges and Electric Field Demo
' 1. mouse down where you want to have a chage
' 2. drag to set the size
' 3. hold shift down for positive chages
Const Grid! = 50 'Decrease this value (not below 15) for more more gridpoints ( _
cool), increase to see the field lines (also cool) to 100 or 150
Const ShowCharges As Boolean = True
Const Escale! = 2000000 'Field Strength Scale Constant
Private Type P2S
X As Single
Y As Single
End Type
Private Type Charge
Mag As Single
Pos As P2S
End Type
Private Enum enmmode
None
DrawCharge
End Enum
Dim mode As enmmode, current As Charge, _
charges() As Charge, chargescount As Long
Private Sub Draw()
On Error Resume Next
Dim x!, y!, n&, sumx!, sumy!, dx!, dy!, m!, q!
Cls
For Y = ScaleTop To ScaleTop + ScaleHeight Step Grid
For X = ScaleLeft To ScaleWidth Step Grid
sumx = 0
sumy = 0
For N = 0 To chargescount - 1
With charges(N)
dx = .Pos.X - X
dy = .Pos.Y - Y
M = (dx * dx + dy * dy)
Q = Sqr(M)
If Q > 10 And M > 10 Then
sumx = sumx + dx * .Mag / (M * Q) * Escale
sumy = sumy + dy * .Mag / (M * Q) * Escale
End If
End With
Next N
M = sumx * sumx + sumy * sumy
Q = Sqr(M)
'if you want you could make a function that gradiently shifts between colors
If Q Then Line (X, Y)-Step( _
sumx / Q * 100, sumy / Q * 100), RGB((M / 1000) And 255, _
(M / 100) And 255, (M / 10000) And 255)
'this is another version, doesn't look cool though, red and logaritmic
'If q Then Line (x, y)-Step(sumx / q * 100, sumy / q * 100), (Log(m) * 10)
'If m < 10000000 Then Line (x, y)-Step(sumx / 10, sumy / 10), RGB((Log(m) * 2) And 255, _
(Log(m) * 4) And 255, (Log(m) * 6) And 255)
Next X
Next Y
If ShowCharges Then
For N = 0 To chargescount - 1
With charges(N)
FillColor = QBColor(Sgn(.Mag) * 1.5 + 2.1)
Circle (.Pos.X, .Pos.Y), Abs(.Mag)
End With
Next N
End If
End Sub
Private Sub Form_Load()
Caption = "Kedamans Electric field and chages demo: mouse down where you want to have a chage, drag to set the size, _
hold shift down for positive chages"
FillStyle = vbDiagonalCross
WindowState = vbMaximized
'BackColor = 0
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
current.Pos.X = X
current.Pos.Y = Y
mode = DrawCharge
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
Dim dx!, dy!
If mode = DrawCharge Then
With current
dx = .Pos.X - X
dy = .Pos.Y - Y
FillColor = QBColor(Shift * 3 + 1)
Circle (.Pos.X, .Pos.Y), Sqr(dx * dx + dy * dy)
End With
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
Dim dx!, dy!
dx = current.Pos.X - X
dy = current.Pos.Y - Y
current.Mag = Sqr(dx * dx + dy * dy) * Sgn(Shift - 0.5)
mode = None
ReDim Preserve charges(chargescount)
charges(chargescount) = current
chargescount = chargescount + 1
Draw
End Sub
Private Sub Form_Resize()
Draw
End Sub
'Code improved by vBulletin Tool (http://orion.spaceports.com/~mccloud/vbtool.zip) (Save as...)
Option Explicit
'Kedamans Charges and Electric Field Demo
' 1. mouse down where you want to have a chage
' 2. drag to set the size
' 3. hold shift down for positive chages
Const Grid! = 50 'Decrease this value (not below 15) for more more gridpoints ( _
cool), increase to see the field lines (also cool) to 100 or 150
Const ShowCharges As Boolean = True
Const Escale! = 2000000 'Field Strength Scale Constant
Private Type P2S
X As Single
Y As Single
End Type
Private Type Charge
Mag As Single
Pos As P2S
End Type
Private Enum enmmode
None
DrawCharge
End Enum
Dim mode As enmmode, current As Charge, _
charges() As Charge, chargescount As Long
Private Sub Draw()
On Error Resume Next
Dim x!, y!, n&, sumx!, sumy!, dx!, dy!, m!, q!
Cls
For Y = ScaleTop To ScaleTop + ScaleHeight Step Grid
For X = ScaleLeft To ScaleWidth Step Grid
sumx = 0
sumy = 0
For N = 0 To chargescount - 1
With charges(N)
dx = .Pos.X - X
dy = .Pos.Y - Y
M = (dx * dx + dy * dy)
Q = Sqr(M)
If Q > 10 And M > 10 Then
sumx = sumx + dx * .Mag / (M * Q) * Escale
sumy = sumy + dy * .Mag / (M * Q) * Escale
End If
End With
Next N
M = sumx * sumx + sumy * sumy
Q = Sqr(M)
'if you want you could make a function that gradiently shifts between colors
If Q Then Line (X, Y)-Step( _
sumx / Q * 100, sumy / Q * 100), RGB((M / 1000) And 255, _
(M / 100) And 255, (M / 10000) And 255)
'this is another version, doesn't look cool though, red and logaritmic
'If q Then Line (x, y)-Step(sumx / q * 100, sumy / q * 100), (Log(m) * 10)
'If m < 10000000 Then Line (x, y)-Step(sumx / 10, sumy / 10), RGB((Log(m) * 2) And 255, _
(Log(m) * 4) And 255, (Log(m) * 6) And 255)
Next X
Next Y
If ShowCharges Then
For N = 0 To chargescount - 1
With charges(N)
FillColor = QBColor(Sgn(.Mag) * 1.5 + 2.1)
Circle (.Pos.X, .Pos.Y), Abs(.Mag)
End With
Next N
End If
End Sub
Private Sub Form_Load()
Caption = "Kedamans Electric field and chages demo: mouse down where you want to have a chage, drag to set the size, _
hold shift down for positive chages"
FillStyle = vbDiagonalCross
WindowState = vbMaximized
'BackColor = 0
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
current.Pos.X = X
current.Pos.Y = Y
mode = DrawCharge
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
Dim dx!, dy!
If mode = DrawCharge Then
With current
dx = .Pos.X - X
dy = .Pos.Y - Y
FillColor = QBColor(Shift * 3 + 1)
Circle (.Pos.X, .Pos.Y), Sqr(dx * dx + dy * dy)
End With
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
Dim dx!, dy!
dx = current.Pos.X - X
dy = current.Pos.Y - Y
current.Mag = Sqr(dx * dx + dy * dy) * Sgn(Shift - 0.5)
mode = None
ReDim Preserve charges(chargescount)
charges(chargescount) = current
chargescount = chargescount + 1
Draw
End Sub
Private Sub Form_Resize()
Draw
End Sub
'Code improved by vBulletin Tool (http://orion.spaceports.com/~mccloud/vbtool.zip) (Save as...)