VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   3195
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3195
   ScaleWidth      =   4680
   StartUpPosition =   3  'Windows Default
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
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! = 100 '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 = False
Const Escale! = 20000000 '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
        CurrentX = 0
        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 q Then Line -(x - Log(m) * 50, y - Log(m) * 90), RGB(Log(m) And 255, (Log(m) * 9) And 255, (Log(m) * 6) And 255)
        Next x
    Next y
    For x = ScaleLeft To ScaleWidth Step Grid
        CurrentY = 0
        For y = ScaleTop To ScaleTop + ScaleHeight 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 q Then Line -(x - Log(m) * 50, y - Log(m) * 90), RGB(Log(m) And 255, (Log(m) * 6) And 255, (Log(m) * 6) And 255)
        Next y
    Next x
    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
